您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在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中利用Imagick擴展合成圖片,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。