staempfli / magento2-module-image-resizer

Magento 2 Module to add simple image resizing capabilities in all blocks and .phtml templates
93 stars 32 forks source link

Feature Request: Add Adaptive Resizing #11

Open jahvi opened 6 years ago

jahvi commented 6 years ago

Great extension, I was wondering if you could integrate adaptive resizing into it (similar to this other extension) it's probably out of scope but I find it very useful.

687474703a2f2f692e696d6775722e636f6d2f726b7938532e706e67

As I can see this is the part of the code that handles the resizing but I haven't been able to integrate into this extension: https://github.com/trive-digital/AdaptiveResizeM2/blob/master/Model/Product/Image.php#L66-L80

jahvi commented 6 years ago

I managed to make this work by roughly replacing this line with the following code:

$currentRatio = $imageAdapter->getOriginalWidth() / $imageAdapter->getOriginalHeight();
$targetRatio = $this->width / $this->height;

if ($targetRatio > $currentRatio) {
    $imageAdapter->resize($this->width, null);
} else {
    $imageAdapter->resize(null, $this->height);
}

$diffWidth = $imageAdapter->getOriginalWidth() - $this->width;
$diffHeight = $imageAdapter->getOriginalHeight() - $this->height;

$imageAdapter->crop(
    floor($diffHeight * 0.5),
    floor(($diffWidth / 2) * 1),
    ceil(($diffWidth / 2) * 1),
    ceil($diffHeight * 0.5)
);

Hopefully it can help.

jalogut commented 6 years ago

Hi @jahvi

Thanks for the suggestion. To be honest, I do not think that we will add this feature soon as we do not need it for now. However, if you feel like investing time on it, we'd glad to receive a PR and merge it into the module.

vishalpatel19862 commented 5 years ago

@jahvi

https://github.com/trive-digital/AdaptiveResizeM2/blob/master/Model/Product/Image.php#L66-L80

I have installed this extension.But it is not working.

1.$this->helper('Trive\AdaptiveResize\Helper\Image')->init($_product)->adaptiveResize(240,300); 2.$this->helper('Trive\AdaptiveResize\Helper\Image')->init($_product,'category_page_grid')->adaptiveResize(240,300); 3.$this->helper('Trive\AdaptiveResize\Helper\Image')->init($_product,'category_page_grid')->setCropPosition('top')->adaptiveResize(240);

I do not know from where to make call to this helper class. Please let me know where to call helper class?