endroid / qr-code

QR Code Generator
https://endroid.nl
MIT License
4.44k stars 727 forks source link

Uncaught Endroid\QrCode\Exception\GenerateImageException: Unable to generate image: check your GD installation #308

Closed sonqor closed 3 years ago

sonqor commented 3 years ago

Hey there! I was running my code on version 3.9.6 well.

require_once("qr-code/vendor/autoload.php");
use Endroid\QrCode\QrCode;
$qrCode = new QrCode("TEST");
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 127]);
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();

As I updated the package to version 4.0.0, a PHP fatal error has thrown:

[29-Mar-2021 15:07:36 UTC] PHP Fatal error:  Uncaught Endroid\QrCode\Exception\GenerateImageException: Unable to generate image: check your GD installation in /MY_SCRIPT_PATH/qr-code/vendor/endroid/qr-code/src/Writer/PngWriter.php:74
Stack trace:
#0 /MY_SCRIPT_PATH/qr-code/vendor/endroid/qr-code/src/Writer/PngWriter.php(61): Endroid\QrCode\Writer\PngWriter->createBaseImage()
#1 /MY_SCRIPT_PATH/qr-code/vendor/endroid/qr-code/src/Writer/PngWriter.php(30): Endroid\QrCode\Writer\PngWriter->createImage()
#2 /MY_SCRIPT_PATH/qr-code/vendor/endroid/qr-code/src/QrCode.php(397): Endroid\QrCode\Writer\PngWriter->writeString()
#3 /MY_SCRIPT_PATH/index.php(8): Endroid\QrCode\QrCode->writeString()
#4 {main}
  thrown in /MY_SCRIPT_PATH/qr-code/vendor/endroid/qr-code/src/Writer/PngWriter.php on line 74

Any idea about this error? The PHP version is 8 and GD extension is installed and enabled (checked by phpinfo()). Also, I've tested GD. It works fine.

endroid commented 3 years ago

Hi @sonqor I can not reproduce this issue, but the error states that the imagecreatetruecolor($width, $height) function does not create a valid image resource so you have to look in that direction. For instance you could try calling the imagecreatetruecolor function yourself with some width and height (completely outside any qr code context) and see if it fails. If it does I really think something is wrong with your GD installation. Or maybe you could try some different width / heights or different PHP patch version (I had some issues myself on earlier PHP 8 versions). Please let me know if you have any new findings.

sonqor commented 3 years ago

Hi @sonqor I can not reproduce this issue, but the error states that the imagecreatetruecolor($width, $height) function does not create a valid image resource so you have to look in that direction. For instance you could try calling the imagecreatetruecolor function yourself with some width and height (completely outside any qr code context) and see if it fails. If it does I really think something is wrong with your GD installation. Or maybe you could try some different width / heights or different PHP patch version (I had some issues myself on earlier PHP 8 versions). Please let me know if you have any new findings.

Hi.

I checked imagecreatetruecolor with this code (from https://www.php.net/manual/en/function.imagecreatetruecolor.php) No error occurred and the image created successfully.

But sorry me! Looks like I loaded wrong package version. I did a review of this article: https://github.com/endroid/qr-code/issues/282

Now, there is a new error:

[29-Mar-2021 21:22:14 UTC] PHP Fatal error:  Uncaught TypeError: Endroid\QrCode\QrCode::setBackgroundColor(): Argument #1 ($backgroundColor) must be of type Endroid\QrCode\Color\ColorInterface, array given, called in index.php on line 18 and defined in qr4/vendor/endroid/qr-code/src/QrCode.php:156
Stack trace:
#0 index.php(18): Endroid\QrCode\QrCode->setBackgroundColor()
#1 {main}
  thrown in qr4/vendor/endroid/qr-code/src/QrCode.php on line 156

I have this problem in PHP version 8.0.2 and endroid/qr-code 4.0.0.

Thank you for taking the time to respond to me.

sonqor commented 3 years ago

@endroid I took another brief look at the package documentation and realized that I had to use the Color class. So I changed the code like this: -> setBackgroundColor (new Color (255, 255, 255, 127));

Thanks again for your help.