//PHP设置二维码中间为logo
$qcode = './qcode.png';
$logo = imgZoom('./big.jpg','small.png',0.08);
list($q_width, $q_height)=getimagesize($qcode);
list($l_width, $l_height)=getimagesize($logo);
$x_qcode_logo = ($q_width-$l_width)/2;
$y_qcode_logo = ($q_height-$l_height)/2;
$image_qcode = imagecreatefrompng($qcode);
$image_logo = imagecreatefrompng($logo);
//合成照片
//imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )---拷贝并合并图像的一部分
//将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
imagecopymerge($image_qcode, $image_logo, $x_qcode_logo,$y_qcode_logo, 0, 0, imagesx($image_logo), imagesy($image_logo), 100);
$merge = 'merge_logo.png';
var_dump(imagepng($image_qcode,$merge));
/**
* 将指定图片按指定比例等比缩放
* @param [type] $filename 原图片
* @param [type] $newFile 缩放后的图片
* @param integer $per 缩放比例
* @return [type] 生成存放的路径
*/
function imgZoom($filename,$newFile,$per=1){
if(!$filename || !$newFile){
return flase;
}
list($width, $height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$height*$per;
$new=imagecreatetruecolor($n_w, $n_h);
$img=imagecreatefromjpeg($filename);
//copy部分图像并调整
imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
//图像输出新图片、另存为
imagecolorallocatealpha($new, 255, 255, 255, 127);
imagepng($new, $newFile);
imagedestroy($new);
imagedestroy($img);
return './'.$newFile;
}
/**
* PHP生成二维码
* @param [type] $url 扫描二维码跳转的url地址
* @param [type] $m 生成二维码的空白区域大小
* @param string $logo 二维码中间logo的地址
* @return [type] 二维码图片直接输出在页面
*/
function qrcodeLogo() {
$url = !empty($_GET['url']) ? trim($_GET['url']) : 'http://blog.58haha.cn';
$m = !empty($_GET['m']) ? trim($_GET['m']) : 2;
$logo = 'https://www.baidu.com/img/bd_logo1.png';
if($url) {
include_once '../phpqrcode/phpqrcode.php';
$qrcode = new QRcode();
if(!empty($logo)){
$qrcode::png($url,'qrcode.png', 'L', 7, $m);
createLogo($logo);
}else{
$qrcode::png($url, false, 'L', 7, $m);
}
}
}
/**
* 生成logo
*/
function createLogo($logo){
$QR = 'qrcode.png';
$QR = imagecreatefromstring(file_get_contents($QR));
$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);
// 输出图像
header("Content-type: image/png");
imagepng($QR);
}