思路:
1、請求微信接口獲取一定尺寸微信二維碼
2、准備海報主圖,處理尺寸按比例縮放
3、准備分享語錄,計算段落高度
4、生成海報:創建畫布,分寫別入按順序和位置寫入二維碼、圖片、文字等
5、保存海報
具體如下:
1、請求微信接口獲取一定尺寸微信二維碼
$access_token = 'jkagfgjkdahfgadhsfkdsj'; $url = sprintf("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s",$access_token); $postdata = '{"path": "/pages/subActivity/pages/activityDetail/activityDetail?id='.$id.'", "width": 210}'; //微信的二維碼 $image = http_post($url,$postdata);
//准備微信二維碼以備生成海報使用
$wxim = imagecreatefromstring($image);
$wxim_size = 270;
function http_post($url = '', $param = '') {
if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = $param; $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定網頁 curl_setopt($ch, CURLOPT_HEADER, 0);//設置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果為字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($ch);//運行curl curl_close($ch); return $data; }
2、准備海報主圖,處理尺寸按比例縮放
$share_img = '圖片路徑'
$share_img_size = getimagesize($share_img); //圖片尺寸
$shareim = imagecreatefromstring(file_get_contents($share_img));
//計算原圖長寬比例 $size_rate = $share_img_size[1]/$share_img_size[0];
//新圖尺寸 $share_img_width = 450; $share_img_height = intval($share_img_width*$size_rate);
//復制生成新圖並保存 $new_share_img = imagecreatetruecolor($share_img_width, $share_img_height);
imagecopyresampled($new_share_img, $shareim, 0, 0, 0, 0,$share_img_width,$share_img_height,$share_img_size[0], $share_img_size[1]); $new_share_img_filename = $dir.'/'.$id.'_new_share.jpg'; imagejpeg($new_share_img, $new_share_img_filename); imagedestroy($new_share_img);
//准備可寫入新分享主圖 以備生成海報使用 $new_shareim = imagecreatefromstring(file_get_contents($dir.'/'.$id.'_new_share.jpg')); unlink($new_share_img_filename); //刪除臨時新的分享圖
3、准備分享語錄文字,計算高度
//准備分享語錄 $content = ""; // 將字符串拆分成一個個單字 保存到數組 letter 中 for ($i=0;$i<mb_strlen($share_title);$i++) { $letter[] = mb_substr($share_title, $i, 1); } foreach ($letter as $l) { $teststr = $content." ".$l; $share_title_size = imagettfbbox(16, 0, $font, $teststr); // 判斷拼接后的字符串是否超過預設的寬度 if (($share_title_size[2] > $share_img_width) && ($content !== "")) { $content .= "\n"; } $content .= $l; }
//文字高度 $share_title_height = $share_title_size[3];
4、生成海報:創建畫布,分寫別入按順序和位置寫入二維碼、圖片、文字等
//生成海報
$font = FCPATH.'/styles/font/wryh.ttf'; //保證字體文件目錄正確
$filepath = $dir.'/'.$id.'_p.jpg'; $posters_width = 510; $posters_height = 130 + $share_img_height+$share_title_height + 280; $newimg = imagecreatetruecolor($posters_width, $posters_height); $bg = imagecolorallocate($newimg, 255, 255, 255); imagefill($newimg,0,0,$bg); $black = imagecolorallocate($newimg, 0, 0, 0); //寫宣傳言 imagettftext($newimg,18,0,30,47,$black,$font,'美好生活'); imagettftext($newimg,15,0,30,76,$black,$font,'關注美好生活了解社區好生活'); //寫分享圖片 imagecopy($newimg,$new_shareim,30,95,0,0,$share_img_width,$share_img_height); //寫分享標題 imagettftext ($newimg, 15, 0, 30, $share_img_height+122, $black, $font, $content ); //寫二維碼 $wxim_start_hight = 130+$share_title_height+$share_img_height; imagecopy($newimg,$wxim,20,$wxim_start_hight,0,0,$wxim_size,$wxim_size); $gray = imagecolorallocate($newimg, 136, 136, 136); //寫提示語 $tips = '長按保存海報至手機相冊'; $tips_hight = $wxim_start_hight+200; imagettftext($newimg,13,0,290,$tips_hight,$gray,$font,$tips); //海報生成保存 imagejpeg($newimg, $filepath); ImageDestroy($newimg); ImageDestroy($wxim);
整體代碼
$access_token = '請求微信接口的aceess_token'; if($access_token){ $url = sprintf("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s",$access_token); $postdata = '{"path": "/pages/subActivity/pages/activityDetail/activityDetail?id='.$id.'", "width": 210}'; //微信的二維碼 $image = http_post($url,$postdata); if(strlen($image) < 200){ //出錯了 log_message('error',"getTopicQRCodeProcessed, error : " . $image); //刷新token ls('wx_service'); $this->wx_service->refreshToken(); return false; } $dir = FCPATH.'upload/qrcode/'.date('Ym'); MkFolder($dir); $path = $dir.'/'.$id.'.jpg'; file_put_contents($path,$image); $qrcode = pathATOR($path); $share_title = element('share_title',$event); $share_img = element('share_img_moment',$event) ? : element('cover',$event); $share_img = get_oss_file_url($share_img); $font = FCPATH.'/styles/font/wryh.ttf'; //准備分享圖片 先處理圖片尺寸 $share_img_size = getimagesize($share_img); $shareim = imagecreatefromstring(file_get_contents($share_img)); $size_rate = $share_img_size[1]/$share_img_size[0]; $share_img_width = 450; $share_img_height = intval($share_img_width*$size_rate); $new_share_img = imagecreatetruecolor($share_img_width, $share_img_height); imagecopyresampled($new_share_img, $shareim, 0, 0, 0, 0,$share_img_width,$share_img_height,$share_img_size[0], $share_img_size[1]); $new_share_img_filename = $dir.'/'.$id.'_new_share.jpg'; imagejpeg($new_share_img, $new_share_img_filename); imagedestroy($new_share_img); $new_shareim = imagecreatefromstring(file_get_contents($dir.'/'.$id.'_new_share.jpg')); unlink($new_share_img_filename); //准備分享標題 $content = ""; // 將字符串拆分成一個個單字 保存到數組 letter 中 for ($i=0;$i<mb_strlen($share_title);$i++) { $letter[] = mb_substr($share_title, $i, 1); } foreach ($letter as $l) { $teststr = $content." ".$l; $share_title_size = imagettfbbox(16, 0, $font, $teststr); // 判斷拼接后的字符串是否超過預設的寬度 if (($share_title_size[2] > $share_img_width) && ($content !== "")) { $content .= "\n"; } $content .= $l; } $share_title_height = $share_title_size[3]; //准備微信二維碼 $wxim = imagecreatefromstring($image); $wxim_size = 270; //生成海報 $filepath = $dir.'/'.$id.'_p.jpg'; $posters_width = 510; $posters_height = 130 + $share_img_height+$share_title_height + 280; $newimg = imagecreatetruecolor($posters_width, $posters_height); $bg = imagecolorallocate($newimg, 255, 255, 255); imagefill($newimg,0,0,$bg); $black = imagecolorallocate($newimg, 0, 0, 0); //寫宣傳言 imagettftext($newimg,18,0,30,47,$black,$font,'昌品生活'); imagettftext($newimg,15,0,30,76,$black,$font,'關注昌品生活了解社區好生活'); //寫分享圖片 imagecopy($newimg,$new_shareim,30,95,0,0,$share_img_width,$share_img_height); //寫分享標題 imagettftext ($newimg, 15, 0, 30, $share_img_height+122, $black, $font, $content ); //寫二維碼 $wxim_start_hight = 130+$share_title_height+$share_img_height; imagecopy($newimg,$wxim,20,$wxim_start_hight,0,0,$wxim_size,$wxim_size); $gray = imagecolorallocate($newimg, 136, 136, 136); //寫提示語 $tips = '長按保存海報至手機相冊'; $tips_hight = $wxim_start_hight+200; imagettftext($newimg,13,0,290,$tips_hight,$gray,$font,$tips); //海報生成保存 imagejpeg($newimg, $filepath); ImageDestroy($newimg); ImageDestroy($wxim); $posters = pathATOR($filepath);