一、主要功能:
1,利用phpQrcode生成二維碼。
2,将二維碼中間合并logo。
3,生成的二維碼圖檔可以不指定具體的路徑存儲。
二、效果:

三、注意
1,logo圖檔四周最好是圓角,并且四周最好有一圈空白,這樣展示效果比較好。
2,logo圖檔不宜過大,否則二維碼掃不出來。
<?php
class Dg_Helper_Qrcode
{
/**
* 生成二維碼,參數同phpQrcode,注意增加了第二個參數
* @param string $text 生成的資料
* @param string $logo 合并的logo
* @return
*/
static function png($text, $logo = false, $outfile = false, $level = 'H', $size = 5, $margin = 4, $saveandprint=false)
{
include "phpQrcode/qrlib.php";
if ( $outfile ) {
QRcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
$QR = file_get_contents($outfile);
} else {
ob_start();
$QR = QRcode::png($text, $outfile, $level, $size, $margin, $saveandprint);
$QR = ob_get_contents();
ob_end_clean();
}
//合并logo
if ( $logo !== false && $logo = file_get_contents($logo) ) {
$QR = imagecreatefromstring($QR);
$logo = imagecreatefromstring($logo);
$QR_width = imagesx($QR);//二維碼圖檔寬度
$QR_height = imagesy($QR);//二維碼圖檔高度
$logo_width = imagesx($logo);//logo圖檔寬度
$logo_height = imagesy($logo);//logo圖檔高度
//重新組合圖檔并調整大小
// $logo_qr_width = $QR_width / 5;
// $scale = $logo_width/$logo_qr_width;
// $logo_qr_height = $logo_height/$scale;
// $from_width = ($QR_width - $logo_qr_width) / 2;
// imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
//logo不失真
$dstX = $QR_width/2 - $logo_width/2;
$dstY = $QR_width/2 - $logo_width/2;
imagecopyresampled($QR, $logo, $dstX, $dstY, 0, 0, $logo_width, $logo_height, $logo_width, $logo_height);
if ( $outfile ) {
imagepng($QR, $outfile);
} else {
ob_start();
imagepng($QR);
$QR = ob_get_contents();
ob_end_clean();
}
}
return $QR;
}
}