1

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
ziLk
  • 3,120
  • 21
  • 45

1 Answers1

1

If you remove the header() call, you'll be able to see the errors in the script.

In yours, there are two:

  • Variable $sizex, used in $qr_url is undefined.
  • To create a new image from an external resource, you need to use 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:

qr image

ishegg
  • 9,685
  • 3
  • 16
  • 31
  • Thank you for your feedback. I have changed my code with yours. unfortunately, the result is same. It gave me zero byte qr.png file. – ziLk Oct 14 '17 at 12:39
  • Try removing the `header()` call again, what errors do you get? – ishegg Oct 14 '17 at 12:39
  • I can't see any error log in my page. Maybe admin, disabled to show error logs. I don't have access php.ini file :/ – ziLk Oct 14 '17 at 12:54
  • Add this to the top of the page: `ini_set("display_errors", "On"); error_reporting(E_ALL);` – ishegg Oct 14 '17 at 12:55
  • Oh my god!, there are several warning message. Thank you very much for your help! – ziLk Oct 14 '17 at 12:57
  • 1
    It's no problem, brother. If you have more doubts don't hesitate to ask. Only, if they differ *too* much from the original question, maybe ask a new one, to keep things tidy. Good luck! – ishegg Oct 14 '17 at 13:00
  • @Zilk I saw you edit. All the errors stem from the first one: you can't fetch external resources with `file_get_contents()` because of your server configuration. If you can't change `allow_url_fopen` to `On` (which I think you can't), have a look at [this answer](https://stackoverflow.com/questions/21677579/server-configuration-by-allow-url-fopen-0-in) – ishegg Oct 14 '17 at 13:06
  • Thank you so much for your help. – ziLk Oct 14 '17 at 14:32