BUG : Image not readable on PHP 8
This must be a bug that should be corrected for PHP 8.
Getting... error on
Image::make(DIR.'/asset/logo/logo.png')->opacity(50);
Fatal error: Uncaught Intervention\Image\Exception\NotReadableException: Image source not readable in...
I have the same issue:
Error: {message: "Image source not readable", exception: "Intervention\Image\Exception\NotReadableException",…} exception: "Intervention\Image\Exception\NotReadableException" file: "....\vendor\intervention\image\src\Intervention\Image\AbstractDecoder.php" line: 346 message: "Image source not readable"
php --version PHP 8.0.6 (cli) (built: May 4 2021 23:31:45) ( ZTS Visual C++ 2019 x64 ) Copyright (c) The PHP Group Zend Engine v4.0.6, Copyright (c) Zend Technologies
$ php artisan --version Laravel Framework 8.43.0
For my mistake I had a solution. My code above was something like the following:
$layout = imagecreate(1, 1);
imagecolorallocate($layout, 255, 255, 255);
$base = ImageManagerStatic::make($layout)->resize(500, 200);
My solution was to use an Intervention object, for this case a canvas:
$layout = ImageManagerStatic::canvas(1, 1, '#ffffff');
$base = ImageManagerStatic::make($layout)->resize(500, 200);
Intervention\Image\AbstractDecoder
Now instead of validating the resource as a GD resource with:
/**
* Initiates new image from mixed data
*
* @param mixed $data
* @return \Intervention\Image\Image
*/
public function init($data)
{
$this->data = $data;
switch (true) {
case $this->isGdResource():
return $this->initFromGdResource($this->data);
Pass the validation of an Intervention instance with:
/**
* Initiates new image from mixed data
*
* @param mixed $data
* @return \Intervention\Image\Image
*/
public function init($data)
{
$this->data = $data;
switch (true) {
case $this->isInterventionImage():
return $this->initFromInterventionImage($this->data);
$ php --version
PHP 8.0.7 (cli) (built: Jun 2 2021 00:41:03) (ZTS Visual C ++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.0.7, Copyright (c) Zend Technologies
$ php artisan --version
Laravel Framework 8.48.2
Workaround
Because PHP 8 had changes with respect to the GD library, the validations that were done with versions 7. x and earlier with is_resource and get_resource_type functions are no longer supported, all this because now the gd extension uses GdImage objects instead of gd type resources.
https://www.php.net/manual/en/migration80.incompatible.php
Resource to Object Migration
Several resources have been migrated to objects. Return value checks using is_resource() should be replaced with checks for false. The GD extension now uses GdImage objects as the underlying data structure for images, rather than resources. The imagedestroy () function no longer has an effect; instead the GdImage instance is automatically destroyed if it is no longer referenced.
Since there have been no recent Intervention updates to fix the PHP 8 compatibility bug, how workaround can be changed:
Intervention\Image\AbstractDecoder
This
/**
* Determines if current source data is GD resource
*
* @return boolean
*/
public function isGdResource()
{
if (is_resource($this->data)) {
return (get_resource_type($this->data) == 'gd');
}
return false;
}
To
/**
* Determines if current source data is GD resource
*
* @return boolean
*/
public function isGdResource()
{
return $this->data instanceof \GdImage;
}
Returned value PHP 7.4.5
gd resource @930 ▼
size: "390x200"
trueColor: true
}
PHP 8.0.7
GdImage {#1691 ▼
+size: "390x200"
+trueColor: true
}
Hope this works for you.
I see that there is a commit regarding the explanation I gave earlier as a workaround, but it has not been updated on https://packagist.org/packages/intervention/image and that it has been done since December 2020. Issue #1061 PR #1059
This has been updated on packagist. Version 2.6.0+ support PHP 8.
Fixed by #1059