需要引入 phpqrcode.php 文件
下载地址:https://sourceforge.net/projects/phpqrcode/
$url = 'http://www.baidu.com';
include_once ROOT_PATH.'phpqrcode.php';
$value = $url; //二维码内容
$errorCorrectionLevel = 'H'; //容错级别(L:7%; M:15%; Q:25%;H:30%)
$matrixPointSize = 6; //生成图片大小
//生成二维码图片
$filename = 'C:/logo/'.microtime().'.png'; // 名称用随机数
QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2); //$filename 路径下生成了原始二维码图
$logoPath = 'C:/logo/logo.png'; //准备好的logo图片
if (file_exists($logoPath)) {
$QR = imagecreatefromstring(file_get_contents($filename)); //目标图象连接资源。
$logo = imagecreatefromstring(file_get_contents($logoPath)); //源图象连接资源。
$QR_width = imagesx($QR); //二维码图片宽度
$QR_height = imagesy($QR); //二维码图片高度
$logo_width = imagesx($logo); //logo图片宽度
$logo_height = imagesy($logo); //logo图片高度
$logo_qr_width = $QR_width / 4; //组合之后logo的宽度(占二维码的1/5)
$scale = $logo_width/$logo_qr_width; //logo的宽度缩放比(本身宽度/组合后的宽度)
$logo_qr_height = $logo_height/$scale; //组合之后logo的高度
$from_width = ($QR_width - $logo_qr_width) / 2; //组合之后logo左上角所在坐标点
//重新组合图片并调整大小 imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
}
//输出图片
imagepng($QR, 'C:/logo/qrcode.png');
if (file_exists($logoPath)){
$res = unlink($filename); // 销毁原始二维码
imagedestroy($QR); // 释放关联的内存
imagedestroy($logo);
}
效果图