您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在PHP中利用Imagick擴(kuò)展合成圖片,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
方法:采用PHP的Imagick擴(kuò)展功能對圖片進(jìn)行合成處理。對微信頭像進(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(); //公眾號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); //設(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)建一個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']; // 按照縮略圖大小創(chuàng)建一個有顏色的圖片 $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 //輸出圖片
上述內(nèi)容就是怎么在PHP中利用Imagick擴(kuò)展合成圖片,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。