1.封装函数
//生成二维码
function qr_user_code($url_code,$uid){
header("Access-Control-Allow-Origin:*");
vendor('phpqrcode.phpqrcode');//引用phpqrcode类包
$url = $url_code;
$errorCorrectionLevel = 'H';//容错级别
$matrixPointSize = 5;//图片大小慢慢自己调整,只要是int就行
$path = 'user_cache/';
if (!file_exists($path)) {
mkdir($path);
}
$QR =$QRB = $path.rand(10000,99999).time().".png";
\QRcode::png($url, $QR, $errorCorrectionLevel, $matrixPointSize, 2);
$dst_path = '20180326171846.png';//web\lALPBbCc1VATaEvNA4DNAnw_636_896.png
$src_path = $QR;//覆盖图
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
//获取覆盖图图片的宽高
list($src_w, $src_h) = getimagesize($src_path);
//将覆盖图复制到目标图片上,最后个参数100是设置透明度(100是不透明),这里实现不透明效果
imagecopymerge($dst, $src, 184, 410, 0, 0, 286, 286, 100);
// @unlink($QIMG); //删除二维码与logo的合成图片
@unlink($QRB); //删除服务器上二维码图片
header("Content-type: image/png");
imagepng($dst,'uploads/user/'.$uid.'.png');
// imagejpeg($dst,'../uploads/user/'.$uid.'.jpg');
imagedestroy($dst);
imagedestroy($src);
}
2.调用步骤一的函数
public function qr_code($uid){
$url = 'http://ypyz.yxyzyl.com/template/mobile/xian/static/takehtml/personalhtml/bind.html?uid='.$uid;
qr_user_code($url,$uid);//因为我封装在common里面的,所以这样调用
$time = time();
$path = '/uploads/user/'.$uid.'.png';
$sha1 = sha1($uid.$time.'.png');
$res = (new Us())->where(['user_id'=>$uid])->update(['qr_code'=>$sha1]);
$re = db('picture')->insert([
'path'=>$path,
'sha1'=>$sha1,
'status'=>1,
'create_time'=>$time
]);
if ($res !== false&&$re!=false) {
$this->assign('path',$path);
return $this->fetch();
} else {
$this->error('生成二维码失败', '');
}
}