最近工作需求需要生成分享圖片,最初用j的hml2cava截圖插件各種問(wèn)題,后來(lái)干脆PHP的PG庫(kù)在后臺(tái)生成圖片,很愉快的解決了各種問(wèn)題,我們要實(shí)現(xiàn)的效果如下圖:假設(shè)代碼中用到的資源文件夾在當(dāng)前code_pg目錄下:ph......
以下是【金聰采編】分享的內(nèi)容全文:
以下是【金聰采編】分享的內(nèi)容全文:
最近工作需求需要生成分享圖片,最初用js的html2canvas截圖插件各種問(wèn)題,后來(lái)干脆PHP的PG庫(kù)在后臺(tái)生成圖片,很愉快的解決了各種問(wèn)題,我們要實(shí)現(xiàn)的效果如下圖:

假設(shè)代碼中用到的資源文件夾在當(dāng)前code_png目錄下:
php代碼:
/** * 分享圖片生成 * @param $gData 商品數(shù)據(jù),array * @param $codeName 二維碼圖片 * @param $fileName string 保存文件名,默認(rèn)空則直接輸入圖片 */ function createSharePng($gData,$codeName,$fileName = ''){ //創(chuàng)建畫(huà)布 $im = imagecreatetruecolor(618, 1000); //填充畫(huà)布背景色 $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"; //設(shè)定字體的顏色 $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); //Logo list($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, '溫馨提示:喜歡長(zhǎng)按圖片識(shí)別二維碼即可前往購(gòu)買'); //商品圖片 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, "券后價(jià)¥"); 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, "現(xiàn)價(jià)¥".$gData["original_price"]); //優(yōu)惠券 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); } /** * 從圖片文件創(chuàng)建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; } /** * 獲取網(wǎng)絡(luò)圖片類型 * @param $url 網(wǎng)絡(luò)圖片url,支持不帶后綴名url * @return bool */ function getNetworkImgType($url){ $ch = curl_init(); //初始化curl curl_setopt($ch, CURLOPT_URL, $url); //設(shè)置需要獲取的URL curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//設(shè)置超時(shí) curl_setopt($ch, CURLOPT_TIMEOUT, 3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https curl_exec($ch);//執(zhí)行curl會(huì)話 $http_code = curl_getinfo($ch);//獲取curl連接資源句柄信息 curl_close($ch);//關(guān)閉資源連接 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; } } /** * 分行連續(xù)截取字符串 * @param $str 需要截取的字符串,UTF-8 * @param int $row 截取的行數(shù) * @param int $number 每行截取的字?jǐn)?shù),中文長(zhǎng)度 * @param bool $suffix 最后行是否添加‘...'后綴 * @return array 返回?cái)?shù)組共$row個(gè)元素,下標(biāo)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); //每行實(shí)際字節(jié)長(zhǎng)度 $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; } /** * 按字節(jié)截取utf-8字符串 * 識(shí)別漢字全角符號(hào),全角中文3個(gè)字節(jié),半角英文1個(gè)字節(jié) * @param $str 需要切取的字符串 * @param $len 截取長(zhǎng)度[字節(jié)] * @param int $start 截取開(kāi)始位置,默認(rèn)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編碼獲取新start if($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; } } } //切取內(nèi)容 for($i=$start;$i<$q_strlen;$i++){ //ord()函數(shù)取得substr()的第一個(gè)字符的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; } //使用方法------------------------------------------------- //數(shù)據(jù)格式,如沒(méi)有優(yōu)惠券coupon_price值為0。 $gData = [ 'pic' => 'code_png/nv_img.jpg', 'title' =>'chic韓版工裝羽絨棉服女冬中長(zhǎng)款2017新款棉襖大毛領(lǐng)收腰棉衣外套', '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'); 以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
金聰線報(bào)提示:[ PHP分享圖片的生成方法 ] 僅為會(huì)員分享,分享目的如下:
1.軟件源碼推廣展示:目的展示軟件相關(guān)功能,接收技術(shù)學(xué)習(xí)者測(cè)試、測(cè)評(píng);
2.教程課程信息展示:展示課程信息,傳授課程各階段內(nèi)容;
3.設(shè)計(jì)素材圖片展示:展示素材設(shè)計(jì)理念、思維方式、傳播設(shè)計(jì)理念;
4.福利優(yōu)惠信息展示:分享各類最新的福利信息,各種優(yōu)惠信息展示;
以上分享目的僅供學(xué)習(xí)、參考使用,請(qǐng)勿用于其他用途,如果想商業(yè)使用或者代理,請(qǐng)自行聯(lián)系版權(quán)方獲取授權(quán)。任何未獲取授權(quán)的商業(yè)使用與本站無(wú)關(guān),請(qǐng)自行承擔(dān)相應(yīng)責(zé)任。
本站不存儲(chǔ)任何資源文件,敬請(qǐng)周知!
本網(wǎng)站采用 BY-NC-SA 協(xié)議進(jìn)行授權(quán) 轉(zhuǎn)載請(qǐng)注明原文鏈接:PHP分享圖片的生成方法
1.軟件源碼推廣展示:目的展示軟件相關(guān)功能,接收技術(shù)學(xué)習(xí)者測(cè)試、測(cè)評(píng);
2.教程課程信息展示:展示課程信息,傳授課程各階段內(nèi)容;
3.設(shè)計(jì)素材圖片展示:展示素材設(shè)計(jì)理念、思維方式、傳播設(shè)計(jì)理念;
4.福利優(yōu)惠信息展示:分享各類最新的福利信息,各種優(yōu)惠信息展示;
以上分享目的僅供學(xué)習(xí)、參考使用,請(qǐng)勿用于其他用途,如果想商業(yè)使用或者代理,請(qǐng)自行聯(lián)系版權(quán)方獲取授權(quán)。任何未獲取授權(quán)的商業(yè)使用與本站無(wú)關(guān),請(qǐng)自行承擔(dān)相應(yīng)責(zé)任。
本站不存儲(chǔ)任何資源文件,敬請(qǐng)周知!
此資源僅供個(gè)人學(xué)習(xí)、研究使用,禁止非法轉(zhuǎn)播或商業(yè)用途,請(qǐng)?jiān)讷@取后24小時(shí)內(nèi)刪除,如果你覺(jué)得滿意,請(qǐng)尋求購(gòu)買正版或獲取授權(quán)!
如果您認(rèn)為本頁(yè)信息內(nèi)容侵犯了您的相關(guān)權(quán)益(包含但不限于:著作權(quán)、首發(fā)權(quán)、隱私權(quán)等權(quán)利),或者您認(rèn)為自己是此信息的權(quán)利人但是此信息不是自己發(fā)布的,可以直接版權(quán)舉報(bào)投訴,我們會(huì)根據(jù)網(wǎng)站注冊(cè)協(xié)議、資源分享協(xié)議等協(xié)議處理,以保護(hù)您的合法權(quán)益。
免責(zé)申明:本站僅提供學(xué)習(xí)的平臺(tái),所有資料均來(lái)自于網(wǎng)絡(luò)分享線索,版權(quán)歸原創(chuàng)者所有!本站不提供任何保證,并不承擔(dān)任何法律責(zé)任,如果對(duì)您的版權(quán)或者利益造成損害,請(qǐng)?zhí)峁┫鄳?yīng)的資質(zhì)證明,我們將于3個(gè)工作日內(nèi)予以處理。版權(quán)申訴相關(guān)說(shuō)明如果您認(rèn)為本頁(yè)信息內(nèi)容侵犯了您的相關(guān)權(quán)益(包含但不限于:著作權(quán)、首發(fā)權(quán)、隱私權(quán)等權(quán)利),或者您認(rèn)為自己是此信息的權(quán)利人但是此信息不是自己發(fā)布的,可以直接版權(quán)舉報(bào)投訴,我們會(huì)根據(jù)網(wǎng)站注冊(cè)協(xié)議、資源分享協(xié)議等協(xié)議處理,以保護(hù)您的合法權(quán)益。
本網(wǎng)站采用 BY-NC-SA 協(xié)議進(jìn)行授權(quán) 轉(zhuǎn)載請(qǐng)注明原文鏈接:PHP分享圖片的生成方法

侵權(quán)舉報(bào)/版權(quán)申訴



