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

yipeiwu_com6年前PHP代码库

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《PHP扩展开发教程》、《PHP网络编程技巧总结》、《php curl用法总结》、《PHP数组(Array)操作技巧大全》、《PHP图形与图片操作技巧汇总》及《php字符串(string)用法总结

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP下打开phpMyAdmin出现403错误的问题解决方法

PHP下打开phpMyAdmin出现403错误的问题解决方法

安装完wamp后打开其下的phpMyAdmin也就是路径http://localhost/phpmyadmin/ 出现 看里面的代码一下明白了 解决方法直接贴图如下: 复制代码 代码...

PHP安全技术之 实现php基本安全

1.不要依赖注册全局变量功能(register_globals) 注册全局变量的出现曾经让PHP变得非常易用,但也降低了安全性(方便之处经常会破坏安全性)。建议在编程时把register...

PHP中使用php5-ffmpeg撷取视频图片实例

前几天在玩 FFmpeg 的时后,突然发现 Ubuntu 上多了 php5-ffmpeg 这个扩充套件,就想来玩玩看,看好不好用,有两个结论: 读取影片取决于 FFmpeg 的支援性,如...

迅速确定php多维数组的深度的方法

例如有一个多维数组: 复制代码 代码如下: array( array( array(1,3,4), array( array( 1,2,3 ) ) ), array( array(1,2...

ubuntu下编译安装xcache for php5.3 的具体操作步骤

wget http://xcache.lighttpd.net/pub/Releases/1.3.0/xcache-1.3.0.tar.gzsudo tar -xzvf  xc...