使用PHP生成分享图片
生活随笔
收集整理的這篇文章主要介紹了
使用PHP生成分享图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?小程序導航 wq.xmaht.top
假設代碼中用到的資源文件夾在當前code_png目錄下:/*** 分享圖片生成* @param $gData 商品數據,array* @param $codeName 二維碼圖片* @param $fileName string 保存文件名,默認空則直接輸入圖片*/ function createSharePng($gData,$codeName,$fileName = ''){//創建畫布$im = imagecreatetruecolor(618, 1000);//填充畫布背景色$color = imagecolorallocate($im, 255, 255, 255);imagefill($im, 0, 0, $color);//字體文件$font_file = "code_png/msyh.ttf";$font_file_bold = "code_png/msyh_bold.ttf";//設定字體的顏色$font_color_1 = ImageColorAllocate ($im, 140, 140, 140);$font_color_2 = ImageColorAllocate ($im, 28, 28, 28);$font_color_3 = ImageColorAllocate ($im, 129, 129, 129);$font_color_red = ImageColorAllocate ($im, 217, 45, 32);$fang_bg_color = ImageColorAllocate ($im, 254, 216, 217);//Logolist($l_w,$l_h) = getimagesize('code_png/logo100_100.png');$logoImg = @imagecreatefrompng('code_png/logo100_100.png');imagecopyresized($im, $logoImg, 274, 28, 0, 0, 70, 70, $l_w, $l_h);//溫馨提示imagettftext($im, 14,0, 100, 130, $font_color_1 ,$font_file, '溫馨提示:喜歡長按圖片識別二維碼即可前往購買');//商品圖片list($g_w,$g_h) = getimagesize($gData['pic']);$goodImg = createImageFromFile($gData['pic']);imagecopyresized($im, $goodImg, 0, 185, 0, 0, 618, 618, $g_w, $g_h);//二維碼list($code_w,$code_h) = getimagesize($codeName);$codeImg = createImageFromFile($codeName);imagecopyresized($im, $codeImg, 440, 820, 0, 0, 170, 170, $code_w, $code_h);//商品描述$theTitle = cn_row_substr($gData['title'],2,19);imagettftext($im, 14,0, 8, 845, $font_color_2 ,$font_file, $theTitle[1]);imagettftext($im, 14,0, 8, 875, $font_color_2 ,$font_file, $theTitle[2]);imagettftext($im, 14,0, 8, 935, $font_color_2 ,$font_file, "券后價¥");imagettftext($im, 28,0, 80, 935, $font_color_red ,$font_file_bold, $gData["price"]);imagettftext($im, 14,0, 8,970, $font_color_3 ,$font_file, "現價¥".$gData["original_price"]);//優惠券if($gData['coupon_price']){imagerectangle ($im, 125 , 950 , 160 , 975 , $font_color_3);imagefilledrectangle ($im, 126 , 951 , 159 , 974 , $fang_bg_color);imagettftext($im, 14,0, 135,970, $font_color_3 ,$font_file, "券");$coupon_price = strval($gData['coupon_price']);imagerectangle ($im, 160 , 950 , 198 + (strlen($coupon_price)* 10), 975 , $font_color_3);imagettftext($im, 14,0, 170,970, $font_color_3 ,$font_file, $coupon_price."元");}//輸出圖片if($fileName){imagepng ($im,$fileName);}else{Header("Content-Type: image/png");imagepng ($im);}//釋放空間imagedestroy($im);imagedestroy($goodImg);imagedestroy($codeImg); }/*** 從圖片文件創建Image資源* @param $file 圖片文件,支持url* @return bool|resource 成功返回圖片image資源,失敗返回false*/ function createImageFromFile($file){if(preg_match('/http(s)?:\/\//',$file)){$fileSuffix = getNetworkImgType($file);}else{$fileSuffix = pathinfo($file, PATHINFO_EXTENSION);}if(!$fileSuffix) return false;switch ($fileSuffix){case 'jpeg':$theImage = @imagecreatefromjpeg($file);break;case 'jpg':$theImage = @imagecreatefromjpeg($file);break;case 'png':$theImage = @imagecreatefrompng($file);break;case 'gif':$theImage = @imagecreatefromgif($file);break;default:$theImage = @imagecreatefromstring(file_get_contents($file));break;}return $theImage; }/*** 獲取網絡圖片類型* @param $url 網絡圖片url,支持不帶后綴名url* @return bool*/ function getNetworkImgType($url){$ch = curl_init(); //初始化curlcurl_setopt($ch, CURLOPT_URL, $url); //設置需要獲取的URLcurl_setopt($ch, CURLOPT_NOBODY, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//設置超時curl_setopt($ch, CURLOPT_TIMEOUT, 3);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持httpscurl_exec($ch);//執行curl會話$http_code = curl_getinfo($ch);//獲取curl連接資源句柄信息curl_close($ch);//關閉資源連接if ($http_code['http_code'] == 200) {$theImgType = explode('/',$http_code['content_type']);if($theImgType[0] == 'image'){return $theImgType[1];}else{return false;}}else{return false;} }/*** 分行連續截取字符串* @param $str 需要截取的字符串,UTF-8* @param int $row 截取的行數* @param int $number 每行截取的字數,中文長度* @param bool $suffix 最后行是否添加‘...’后綴* @return array 返回數組共$row個元素,下標1到$row*/ function cn_row_substr($str,$row = 1,$number = 10,$suffix = true){$result = array();for ($r=1;$r<=$row;$r++){$result[$r] = '';}$str = trim($str);if(!$str) return $result;$theStrlen = strlen($str);//每行實際字節長度$oneRowNum = $number * 3;for($r=1;$r<=$row;$r++){if($r == $row and $theStrlen > $r * $oneRowNum and $suffix){$result[$r] = mg_cn_substr($str,$oneRowNum-6,($r-1)* $oneRowNum).'...';}else{$result[$r] = mg_cn_substr($str,$oneRowNum,($r-1)* $oneRowNum);}if($theStrlen < $r * $oneRowNum) break;}return $result; }/*** 按字節截取utf-8字符串* 識別漢字全角符號,全角中文3個字節,半角英文1個字節* @param $str 需要切取的字符串* @param $len 截取長度[字節]* @param int $start 截取開始位置,默認0* @return string*/ function mg_cn_substr($str,$len,$start = 0){$q_str = '';$q_strlen = ($start + $len)>strlen($str) ? strlen($str) : ($start + $len);//如果start不為起始位置,若起始位置為亂碼就按照UTF-8編碼獲取新startif($start and json_encode(substr($str,$start,1)) === false){for($a=0;$a<3;$a++){$new_start = $start + $a;$m_str = substr($str,$new_start,3);if(json_encode($m_str) !== false) {$start = $new_start;break;}}}//切取內容for($i=$start;$i<$q_strlen;$i++){//ord()函數取得substr()的第一個字符的ASCII碼,如果大于0xa0的話則是中文字符if(ord(substr($str,$i,1))>0xa0){$q_str .= substr($str,$i,3);$i+=2;}else{$q_str .= substr($str,$i,1);}}return $q_str; }//使用方法------------------------------------------------- //數據格式,如沒有優惠券coupon_price值為0。 $gData = ['pic' => 'code_png/nv_img.jpg','title' =>'chic韓版工裝羽絨棉服女冬中長款2017新款棉襖大毛領收腰棉衣外套','price' => 19.8,'original_price' => 119.8,'coupon_price' => 100 ]; //直接輸出 createSharePng($gData,'code_png/php_code.jpg'); //輸出到圖片 createSharePng($gData,'code_png/php_code.jpg','share.png');?
轉載于:https://www.cnblogs.com/mverting/p/9317984.html
總結
以上是生活随笔為你收集整理的使用PHP生成分享图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GeoServer之styles定制
- 下一篇: Codeforces Round #26