本文實(shí)例講述了PHP使用 Imagick 擴(kuò)展實(shí)現(xiàn)圖片合成,圓角處理功能。分享給大家供大家參考,具體如下:
需求:為用戶生成特定的二維碼 ,拉取用戶的微信頭像 和特定的背景圖合成一張用戶專屬海報(bào)
方法:采用PHP的Imagick擴(kuò)展功能對(duì)圖片進(jìn)行合成處理。對(duì)微信頭像進(jìn)行圓角處理,然后壓縮圖片的質(zhì)量
1. 根據(jù)微信用戶特定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(); //公眾號(hào)token,這個(gè)要獲取自己公眾號(hào)的
$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、 合成海報(bào)
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); //設(shè)置圖片分辨率
$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 ); //按照選定的比例進(jìn)行縮放
// 預(yù)先下載微信頭像,再生成合成信息
$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 );//等比例縮放
//創(chuàng)建一個(gè)Imagick對(duì)象,同時(shí)獲取要處理的背景圖 /data/wenda/htdocs/upload
$poster = new Imagick( "/data/wenda/htdocs/upload/poster.png" );
$posterWH = $poster->getImageGeometry();
$posterW['width'] = $posterWH['width'];
$posterH['height'] = $posterWH['height'];
// 按照縮略圖大小創(chuàng)建一個(gè)有顏色的圖片
$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); //壓縮質(zhì)量
$canvas->writeImage( "/upload/poster/$userId.jpg" ); //生成圖片
return $canvas; //返回圖片流信息
}
header( "Content-Type: image/jpg" ); //輸出圖片
$posterimg = $this->CompositeImage($Fticket, $Fwnick, $userId);
echo $posterimg //輸出圖片
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP擴(kuò)展開發(fā)教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php curl用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP圖形與圖片操作技巧匯總》及《php字符串(string)用法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- php圖片合成方法(多張圖片合成一張)
- PHP實(shí)現(xiàn)將幾張照片拼接到一起的合成圖片功能【便于整體打印輸出】
- PHP將身份證正反面兩張照片合成一張圖片的代碼
- PHP基于imagick擴(kuò)展實(shí)現(xiàn)合成圖片的兩種方法【附imagick擴(kuò)展下載】
- PHP基于php_imagick_st-Q8.dll實(shí)現(xiàn)JPG合成GIF圖片的方法
- php curl優(yōu)化下載微信頭像的方法總結(jié)
- PHP仿微信多圖片預(yù)覽上傳實(shí)例代碼
- PHP實(shí)現(xiàn)微信圖片上傳到服務(wù)器的方法示例
- 微信小程序上傳圖片到php服務(wù)器的方法
- PHP 圖片合成、仿微信群頭像的方法示例