【问题标题】:Creating a QR Code with a centered Logo in PHP with PHP QR Code Generator使用 PHP QR 码生成器在 PHP 中创建带有居中徽标的 QR 码
【发布时间】:2018-01-13 05:30:47
【问题描述】:

我正在使用 PHP 二维码 (http://phpqrcode.sourceforge.net/) 创建二维码。它运行良好,但现在我需要一个可用空间来在其中心放置自定义图形或徽标。我想这样做而不将图像保存在服务器上。有人建议吗?到目前为止,我得到的是:

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

【问题讨论】:

    标签: php qr-code


    【解决方案1】:

    好的,我找到了解决方案。临时创建图像文件以插入徽标或您想要的任何内容的位置。我不仅仅是在这里找到的代码的一个很小的变化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);
    ?>
    

    【讨论】:

    • 标志未在二维码中心对齐。徽标宽度 = 234 像素,高度 = 107 像素
    • 您好,您想给我看看您的代码和使用过的图片吗?也许我可以看到导致问题的原因。
    • 你试过不同尺寸的二维码吗?我不确定,但也许你的 234 像素宽度的标志对于 9 号的二维码来说太大了?在此处尝试更高的数字 QRcode::png($codeText, $filepath, QR_ECLEVEL_H, 12, 2, true )。
    猜你喜欢
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2016-05-23
    • 2011-12-15
    • 2011-05-10
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多