php-image-resize
php-image-resize copied to clipboard
Image not centering correctly
Hi,
I'm using this library to create the same image but in different sizes. Everything is ok when the height of the new size is greater than the width, the image is correctly centered vertically, but when the height is smaller than the width the image doesn't center horizontally.
This is my code:
if($size['height'] == 'auto'){
$image->resizeToWidth($size['width'], true);
$exactSize = false;
}elseif($size['width'] == 'auto'){
$image->resizeToHeight($size['height'], true);
$exactSize = false;
}else{
$exactSize = [
$size['width'],
$size['height']
];
$image->resizeToBestFit($size['width'], $size['height']);
}
if($image->save($target, null, null, null, $exactSize)){
$response = $this->response->withType('application/json')
->withStringBody(true);
}
This code is inside a foreach, and $size is an array with height and width.
These are the results:

I don't understand why this happens, vertically is fine but not horizontally.
If you could help me I would be so grateful.
Thanks.
Hi @BSmerT, thank you for your report.
If possible, could you provide your image that you manipulate?
And it will help us to try reproducing issue easily :).
Sure, here it is.

Maybe the issue is here, I've recently uploaded a square image and it doesn't center at all.
ImageResize.php:293
if( !empty($exact_size) && is_array($exact_size) ) {
if ($this->getSourceHeight() < $this->getSourceWidth()) {
$this->dest_x = 0;
$this->dest_y = ($exact_size[1] - $this->getDestHeight()) / 2;
}
if ($this->getSourceHeight() > $this->getSourceWidth()) {
$this->dest_x = ($exact_size[0] - $this->getDestWidth()) / 2;
$this->dest_y = 0;
}
}
I modified the code:
if( !empty($exact_size) && is_array($exact_size) ) {
$this->dest_x = ($exact_size[0] - $this->getDestWidth()) / 2;
$this->dest_y = ($exact_size[1] - $this->getDestHeight()) / 2;
// if ($this->getSourceHeight() < $this->getSourceWidth()) {
// $this->dest_x = 0;
// $this->dest_y = ($exact_size[1] - $this->getDestHeight()) / 2;
// }
// if ($this->getSourceHeight() > $this->getSourceWidth()) {
// $this->dest_x = ($exact_size[0] - $this->getDestWidth()) / 2;
// $this->dest_y = 0;
// }
}
This works perfectly. I don't know if this can affect something else, but with this modification all the images in every size, no matter what, are centered.
Thank for your quick response, btw!
@BSmerT, thank you for your reply.
I cannot reproduce issue you mention because I got the result images look fine for me without adding any comment in source code.
And I use the packcge version is 1.9.2.
Here are my code snippets:
<?php
require_once 'vendor/autoload.php';
use Gumlet\ImageResize;
// The file
$filename = 'issue_#142.jpg';
$image = new ImageResize($filename);
$image->resizeToBestFit(210, 300);
$image->save('image_issue#142_210_300.jpg', null, null, null, [210, 300]);
$filename = 'image_issue#142_210_300.jpg';
$image = new ImageResize($filename);
$image->resizeToBestFit(250, 120);
$image->save('image_issue#142_250_120.jpg', null, null, null, [250, 120]);
Resized image results are as follows:
210 * 300

250 * 120

Hi @peter279k,
I noticed that in the second resize you used the resized version of the image, not the original. I've tried your solution and it did work, but if you use the original image in the second resize or if you resize it to 250x120 first, and then you resize it to 210x300 the results aren't correct.
Using your code snippet and resizing it to 250x120 first, these are the results:
250x120

210x300

Hi @BSmerT, thank you for your reply.
It's about checking the source width/height.
The image resizing work will check the source width/height before setting destination width/height.
Resizing to image size 210x300 is fine because the source height is longer than source width.
And resizing image size 250x120 is not good because source height is shorter than source width.
And the image source width and height checking will cause the image has the incorrect position.
As you mention your modified code, removing source width/height checking and set destination width/height directly will make the destination image position fine.
After tracing source code, it looks like this code is added since this PR is merged.
Maybe @felipetnh can explain why it should check source width/height before setting destination width/height.
Please take a look at this, @adityapatadia, thanks.
@felipetnh, @adityapatadia
I will try to find what's wrong and if I find something I'll tell you. Or if you find something please let me know.
Thanks @peter279k for your help.
This is my new account: @IsaacJara
Any update on this?
Any update on this?
Hi @IsaacJara, thank you for your concern. Just wait for these authors I mention on previous comments 😄 .
Please take a look at this, @adityapatadia, thanks.
I am not able to understand what action I need to take.
Please take a look at this, @adityapatadia, thanks.
I am not able to understand what action I need to take.
@adityapatadia, you can look at this PR and know about this issue.
I have already checked it. It's merged. Is there any change required in current master branch behaviour? I am sorry I don't have time to look into this in great detail.
I have already checked it. It's merged. Is there any change required in current master branch behaviour? I am sorry I don't have time to look into this in great detail.
@adityapatadia, I also mention some latest comments on this PR.
In absence of @felipetnh I think we should get that fixed. @peter279k what do you say?