如何使用PHP生成圖片


 79 /**
 80  * 從圖片文件創建Image資源
 81  * @param $file 圖片文件,支持url
 82  * @return bool|resource    成功返回圖片image資源,失敗返回false
 83  */
 84 function createImageFromFile($file){
 85     if(preg_match('/http(s)?:\/\//',$file)){
 86         $fileSuffix = getNetworkImgType($file);
 87     }else{
 88         $fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
 89     }
 90  
 91     if(!$fileSuffix) return false;
 92  
 93     switch ($fileSuffix){
 94         case 'jpeg':
 95             $theImage = @imagecreatefromjpeg($file);
 96             break;
 97         case 'jpg':
 98             $theImage = @imagecreatefromjpeg($file);
 99             break;
100         case 'png':
101             $theImage = @imagecreatefrompng($file);
102             break;
103         case 'gif':
104             $theImage = @imagecreatefromgif($file);
105             break;
106         default:
107             $theImage = @imagecreatefromstring(file_get_contents($file));
108             break;
109     }
110  
111     return $theImage;
112 }
113  
114 /**
115  * 獲取網絡圖片類型
116  * @param $url  網絡圖片url,支持不帶后綴名url
117  * @return bool
118  */
119 function getNetworkImgType($url){
120     $ch = curl_init(); //初始化curl
121     curl_setopt($ch, CURLOPT_URL, $url); //設置需要獲取的URL
122     curl_setopt($ch, CURLOPT_NOBODY, 1);
123     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//設置超時
124     curl_setopt($ch, CURLOPT_TIMEOUT, 3);
125     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https
126     curl_exec($ch);//執行curl會話
127     $http_code = curl_getinfo($ch);//獲取curl連接資源句柄信息
128     curl_close($ch);//關閉資源連接
129  
130     if ($http_code['http_code'] == 200) {
131         $theImgType = explode('/',$http_code['content_type']);
132  
133         if($theImgType[0] == 'image'){
134             return $theImgType[1];
135         }else{
136             return false;
137         }
138     }else{
139         return false;
140     }
141 }
142  
143 /**
144  * 分行連續截取字符串
145  * @param $str  需要截取的字符串,UTF-8
146  * @param int $row  截取的行數
147  * @param int $number   每行截取的字數,中文長度
148  * @param bool $suffix  最后行是否添加‘...’后綴
149  * @return array    返回數組共$row個元素,下標1到$row
150  */
151 function cn_row_substr($str,$row = 1,$number = 10,$suffix = true){
152     $result = array();
153     for ($r=1;$r<=$row;$r++){
154         $result[$r] = '';
155     }
156  
157     $str = trim($str);
158     if(!$str) return $result;
159  
160     $theStrlen = strlen($str);
161  
162     //每行實際字節長度
163     $oneRowNum = $number * 3;
164     for($r=1;$r<=$row;$r++){
165         if($r == $row and $theStrlen > $r * $oneRowNum and $suffix){
166             $result[$r] = mg_cn_substr($str,$oneRowNum-6,($r-1)* $oneRowNum).'...';
167         }else{
168             $result[$r] = mg_cn_substr($str,$oneRowNum,($r-1)* $oneRowNum);
169         }
170         if($theStrlen < $r * $oneRowNum) break;
171     }
172  
173     return $result;
174 }
175  
176 /**
177  * 按字節截取utf-8字符串
178  * 識別漢字全角符號,全角中文3個字節,半角英文1個字節
179  * @param $str  需要切取的字符串
180  * @param $len  截取長度[字節]
181  * @param int $start    截取開始位置,默認0
182  * @return string
183  */
184 function mg_cn_substr($str,$len,$start = 0){
185     $q_str = '';
186     $q_strlen = ($start + $len)>strlen($str) ? strlen($str) : ($start + $len);
187  
188     //如果start不為起始位置,若起始位置為亂碼就按照UTF-8編碼獲取新start
189     if($start and json_encode(substr($str,$start,1)) === false){
190         for($a=0;$a<3;$a++){
191             $new_start = $start + $a;
192             $m_str = substr($str,$new_start,3);
193             if(json_encode($m_str) !== false) {
194                 $start = $new_start;
195                 break;
196             }
197         }
198     }
199  
200     //切取內容
201     for($i=$start;$i<$q_strlen;$i++){
202         //ord()函數取得substr()的第一個字符的ASCII碼,如果大於0xa0的話則是中文字符
203         if(ord(substr($str,$i,1))>0xa0){
204             $q_str .= substr($str,$i,3);
205             $i+=2;
206         }else{
207             $q_str .= substr($str,$i,1);
208         }
209     }
210     return $q_str;
211 }

