include_once('./libs/phpqrcode/phpqrcode.php');
/**
* 生成带logo的二维码
* $codeContents 二维码内容
* $logo logo图片
* $filePath 文件路径
* $fileName 生成二维码图片名称
* $errorCorrectionLevel 容错级别 QR_ECLEVEL_L QR_ECLEVEL_M QR_ECLEVEL_Q QR_ECLEVEL_H
* $pixelSize 像素大小 1==25px 2==50px n==n*25
* $frameSize 框架大小 1==$pixelSize 2==$pixelSize*2 n==$pixelSize*n
* $bgImg 背景图片(这里需要自己手动调整参数)
**/
function generatQrcode($codeContents,$filePath,$fileName,$logo=null,$errorCorrectionLevel="QR_ECLEVEL_M",$pixelSize=9,$frameSize=2,$bgImg){
QRcode::png($codeContents, $filePath.$fileName, $errorCorrectionLevel, $pixelSize, $frameSize);
// 判断是否加logo
if($logo){
$QR = imagecreatefromstring(file_get_contents( $filePath.$fileName ));
$logo = imagecreatefromstring(file_get_contents($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);
//输出图片
imagepng($QR, $filePath.$fileName );
}
// 判断是否有背景图片
if($bgImg){
$backgroupImg = imagecreatefromstring(file_get_contents($bgImg));
$newQR = imagecreatefromstring(file_get_contents($filePath.$fileName));
// 获取新的尺寸
list($width, $height) = getimagesize($filePath.$fileName);
$new_width = 500;
$new_height = 500;;
//重新组合图片并调整大小
imagecopyresampled($backgroupImg,$newQR,220, 220, 0, 0,$new_width, $new_height, $width, $height);
//输出图片
imagepng($backgroupImg, $filePath.$fileName );
}
return true;
}
generatQrcode("http://www.baidu.com","./","copy.png",$logo='./1.png',$errorCorrectionLevel="QR_ECLEVEL_L",$pixelSize=10,$frameSize=2,$bgImg="./bg.png");
效果图:
demo下载地址
参考文档地址:http://www.cnblogs.com/txw1958/p/phpqrcode.html