本文主要和大家分享php实现简易合成图片方法,主要以代码的形式和大家分享,希望能帮助到大家。
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('生成二维码失败', '');
}
}
相关推荐:
php imagick 扩展合成图片的两张方法_php教程
以上就是php实现简易合成图片方法的详细内容。