接下來准備生成所需的背景圖片,圖片元素,以及字體庫等

 1 /**
 2  * 分享圖片生成
 3  * @param $gData  商品數據,array
 4  * @param $codeName 二維碼圖片
 5  * @param $fileName string 保存文件名,默認空則直接輸入圖片
 6  */
 7 function createSharePng($gData,$codeName,$fileName = ''){
 8    //載入圖片路徑
 9    //$bg_img   = createImageFromFile($gData['bg_image']);
10 
11    //獲取圖片大小
12    $img_info = getimagesize($gData['bg_image']);
13    $img_w    = $img_info[0];
14    $img_h    = $img_info[1];
15    $im       = @imagecreatetruecolor($img_w, $img_h);
16 
17    imagecopy($im, $bg_img,0,0,0,0, $img_w, $img_h);
18    imagedestroy($bg_img);
19 
20    //第二種,創建畫布,創建圖片元素等
21     //創建畫布
22     $im = imagecreatetruecolor(618, 1000);
23  
24     //填充畫布背景色
25     $color = imagecolorallocate($im, 255, 255, 255);
26     imagefill($im, 0, 0, $color);
27  
28     //字體文件
29     $font_file = "code_png/msyh.ttf";
30     $font_file_bold = "code_png/msyh_bold.ttf";
31  
32     //設定字體的顏色
33     $font_color_1 = ImageColorAllocate ($im, 140, 140, 140);
34     $font_color_2 = ImageColorAllocate ($im, 28, 28, 28);
35     $font_color_3 = ImageColorAllocate ($im, 129, 129, 129);
36     $font_color_red = ImageColorAllocate ($im, 217, 45, 32);
37  
38     $fang_bg_color = ImageColorAllocate ($im, 254, 216, 217);
39  
40     //Logo
41     list($l_w,$l_h) = getimagesize('code_png/logo100_100.png');
42     $logoImg = @imagecreatefrompng('code_png/logo100_100.png');
43     imagecopyresized($im, $logoImg, 274, 28, 0, 0, 70, 70, $l_w, $l_h);
44  
45     //溫馨提示
46     imagettftext($im, 14,0, 100, 130, $font_color_1 ,$font_file, '溫馨提示:喜歡長按圖片識別二維碼即可前往購買');
47  
48     //商品圖片
49     list($g_w,$g_h) = getimagesize($gData['pic']);
50     $goodImg = createImageFromFile($gData['pic']);
51     imagecopyresized($im, $goodImg, 0, 185, 0, 0, 618, 618, $g_w, $g_h);
52  
53     //二維碼
54     list($code_w,$code_h) = getimagesize($codeName);
55     $codeImg = createImageFromFile($codeName);
56     imagecopyresized($im, $codeImg, 440, 820, 0, 0, 170, 170, $code_w, $code_h);
57  
58     //商品描述
59     $theTitle = cn_row_substr($gData['title'],2,19);
60     imagettftext($im, 14,0, 8, 845, $font_color_2 ,$font_file, $theTitle[1]);
61     imagettftext($im, 14,0, 8, 875, $font_color_2 ,$font_file, $theTitle[2]);
62  
63     imagettftext($im, 14,0, 8, 935, $font_color_2 ,$font_file, "券后價¥");
64     imagettftext($im, 28,0, 80, 935, $font_color_red ,$font_file_bold, $gData["price"]);
65     imagettftext($im, 14,0, 8,970, $font_color_3 ,$font_file, "現價¥".$gData["original_price"]);
66  
67     //優惠券
68     if($gData['coupon_price']){
69         imagerectangle ($im, 125 , 950 , 160 , 975 , $font_color_3);
70         imagefilledrectangle ($im, 126 , 951 , 159 , 974 , $fang_bg_color);
71         imagettftext($im, 14,0, 135,970, $font_color_3 ,$font_file, "券");
72  
73         $coupon_price = strval($gData['coupon_price']);
74         imagerectangle ($im, 160 , 950 , 198 + (strlen($coupon_price)* 10), 975 , $font_color_3);
75         imagettftext($im, 14,0, 170,970, $font_color_3 ,$font_file, $coupon_price."元");
76     }
77  
78     //輸出圖片
79     if($fileName){
80         imagepng ($im,$fileName);
81     }else{
82         Header("Content-Type: image/png");
83         imagepng ($im);
84     }
85  
86     //釋放空間
87     imagedestroy($im);
88     imagedestroy($goodImg);
89     imagedestroy($codeImg);
90 }

使用方法:

1 //直接輸出
2 createSharePng($gData,'code_png/php_code.jpg');
3 //輸出到圖片
4 createSharePng($gData,'code_png/php_code.jpg','share.png');

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM