前期准备
- 引入phpqrcode类库(下载地址:https://download.csdn.net/download/weixin_37557729/11891240;支持彩色二维码的下载地址:https://download.csdn.net/download/weixin_37557729/11891244)
- PHP开启GD扩展库支持
1、利用phpqrcode生成二维码:
原理分析:
下载下来的类文件是一个压缩包,包含很多文件和演示程序,我们只需要里边的phpqrcode.php
这一个文件就可以生成二维码了,它是一个多个类的集合文件,我们需要用到里边的QRcode类的png()方法:
//测试生成带头像的网站二维码海报 public function ce_haibao_qrcode(){ $codeurl = "https://www.baidu.com"; Vendor('PHPQRcode.class#phpqrcode'); $logoQR = "http://thirdwx.qlogo.cn/mmopen/vi_32/icaYhiapVcmsyGnHouHeSvYiaz8yxtvfBicgx5x8joGh4uNiaibp8skQf8Uv4CNtibsJDndbOQwI9LSvDQP6slFQaLy4g/132"; $level=3; $size=6; $errorCorrectionLevel =intval($level) ;//容错级别 $matrixPointSize = intval($size);//生成图片大小 $dirPath ='./Uploads/qrcode/'.date('Y-m-d').'/';//保存二维码路径 $dirPath_B ='/Uploads/qrcode/'.date('Y-m-d').'/';//保存二维码路径 if(!file_exists($dirPath)){ mkdir($dirPath, 0777); } $tmpName = time().".png"; //保存在服务器上的二维码名称 $qrcodeName = $dirPath .$tmpName; //保存在服务器上的二维码路径 $qrcodeName_B = $dirPath_B .$tmpName; //保存在服务器上的二维码路径 //生成二维码图片 $object = new \QRcode(); $object->png($codeurl, $qrcodeName, $errorCorrectionLevel, $matrixPointSize, 2); //$QR = imagecreatefrompng($qrcodeName); $QR = $qrcodeName; $QRlogoPath = './Uploads/qrcode/qrlogo/'.date('Y-m-d').'/'; $QRlogoPath_B = '/Uploads/qrcode/qrlogo/'.date('Y-m-d').'/'; if(!file_exists($QRlogoPath)){ mkdir($QRlogoPath, 0777); } $qrcode = time().'.png'; $QRlogo = $QRlogoPath .$qrcode; $QRlogo_B = $QRlogoPath_B .$qrcode; if($logoQR !==FALSE){ $QR = imagecreatefromstring(file_get_contents($QR)); $logo = imagecreatefromstring(file_get_contents($logoQR)); $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');
//合成带logo的二维码 ImagePng($QR,$QRlogo); $dst_path = './Uploads/haibao/haibao.png';//海报素材底图 $src_path = $QRlogo; //覆盖图,用上面的那张图QRlogo $haibaoPath = './Uploads/qrcode/haibao/'.date('Y-m-d').'/'; //$haibaoPath_B = '/Uploads/qrcode/haibao/'.date('Y-m-d').'/'; if(!file_exists($haibaoPath)){ mkdir($haibaoPath, 0777); } $haibao = time().'.png'; $haibao = $haibaoPath .$haibao; //$haibao_B = $haibaoPath_B .$haibao; //创建图片实例 $dst = imagecreatefromstring(file_get_contents($dst_path));//海报 $src = imagecreatefromstring(file_get_contents($src_path));//二维码 //获取覆盖图的宽高 list($src_w, $src_h) = getimagesize($src_path); //获取海报的宽高 list($dst_w, $dst_h) = getimagesize($dst_path); // imagecopymerge($dst, $src, 20, 120, 0, 0, $src_w, $src_h, 100); imagecopymerge($dst, $src, ($dst_w-$src_w)/2, $dst_h-100-$src_h, 0, 0, $src_w, $src_h, 100); list($dst_w, $dst_h) = getimagesize($dst_path); imagepng($dst,$haibao);//生成图片并保存到服务器上 合成的海报 imagedestroy($dst); imagedestroy($src); $haibao = substr($haibao,1);//去掉左边第一个小点 $arr['msg'] = $haibao; echo json_encode($arr);exit; } }