你有一个错误,但你不能在浏览器中查看它,因为你已经设置了Content-Type: image/png
这样的调试只是注释掉该行或检查你的服务器日志。
我想起的第一件事是this answer使用相对于您的字体的路径。这足以引发一个警告,即如果输出到屏幕会让您的图像变得模糊不已,更不用说您不会获得所需的字体。我会fix this line。
$font = realpath(__DIR__.'/arial.ttf');
对我来说,致命的错误是:
调用未定义功能imagecopymerge_alpha()
我不知道你在哪里得到的代码,但是我发现这样this question我认为它可能是相关的。
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying relevant section from background to the cut resource
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// copying relevant section from watermark to the cut resource
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
// insert cut resource to destination image
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
然后我注意到了白色标记为黑色,并设置为两个背景和文本颜色 - 因此,无论哪种颜色的它,那将是不可见的。所以我changed these lines。 (-
意味着删除线和+
手段添加的行。)
// Create the image
$im = imagecreatetruecolor(150, 30);
// Create some colors
-$black = imagecolorallocate($im, 255, 255, 255);
+$white = imagecolorallocate($im, 255, 255, 255);
+$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $black);
// Font path
$font = realpath(__DIR__.'/arial.ttf');
// Add the text
-imagettftext($im, 20, 0, 5, 25, $black, $font, 'sample');
+imagettftext($im, 20, 0, 5, 25, $white, $font, 'sample');
imagecopymerge_alpha($main, $qr, 0, 0, 0, 0, 150, 150, 100);
imagecopymerge_alpha($main, $im, 0, 150, 0, 0, 150, 30, 100);
imagepng($main);
最后,而不是硬编码字sample
,只是为了好玩我设置的查询字符串。
+$text = $_GET['qr'] ?? 'sample';
+
// Set the content-type
header('Content-Type: image/png');
header("Content-Disposition: filename='sample.png'");
$main = imagecreatetruecolor(150, 180);
-$qr = imagecreatefrompng("https://api.qrserver.com/v1/create-qr-code/?size=150x150&format=png&margin=5&data=sample");
+$qr = imagecreatefrompng("https://api.qrserver.com/v1/create-qr-code/?size=150x150&format=png&margin=5&data=$text");
// Create the image
$im = imagecreatetruecolor(150, 30);
// Create some colors
@@ -14,7 +16,7 @@ imagefilledrectangle($im, 0, 0, 399, 29, $black);
// Font path
$font = realpath(__DIR__.'/arial.ttf');
// Add the text
-imagettftext($im, 20, 0, 5, 25, $white, $font, 'sample');
+imagettftext($im, 20, 0, 5, 25, $white, $font, $text);
GET/
GET /?QR =你好
<?php
$text = $_GET['qr'] ?? 'sample';
// Set the content-type
header('Content-Type: image/png');
header("Content-Disposition: filename='sample.png'");
$main = imagecreatetruecolor(150, 180);
$qr = imagecreatefrompng("https://api.qrserver.com/v1/create-qr-code/?size=150x150&format=png&margin=5&data=$text");
// Create the image
$im = imagecreatetruecolor(150, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $black);
// Font path
$font = realpath(__DIR__.'/arial.ttf');
// Add the text
imagettftext($im, 20, 0, 5, 25, $white, $font, $text);
imagecopymerge_alpha($main, $qr, 0, 0, 0, 0, 150, 150, 100);
imagecopymerge_alpha($main, $im, 0, 150, 0, 0, 150, 30, 100);
imagepng($main);
imagedestroy($main);
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying relevant section from background to the cut resource
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// copying relevant section from watermark to the cut resource
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
// insert cut resource to destination image
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
''imagecreatefrompng''说:“如果fopen包装已启用,则可以使用此URL作为文件名。 “另一种方法是使用''qr_img_as_string = file_get_contents ...''并使用'imagecreatefromstring''。 – alistaircol
你可以使用var_dump()来检查(bool)ini_get('allow_url_fopen'));'' – alistaircol