yii2-imagine icon indicating copy to clipboard operation
yii2-imagine copied to clipboard

add feature to crop image durring resizing to avoid visual deffects

Open igorveremsky opened this issue 7 years ago • 1 comments

What steps will reproduce the problem?

I want to resize image with 2000x1100 dimensions to 1350x1290 without visual deffects. So, i write this code:

$resizedImageObject = Image::resize($originalImageSrc, $newWidth, $newHeight, false, true);

Original image: db-website-avatars-02d

What's expected?

My image will resize to 2346x1290 and than crop left and right side, so i expected to get: db-website-avatars-02d-1350x1290

What do you get instead?

My image was compressed in width and sctretched in height. So, I get: resized-db-website-avatars-02d-1350x1290

Additional info

I solved this with this code:

$originalImageObject = Image::getImagine()->open($originalImageSrc);

$originalImageSizeObject = $originalImageObject->getSize();
$originalImageWidth = $originalImageSizeObject->getWidth();
$originalImageHeight = $originalImageSizeObject->getHeight();

$resizedImageWidth = ($newWidth / $newHeight < $originalImageWidth / $originalImageHeight) ? null : $newWidth;
$resizedImageHeight = ($newWidth / $newHeight > $originalImageWidth / $originalImageHeight) ? null : $newHeight;

$resizedImageObject = Image::resize($originalImageSrc, $resizedImageWidth, $resizedImageHeight, false, true);

$resizedImageSizeObject = $resizedImageObject->getSize();
$resizedImageWidth = $resizedImageSizeObject->getWidth();
$resizedImageHeight = $resizedImageSizeObject->getHeight();

$cropImagePointX = ($resizedImageWidth - $newWidth) / 2;
$cropImagePointY = ($resizedImageHeight - $newHeight) / 2;

$pointForCenterCropImage = new Point($cropImagePointX, $cropImagePointY);
$boxForCenterCropImage = new Box($newWidth, $newHeight);

$resizedImageObject->crop($pointForCenterCropImage, $boxForCenterCropImage);

This feature helps to resize image without visual deffects, to my mind it would be great to implement it in future updates, what do think about it?

Q A
Yii vesion 2.0.13
PHP version 7.1.9
Operating system Windows 10

igorveremsky avatar Feb 14 '18 10:02 igorveremsky

$resizedImageObject = Image::thumbnail($originalImageSrc, $newWidth, $newHeight);

thumbnail method is for doing what you need. resize was added later to do exactly opposite of that. So both usecases are covered AFAIK.

If you think documentaion on it could be improved then some ideas would be helpful.

Renkas avatar Mar 26 '19 12:03 Renkas