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

Trying to resize an image's side below 0px causes an error #196

Open
dominikkaluza opened this issue Sep 16, 2024 · 0 comments
Open

Trying to resize an image's side below 0px causes an error #196

dominikkaluza opened this issue Sep 16, 2024 · 0 comments

Comments

@dominikkaluza
Copy link

Hi,

we've found an use case where the image resizer is not working as expected.

In our case, this happens when using the resizeToWidth and resizeToHeight methods but the issue can be present in more places.

Inside the method a ratio is calculated and from that, the desired height/width is also calculated. It may happen that the calculated dimension is <0px which causes an error.

Example:
Original size: 600x2 px
Desired width is 100px max, so I use the resizeToWidth method.

public function resizeToWidth($width, $allow_enlarge = false)
{
    $ratio  = $width / $this->getSourceWidth(); // 0.16666666666
    $height = (int) round($this->getSourceHeight() * $ratio); // 0.33333333332  -> 0

    $this->resize($width, $height, $allow_enlarge);

    return $this;
}

Then, when I try to get the image's content using the getImageAsString method, it fails with:

imagecreatetruecolor(): Invalid image dimensions

I through about adding some minimum size in the resize method, but I'm not sure if it wouldn't break something else:

$this->dest_w = max($width, 1);
$this->dest_h = max($height, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant