2017-08-05 86 views
0

我使用PHP QR碼(http://phpqrcode.sourceforge.net/)創建QR碼。它運作良好,但現在我需要一個自由空間來放置自定義圖形或徽標。我想在不保存服務器上的圖像的情況下執行此操作。有沒有人有建議?我到目前爲止是這樣的:使用PHP QR碼生成器在PHP中創建QR碼與中心徽標

<?php 
    $param = $_GET['projectid']; 
    $divider = ","; 

    $codeText = 'Projectname'.$divider.$param; 

    // outputs image directly into browser, as PNG stream 
    //QRcode::png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false) 
    QRcode::png($codeText, false, QR_ECLEVEL_H, 9, 2, true); 
?> 

回答

0

好的,我找到了一個解決方案。在創建一個圖像文件臨時插入的標誌或任何你想要的。我只是在這裏發現的代碼非常小的變化http://ourcodeworld.com/articles/read/225/how-to-generate-qr-code-with-logo-easily-in-php-automatically 我在最後使用readfile()將所有內容直接推送到輸出緩衝區。

<?php 
     // user input   
     $param = $_GET['projectid']; 
     $divider = ","; 

     // Path where the images will be saved 
     $filepath = 'content/images/qr/qr-temp-image.png'; 
     // Image (logo) to be drawn 
     $logopath = 'content/images/qr/qr-freespace.png'; 
     // we need to be sure ours script does not output anything!!! 
     // otherwise it will break up PNG binary! 
     ob_start("callback"); 

     // text for the qr code 
     $codeText = 'Projectname'.$divider.$param; 

     // end of processing here 
     $debugLog = ob_get_contents(); 
     ob_end_clean(); 

     // create a QR code and save it in the filepath 
     QRcode::png($codeText, $filepath, QR_ECLEVEL_H, 9, 2, true); 

     // Start DRAWING LOGO IN QRCODE 

     $QR = imagecreatefrompng($filepath); 

     // START TO DRAW THE IMAGE ON THE QR CODE 
     $logo = imagecreatefromstring(file_get_contents($logopath)); 
     $QR_width = imagesx($QR); 
     $QR_height = imagesy($QR); 

     $logo_width = imagesx($logo); 
     $logo_height = imagesy($logo); 

     // Scale logo to fit in the QR Code 
     $logo_qr_width = $QR_width/3; 
     $scale = $logo_width/$logo_qr_width; 
     $logo_qr_height = $logo_height/$scale; 

     imagecopyresampled($QR, $logo, $QR_width/3, $QR_height/3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); 

     // Save QR code again, but with logo on it 
     imagepng($QR,$filepath); 
     // outputs image directly into browser, as PNG stream 
     readfile($filepath); 
?> 
+0

徽標未在qr碼的中心對齊。徽標寬度= 234px和高度= 107px –

+0

嗨,你想讓我看看你的代碼和使用的圖像?也許我可以看到導致問題的原因。 –

+0

您是否嘗試過不同的QR碼大小?我不確定,但是也許您的寬度爲234像素的徽標太大,以至於大小爲9的QR碼? QRcode :: png($ codeText,$ filepath,QR_ECLEVEL_H,12,2,true)。 –