My intention is save the generated qr code image on my local.
I have already checked out whole stackoverflow question about it. However they didn't help me to solve this bug.
<?php
header('Content-type: image/png');
$filename = "./qrs/qr-6234/qr.png";
$link = "https://stackoverflow.com";
$size = 250;
$url = urlencode ( $link );
$qr_url = "http://chart.googleapis.com/chart?chs=$sizex$size&cht=qr&chld=L|0&chl=$url&choe=UTF-8";
$qr = file_get_contents($qr_url);
$imgIn = imagecreatefrompng ($qr);
$imgOut = imagecreate ( $size, $size );
imagecopy ( $imgOut, $imgIn, 0, 0, 0, 0, $size, $size );
imagepng ( $imgOut, $filename, 9);
imagedestroy ( $imgIn );
imagedestroy ( $imgOut );
?>
I don't know why but, it gives me zero byte png file.
Edit: Thanks to ishagg, I got my error logs. These are ;
Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in ./qr/qr_txt_test.php on line 10
Warning: file_get_contents(http://chart.googleapis.com/chart?chs=250&cht=qr&chld=L|0&chl=https%3A%2F%2Fstackoverflow.com&choe=UTF-8): failed to open stream: no suitable wrapper could be found in ./qr/qr_txt_test.php on line 10
Warning: imagecreatefromstring(): Empty string or invalid image in ./qr/qr_txt_test.php on line 11
Warning: imagecopy() expects parameter 2 to be resource, boolean given in ./qr/qr_txt_test.php on line 13
Warning: imagepng(): gd-png error: no colors in palette in ./qr/qr_txt_test.php on line 14
Warning: imagedestroy() expects parameter 1 to be resource, boolean given in ./qr/qr_txt_test.php on line 15
If you remove the header()
call, you'll be able to see the errors in the script.
In yours, there are two:
$qr_url
is undefined.imagecreatefromstring()
.With these two mistakes fixed, the code works correctly:
<?php
header('Content-type: image/png');
$filename = "qr.png";
$link = "https://stackoverflow.com";
$size = 250;
$url = urlencode ( $link );
$qr_url = "http://chart.googleapis.com/chart?chs=$size&cht=qr&chld=L|0&chl=$url&choe=UTF-8";
$qr = file_get_contents($qr_url);
$imgIn = imagecreatefromstring($qr);
$imgOut = imagecreate ( $size, $size );
imagecopy ( $imgOut, $imgIn, 0, 0, 0, 0, $size, $size );
imagepng ( $imgOut, $filename, 9);
imagedestroy ( $imgIn );
imagedestroy ( $imgOut );
Result: