我的目的是将生成的qr代码图像保存在我的本地。
我已经查看了有关它的整个stackoverflow问题。但是他们没有帮我解决这个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 );
?>
我不知道为什么但它给了我零字节的png文件。
编辑:感谢ishagg,我收到了错误日志。这些是;
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
答案 0 :(得分:1)
如果您删除header()
来电,则可以在脚本中看到错误。
在你的,有两个:
$qr_url
中使用的变量$ sizex未定义。imagecreatefromstring()
。修复了这两个错误,代码正常工作:
<?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 );
结果: