yii2-imagine
yii2-imagine copied to clipboard
add feature to crop image durring resizing to avoid visual deffects
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:
What's expected?
My image will resize to 2346x1290 and than crop left and right side, so i expected to get:
What do you get instead?
My image was compressed in width and sctretched in height. So, I get:
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 |
$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.