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

2022-04-14 0 1,359

本文实例讲述了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 //输出图片

更多关于PHP相关内容感兴趣的读者可查看本站专题

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 PHP编程 PHP使用 Imagick 扩展实现图片合成,圆角处理功能示例 https://www.niceym.com/12492.html