使用 PHP QR 代码生成器在 PHP 中创建带有居中徽标的 QR 代码
Creating a QR Code with a centered Logo in PHP with PHP QR Code Generator
我正在使用 PHP QR 码 (http://phpqrcode.sourceforge.net/) 创建 QR 码。它运行良好,但现在我需要一个免费的 space 用于其中心的自定义图形或徽标。我想在不将图像保存在服务器上的情况下执行此操作。有人有建议吗?到目前为止我得到的是:
<?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);
?>
我正在使用 PHP QR 码 (http://phpqrcode.sourceforge.net/) 创建 QR 码。它运行良好,但现在我需要一个免费的 space 用于其中心的自定义图形或徽标。我想在不将图像保存在服务器上的情况下执行此操作。有人有建议吗?到目前为止我得到的是:
<?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);
?>