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

resize function does not check size values #296

Open
Madpeterz opened this issue Mar 2, 2022 · 2 comments
Open

resize function does not check size values #296

Madpeterz opened this issue Mar 2, 2022 · 2 comments
Labels

Comments

@Madpeterz
Copy link

Madpeterz commented Mar 2, 2022

Versions:
PHP 7.4.27 (cli)
claviska/simpleimage ^3.6

if you pass invalid values to the resize function it does not throw an error and ends up
creating bad images

Warnings

Warning: imagecreatetruecolor(): Invalid image dimensions in 
vendor\claviska\simpleimage\src\claviska\SimpleImage.php on line 805

Warning: imagecolorallocatealpha() expects parameter 1 to be resource, bool given in vendor\claviska\simpleimage\src\claviska\SimpleImage.php on line 806

Warning: imagecolortransparent() expects parameter 1 to be resource, bool given in vendor\claviska\simpleimage\src\claviska\SimpleImage.php on line 807

Warning: imagefill() expects parameter 1 to be resource, bool given in 
vendor\claviska\simpleimage\src\claviska\SimpleImage.php on line 808

Warning: imagecopyresampled() expects parameter 1 to be resource, bool given in vendor\claviska\simpleimage\src\claviska\SimpleImage.php on line 816

[Broken] Code
(You should use the change as a percentage so you dot get zero or negative heights :P )

    try {
        $image->fromFile($path2image);
        $filesizeold = filesize($path2image);
        if (($image->getWidth() > 1000) && ($filesizekb > 400)) {
            $change = $image->getWidth() - 750;
            $image->resize($image->getWidth() - $change, $image->getHeight() - $change);
            $image->toFile($path2image, null, 75);
        }
    } catch (Exception $e) {
        // --
    }
@Madpeterz Madpeterz changed the title Images from file can fail in fun and weird ways but not trigger try catch resize function does not check size values Mar 2, 2022
@claviska
Copy link
Owner

claviska commented Mar 2, 2022

Thanks for reporting this. I'm not sure there's anything to do here, as runtime type checking isn't something I want to add to every method. I agree that it would be more graceful to catch these instead of seeing warnings.

What do you propose?

@Madpeterz
Copy link
Author

maybe just 1 check on the result imagecreatetruecolor if that's not a valid resource throw an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants