2017-07-06 52 views
3

我使用此代碼製作具有以下自定義文本的qr代碼。我嘗試了幾種方法,但我總是失敗。我需要幫助。無法使用下面的文本創建QR

// 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); 

而我只是看到空白頁。

+0

''imagecreatefrompng''說:「如果fopen包裝已啓用,則可以使用此URL作爲文件名。 「另一種方法是使用''qr_img_as_string = file_get_contents ...''並使用'imagecreatefromstring''。 – alistaircol

+0

你可以使用var_dump()來檢查(bool)ini_get('allow_url_fopen'));'' – alistaircol

回答

2

你有一個錯誤,但你不能在瀏覽器中查看它,因爲你已經設置了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/

sample qr

GET /?QR =你好

hello 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); 
} 
+1

很好的答案,非常感謝你! – nskmr