2017-08-05 102 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)。 –