Php Can';t使用下面的文本创建QR

Php Can';t使用下面的文本创建QR,php,api,imagecreatefrompng,Php,Api,Imagecreatefrompng,我使用此代码制作二维码,并在下面自定义文本。我尝试了几种方法,但总是失败。我需要帮忙 // 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/

我使用此代码制作二维码,并在下面自定义文本。我尝试了几种方法,但总是失败。我需要帮忙

// 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");
// Create the image
$im = imagecreatetruecolor(150, 30);
// Create some colors
$black = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 399, 29, $black);
// Font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 5, 25, $black, $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);
imagedestroy($main);

我只看到一个空白页。

您有一个错误,但您无法在浏览器中查看它,因为您已设置了
内容类型:image/png
,所以要进行调试,只需注释掉该行或检查服务器日志即可

我想到的第一件事是使用字体的相对路径。这足以引发一个警告,如果输出到屏幕上,将使您的图像混乱,更不用说您将没有所需的字体。我会的

对我来说,致命的错误是:

调用未定义的函数imagecopymerge_alpha()

我不确定你从哪里得到的代码,但我找到了,所以我认为可能有关联

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);
}
然后我注意到白色被标记为黑色,并设置为背景色和文本色-因此,无论它是哪种颜色,它都不可见。所以我。(
-
表示删除行,
+
表示添加行。)

最后,为了好玩,我没有硬编码单词
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);
得到/

获取/?qr=你好


imagecreatefrompng
说“如果启用了fopen包装器,URL可以用作此函数的文件名。有关如何指定文件名的详细信息,请参阅fopen()。另一种方法是使用
$qr\u img\u as\u string=file\u get\u contents…
并使用
imagecreatefromstring
。您可以使用
var\u dump((bool)ini\u get('allow\u url\u fopen')进行检查很好的答案。非常感谢!
 // 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);
+$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);
<?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);
}