thephpleague / color-extractor

Extract colors from an image like a human would do.
thephpleague.com
MIT License
1.3k stars 159 forks source link

PHP 8.1.5 GD image resources bug? #76

Closed UFMurphy closed 2 years ago

UFMurphy commented 2 years ago

Hey guys,

I am doing some tutorials on composer and one of the lessons uses the color-extractor package. All seems to install ok. But I keep getting an error when I try to stream in an image.

This code block seems to cause an issue (line 54-55 - vendor/league/color-extractor/src/League/ColorExtractor/Palette.php): $image = imagecreatefromstring(file_get_contents($filename)); $palette = self::fromGD($image, $backgroundColor);

As does this one (line 71 same file): if (!is_resource($image) || get_resource_type($image) != 'gd')

The condition gets met and we see the following error.

Fatal error: Uncaught InvalidArgumentException: Image must be a gd resource in /Users/ufmurphy/Sites/vendor/league/color-extractor/src/League/ColorExtractor/Palette.php:72 Stack trace: #0 /Users/ufmurphy/Sites/vendor/league/color-extractor/src/League/ColorExtractor/Palette.php(55): League\ColorExtractor\Palette::fromGD(Object(GdImage), NULL) #1 /Users/ufmurphy/Sites/index.php(19): League\ColorExtractor\Palette::fromFilename('images/bricks.j...') #2 {main} thrown in /Users/ufmurphy/Sites/vendor/league/color-extractor/src/League/ColorExtractor/Palette.php on line 72

Doing some sleuthing, it looks like a change in how GD images are handled in PHP8. https://php.watch/versions/8.0/gdimage

Is there a more recent version than 0.3.2?

Thanks, Jim

UFMurphy commented 2 years ago

Found resolution.

nocturnalpollinator commented 2 years ago

What solution did you find?

UFMurphy commented 2 years ago

@nocturnalpollinator ..

I changed this line (line 71 in vendor/league/color-extractor/src/League/ColorExtractor/Palette.php) if (!is_resource($image) || get_resource_type($image) != 'gd')

to this line: if (!($image instanceof \GdImage) && (!is_resource($image) || get_resource_type($image) != 'gd')) {