PHP使用 Imagick 扩展实现图片合成,圆角处理功能示例
发布:smiling 来源: PHP粉丝网 添加日期:2021-12-16 10:40:29 浏览: 评论:0
本文实例讲述了PHP使用 Imagick 扩展实现图片合成,圆角处理功能,分享给大家供大家参考,具体如下:
需求:为用户生成特定的二维码 ,拉取用户的微信头像 和特定的背景图合成一张用户专属海报
方法:采用PHP的Imagick扩展功能对图片进行合成处理,对微信头像进行圆角处理,然后压缩图片的质量
1. 根据微信用户特定id生成专属二维码
- public static function getTicket($scene_id)
- {
- $qrcode = '{"expire_seconds": 2592000, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": $scene_id }}}'; //二维码信息
- $access_token = self::getToken(); //公众号token,这个要获取自己公众号的
- $getticket_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$access_token";
- $ticketinfo = self::request_by_curl($getticket_url,$qrcode);
- return $ticketinfo['ticket']; //专属二维码的ticken
- }
- public static function request_by_curl($remote_server, $post_string='')
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $remote_server);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: "));
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
- $content = curl_exec($ch);
- curl_close($ch);
- $reArr=json_decode($content,true);
- return $reArr;
- }
2、 合成海报
- public function CompositeImage ($ticket, $wxnick, $userId)
- {
- $Qrcode = new Imagick("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=$ticket");
- $Qrcode->setImageResolution(0.1,0.3); //设置图片分辨率
- $QrcodeWH = $Qrcode->getImageGeometry(); //获取源图片宽和高
- if ($QrcodeWH['width']>200) {
- $QrcodeW['width'] = 200;
- $QrcodeH['height'] = $QrcodeW['width']/$QrcodeWH['width']*$QrcodeWH['height'];
- } else {
- $QrcodeW['width'] = $QrcodeWH['width'];
- $QrcodeH['height'] = $QrcodeWH['height'];
- }
- $Qrcode->thumbnailImage( $QrcodeW['width'], $QrcodeWH['height'], true ); //按照选定的比例进行缩放
- // 预先下载微信头像,再生成合成信息
- $curl = curl_init($wxnick);
- $wxnickpath = "upload/wxnick/".$userId.".jpg";
- curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
- $imageData = curl_exec($curl);
- curl_close($curl);
- $tp = @fopen($wxnickpath, 'a');
- fwrite($tp, $imageData);
- fclose($tp);
- $weixin = new Imagick($wxnickpath);
- $weixin->setImageResolution(0.1,0.3);
- $weixin->roundCorners(360,360); //圆角处理
- $wxWH = $weixin->getImageGeometry();
- if ($wxWH['width']>200) {
- $wxW['width'] = 200;
- $wxH['height'] = $wxW['width']/$wxWH['width']*$wxWH['height'];
- } else {
- $wxW['width'] = $wxWH['width'];
- $wxH['height'] = $wxWH['height'];
- }
- $weixin->thumbnailImage( $wxW['width'], $wxWH['height'], true );//等比例缩放
- //创建一个Imagick对象,同时获取要处理的背景图 /data/wenda/htdocs/upload
- $poster = new Imagick( "/data/wenda/htdocs/upload/poster.png" );
- $posterWH = $poster->getImageGeometry();
- $posterW['width'] = $posterWH['width'];
- $posterH['height'] = $posterWH['height'];
- // 按照缩略图大小创建一个有颜色的图片
- $canvas = new Imagick();
- $canvas->newImage( $posterW['width'], $posterH['height'], 'black', 'jpg' );
- //二维码 微信头像 背景 合成
- $poster->compositeImage($Qrcode,Imagick::COMPOSITE_OVER,275,960);
- $poster->compositeImage($weixin,Imagick::COMPOSITE_OVER,275,402);
- $canvas->compositeImage( $poster, imagick::COMPOSITE_OVER, 0, 0);
- $canvas->setImageCompressionQuality(60); //压缩质量
- $canvas->writeImage( "/upload/poster/$userId.jpg" ); //生成图片
- return $canvas; //返回图片流信息
- }
- header( "Content-Type: image/jpg" ); //输出图片
- $posterimg = $this->CompositeImage($Fticket, $Fwnick, $userId);
- echo $posterimg //输出图片
Tags: Imagick PHP图片合成
相关文章
- ·php_imagick实现图片剪切、旋转、锐化、减色或增加特效(2013-11-14)
- ·php利用Imagick把pdf生成png缩略图(2014-08-18)
- ·使用Imagick绘图 文字换行 生成二维码例子(2015-12-24)
- ·PHP使用imagick读取PDF生成png缩略图的两种方法(2020-10-29)
- ·php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法(2021-05-03)
- ·PHP中使用Imagick实现各种图片效果实例(2021-05-08)
- ·PHP中使用Imagick读取pdf并生成png缩略图实例(2021-05-08)
- ·PHP中使用Imagick操作PSD文件实例(2021-05-08)
- ·PHP中使用imagick实现把PDF转成图片(2021-05-08)
- ·php使用Imagick生成图片的方法(2021-06-15)
- ·PHP利用imagick生成组合缩略图(2021-07-10)
- ·PHP Imagick完美实现图片裁切、生成缩略图、添加水印(2021-07-10)
- ·php图片合成方法(多张图片合成一张)(2021-08-21)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)