如何使用PHP生成图片

摘要:
$str)返回$result;159160$strlen=strlen;161162//每行的实际字节长度为163$oneRowNumber=$number*3;164对于{165if{166$result[$r]=mg_cn_substr。'…';167}否则{168$result[$sr]=mg_cn_substra;169}170ifbreak;171}172173返回$result;174}175176/**177_ cn_ Substr{185$q_str=“”;186$q_strlen=˃strlen?==false){194$start=$new_start;195break;196}197}198}199200//剪切{202//ord()函数的内容201以获取Substr()的第一个字符的ASCII代码。如果它大于0xa0,则如果{204$q_str.=Substr;205$i+=2;206}否则{207$q_str.=Substr,208}209}210返回$q_ str,则它是一个汉字203;211}接下来,准备生成所需的背景图片、图片元素、字体库等。1/**2*共享图片以生成3*@param$gData产品数据,array4*@param$codeName二维码图片5*@param$fileNamestr
 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 }

接下来准备生成所需的背景图片,图片元素,以及字体库等

如何使用PHP生成图片第1张

 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');

免责声明:文章转载自《如何使用PHP生成图片》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Redis有效时间设置及时间过期处理阿里云ECS专有网络产品三个步骤配置教程下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

给source insight添加.cc的C++文件后缀识别

Options -> Document Options, Document Type 下拉选 C++ Source File, File Filter 中加入,*.cc 为Source Insight添加语言支持 1. 下载语言支持文件。下载地址http://www.sourceinsight.com/public/languages/2. 选择 O...

网页ASP函数大全(摘选)

<%'***********************************'功能描述:将日期转换成指定的显示格式'入口参数: 'dtDateValue 想显示的日期 'iDateFormat 日期显示的方式  'iDataFormat=0 2000-10-10 下午 12:17:45 'iDataFormat=1 2000-10-10 23:17:...

ElasticSearch的基本原理与用法

一、简介 ElasticSearch和Solr都是基于Lucene的搜索引擎,不过ElasticSearch天生支持分布式,而Solr是4.0版本后的SolrCloud才是分布式版本,Solr的分布式支持需要ZooKeeper的支持。 这里有一个详细的ElasticSearch和Solr的对比:http://solr-vs-elasticsearch.co...

六、对ES增删改查

一、ES的数据结构 ES跟MySQL有很大的区别,现在将MySQL跟ES做对比,这样方便理解。 因为ES数据库是通过API接口进行访问的,访问的方式有如下几种 1、es-head插件 2、kibana控制台 3、curl命令 4、Postman工具 1、ES数据库结构对比 MySQL术语 ES术语 库,databases 索引,index 表...

PHP站内搜索:多关键字、加亮显示

一、SQL语句中的模糊查找       主要通过LIKE(不区分大小写)关键字实现模糊查找。LIKE条件一般用在指定搜索某字段的时候, 通过"%"或者" _" 通配符的作用实现模糊查找功能,通配符可以在字段前面也可以在后面或前后都有。只通过LIKE是无法实现模糊查找的,因此通配符的作用不可忽略。下面是三个实例:搜索以PHP开头:SELECT * FROM ...

itextPDF使用笔记

最近在做报表打印,使用的是itextPDF,第一次用到它,简单做个笔记。主要涉及到字体设置,文字处理操作,表格及单元格设置,绘制一些图形 IText中有三个处理text的类com.lowagie.text.Chunk,com.lowagie.text.Phrase,com.lowagie.text.Paragraph。 官网的API:https://api...