claviska / SimpleImage

A PHP class that makes working with images and GD as simple as possible.
MIT License
1.38k stars 382 forks source link

resize function does not check size values #296

Open Madpeterz opened 2 years ago

Madpeterz commented 2 years ago

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) {
        // --
    }
claviska commented 2 years ago

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 commented 2 years ago

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