Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feature to crop image durring resizing to avoid visual deffects #48

Open
igorveremsky opened this issue Feb 14, 2018 · 1 comment
Open
Labels
status:ready for adoption Feel free to implement this issue. type:docs Documentation

Comments

@igorveremsky
Copy link

igorveremsky commented Feb 14, 2018

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
@klimov-paul klimov-paul added the type:enhancement Enhancement label Feb 22, 2018
@Renkas
Copy link
Contributor

Renkas commented Mar 26, 2019

$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.

@machour machour added the type:docs Documentation label Mar 26, 2019
@samdark samdark added status:ready for adoption Feel free to implement this issue. and removed type:enhancement Enhancement labels Mar 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:docs Documentation
Projects
None yet
Development

No branches or pull requests

5 participants