使用PHP二维码生成器在PHP中创建带有居中徽标的二维码

2022-04-14 00:00:00 qr-code php

我正在用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 );
?>

解决方案

好的,我找到了一个解决方案。在那里创建一个临时图像文件以插入徽标或任何您想要的东西。我不只是对这里的代码做了很小的更改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);
?>

相关文章