PHP使用 Imagick 扩展实现图片合成,圆角处理功能示例

本文实例讲述了PHP使用 Imagick 扩展实现图片合成,圆角处理功能,分享给大家供大家参考,具体如下:

需求:为用户生成特定的二维码 ,拉取用户的微信头像 和特定的背景图合成一张用户专属海报

方法:采用PHP的Imagick扩展功能对图片进行合成处理,对微信头像进行圆角处理,然后压缩图片的质量

1. 根据微信用户特定id生成专属二维码

  1. public static function getTicket($scene_id)
  2. {
  3. $qrcode = '{"expire_seconds": 2592000, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": $scene_id }}}'; //二维码信息
  4. $access_token = self::getToken(); //公众号token,这个要获取自己公众号的
  5. $getticket_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$access_token";
  6. $ticketinfo = self::request_by_curl($getticket_url,$qrcode);
  7. return $ticketinfo['ticket']; //专属二维码的ticken
  8. }
  9. public static function request_by_curl($remote_server, $post_string='')
  10. {
  11. $ch = curl_init();
  12. curl_setopt($ch, CURLOPT_URL, $remote_server);
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  15. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  16. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  17. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  18. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  19. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  20. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: "));
  21. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  22. $content = curl_exec($ch);
  23. curl_close($ch);
  24. $reArr=json_decode($content,true);
  25. return $reArr;
  26. }

2、 合成海报

  1. public function CompositeImage ($ticket, $wxnick, $userId)
  2. {
  3. $Qrcode = new Imagick("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=$ticket");
  4. $Qrcode->setImageResolution(0.1,0.3); //设置图片分辨率
  5. $QrcodeWH = $Qrcode->getImageGeometry(); //获取源图片宽和高
  6. if ($QrcodeWH['width']>200) {
  7. $QrcodeW['width'] = 200;
  8. $QrcodeH['height'] = $QrcodeW['width']/$QrcodeWH['width']*$QrcodeWH['height'];
  9. } else {
  10. $QrcodeW['width'] = $QrcodeWH['width'];
  11. $QrcodeH['height'] = $QrcodeWH['height'];
  12. }
  13. $Qrcode->thumbnailImage( $QrcodeW['width'], $QrcodeWH['height'], true ); //按照选定的比例进行缩放
  14. // 预先下载微信头像,再生成合成信息
  15. $curl = curl_init($wxnick);
  16. $wxnickpath = "upload/wxnick/".$userId.".jpg";
  17. curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
  18. $imageData = curl_exec($curl);
  19. curl_close($curl);
  20. $tp = @fopen($wxnickpath, 'a');
  21. fwrite($tp, $imageData);
  22. fclose($tp);
  23. $weixin = new Imagick($wxnickpath);
  24. $weixin->setImageResolution(0.1,0.3);
  25. $weixin->roundCorners(360,360); //圆角处理
  26. $wxWH = $weixin->getImageGeometry();
  27. if ($wxWH['width']>200) {
  28. $wxW['width'] = 200;
  29. $wxH['height'] = $wxW['width']/$wxWH['width']*$wxWH['height'];
  30. } else {
  31. $wxW['width'] = $wxWH['width'];
  32. $wxH['height'] = $wxWH['height'];
  33. }
  34. $weixin->thumbnailImage( $wxW['width'], $wxWH['height'], true );//等比例缩放
  35. //创建一个Imagick对象,同时获取要处理的背景图 /data/wenda/htdocs/upload
  36. $poster = new Imagick( "/data/wenda/htdocs/upload/poster.png" );
  37. $posterWH = $poster->getImageGeometry();
  38. $posterW['width'] = $posterWH['width'];
  39. $posterH['height'] = $posterWH['height'];
  40. // 按照缩略图大小创建一个有颜色的图片
  41. $canvas = new Imagick();
  42. $canvas->newImage( $posterW['width'], $posterH['height'], 'black', 'jpg' );
  43. //二维码 微信头像 背景 合成
  44. $poster->compositeImage($Qrcode,Imagick::COMPOSITE_OVER,275,960);
  45. $poster->compositeImage($weixin,Imagick::COMPOSITE_OVER,275,402);
  46. $canvas->compositeImage( $poster, imagick::COMPOSITE_OVER, 0, 0);
  47. $canvas->setImageCompressionQuality(60); //压缩质量
  48. $canvas->writeImage( "/upload/poster/$userId.jpg" ); //生成图片
  49. return $canvas; //返回图片流信息
  50. }
  51. header( "Content-Type: image/jpg" ); //输出图片
  52. $posterimg = $this->CompositeImage($Fticket, $Fwnick, $userId);
  53. echo $posterimg //输出图片