Rumah > Artikel > pembangunan bahagian belakang > php 打包gd 库
php 封装gd 库
使用sae 版 thinkphp 在本地和sae 上无法实现缩略图,缩略图主要使用在瀑布流上面,不然一张图片2 3 M,速度太慢,就自己封装了一个,见笑!
<code class=" hljs xml"><span class="php"><span class="hljs-preprocessor"><?php</span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Thumb</span>{</span> <span class="hljs-comment">// 设置最大宽度,用来在编辑器中使用和显示</span> <span class="hljs-keyword">private</span> <span class="hljs-variable">$max_width</span> = <span class="hljs-keyword">null</span>; <span class="hljs-keyword">private</span> <span class="hljs-variable">$file_name</span> = <span class="hljs-keyword">null</span>; <span class="hljs-keyword">private</span> <span class="hljs-variable">$water_name</span> = <span class="hljs-keyword">null</span>; <span class="hljs-comment">//获得文件名和图片宽度</span> <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">(<span class="hljs-variable">$max_widht</span>,<span class="hljs-variable">$file_name</span>,<span class="hljs-variable">$water_name</span>)</span> {</span> <span class="hljs-variable">$this</span>->max_width = <span class="hljs-variable">$max_widht</span>; <span class="hljs-variable">$this</span>->file_name = <span class="hljs-variable">$file_name</span>; <span class="hljs-variable">$this</span>->water_name = <span class="hljs-variable">$water_name</span>; } <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">create_image</span><span class="hljs-params">()</span>{</span> <span class="hljs-comment">// 获得ori图片信息</span> <span class="hljs-keyword">list</span>(<span class="hljs-variable">$width</span>,<span class="hljs-variable">$height</span>,<span class="hljs-variable">$type</span>) = getimagesize(<span class="hljs-variable">$this</span>->file_name); <span class="hljs-comment">// 当原有图片大于 要求的最大宽度时,才需要进行压缩</span> <span class="hljs-keyword">if</span>(<span class="hljs-variable">$width</span> > <span class="hljs-variable">$this</span>->max_width){ <span class="hljs-comment">// 获得图片压缩百分比</span> <span class="hljs-variable">$per</span> = <span class="hljs-variable">$this</span>->max_width / <span class="hljs-variable">$width</span>; <span class="hljs-variable">$new_width</span> = <span class="hljs-variable">$width</span> * <span class="hljs-variable">$per</span>; <span class="hljs-variable">$new_height</span> = <span class="hljs-variable">$height</span> * <span class="hljs-variable">$per</span>; }<span class="hljs-keyword">else</span>{ <span class="hljs-variable">$new_height</span> = <span class="hljs-variable">$height</span>; <span class="hljs-variable">$new_width</span> = <span class="hljs-variable">$width</span>; } <span class="hljs-comment">//创建一个真彩色图像</span> <span class="hljs-variable">$image_p</span> = imagecreatetruecolor(<span class="hljs-variable">$new_width</span>, <span class="hljs-variable">$new_height</span> -<span class="hljs-number">10</span>); <span class="hljs-variable">$image</span> = <span class="hljs-variable">$this</span>->image_obj(<span class="hljs-variable">$type</span>, <span class="hljs-variable">$this</span>->file_name); imagecopyresampled(<span class="hljs-variable">$image_p</span>, <span class="hljs-variable">$image</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-variable">$new_width</span>, <span class="hljs-variable">$new_height</span>, <span class="hljs-variable">$width</span>, <span class="hljs-variable">$height</span>); <span class="hljs-variable">$this</span>->image_dump(<span class="hljs-variable">$type</span>, <span class="hljs-variable">$image_p</span>, <span class="hljs-variable">$this</span>->file_name); <span class="hljs-variable">$this</span>->water(); } <span class="hljs-comment">/* * 生成为图片添加水印 */</span> <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">water</span><span class="hljs-params">()</span>{</span> <span class="hljs-variable">$water_name</span> = <span class="hljs-variable">$this</span>->water_name; <span class="hljs-variable">$dist_name</span> = <span class="hljs-variable">$this</span>->file_name; <span class="hljs-keyword">list</span>(<span class="hljs-variable">$dist_width</span>,<span class="hljs-variable">$dist_height</span>,<span class="hljs-variable">$type</span>) = getimagesize(<span class="hljs-variable">$dist_name</span>); <span class="hljs-variable">$dist_im</span> = <span class="hljs-variable">$this</span>->image_obj(<span class="hljs-variable">$type</span>, <span class="hljs-variable">$this</span>->file_name); <span class="hljs-variable">$water_name</span> = <span class="hljs-string">"D:/xampps/htdocs/buyingfeiblog/1/App/Modules/Admin/Tpl/Public/Images/water.png"</span>; <span class="hljs-keyword">list</span>(<span class="hljs-variable">$w_width</span>,<span class="hljs-variable">$w_height</span>) = getimagesize(<span class="hljs-variable">$water_name</span>); <span class="hljs-comment">// 获得图片水印信息</span> <span class="hljs-variable">$water_src</span> = imagecreatefrompng(<span class="hljs-variable">$water_name</span>); <span class="hljs-comment">// 设置图片水印位置 在右下角</span> <span class="hljs-variable">$x</span> = (<span class="hljs-variable">$dist_width</span> - <span class="hljs-variable">$w_width</span>) / <span class="hljs-number">4</span> * <span class="hljs-number">3</span> ; <span class="hljs-variable">$y</span> =(<span class="hljs-variable">$dist_height</span> - <span class="hljs-variable">$w_height</span>) /<span class="hljs-number">4</span> * <span class="hljs-number">3</span> ; <span class="hljs-keyword">if</span>(imagecopy(<span class="hljs-variable">$dist_im</span>, <span class="hljs-variable">$water_src</span>, <span class="hljs-variable">$x</span>, <span class="hljs-variable">$y</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-variable">$w_width</span>, <span class="hljs-variable">$w_height</span>)){ imagepng(<span class="hljs-variable">$dist_im</span>,<span class="hljs-variable">$dist_name</span>); <span class="hljs-keyword">echo</span> <span class="hljs-string">"success"</span>; }<span class="hljs-keyword">else</span>{ <span class="hljs-keyword">echo</span> <span class="hljs-string">"error"</span>; } } <span class="hljs-comment">// 生成图片类型,生成不同图片 保持图片原本类型不发生变化</span> <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">image_dump</span><span class="hljs-params">(<span class="hljs-variable">$type</span>,<span class="hljs-variable">$image_p</span>,<span class="hljs-variable">$filename</span>)</span>{</span> <span class="hljs-keyword">switch</span> (<span class="hljs-variable">$type</span>){ <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>: imagegif(<span class="hljs-variable">$image_p</span>, <span class="hljs-variable">$filename</span>); <span class="hljs-variable">$dis_im</span> = imagecreatefromgif(file_name); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>: imagejpeg(<span class="hljs-variable">$image_p</span>, <span class="hljs-variable">$this</span>->file_name); <span class="hljs-variable">$dis_im</span> = imagecreatefromjpeg(file_name); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>: imagepng(<span class="hljs-variable">$image_p</span>,file_name); <span class="hljs-variable">$dis_im</span> = imagecreatefrompng(file_name); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">default</span> : } } <span class="hljs-comment">// 根据图片不同,生成不同资源对象</span> <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">image_obj</span><span class="hljs-params">(<span class="hljs-variable">$type</span>,<span class="hljs-variable">$filename</span>)</span>{</span> <span class="hljs-keyword">switch</span> (<span class="hljs-variable">$type</span>){<span class="hljs-comment">// 1 = GIF,2 = JPG,3 = PNG,</span> <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>: <span class="hljs-variable">$image</span> = imagecreatefromgif(<span class="hljs-variable">$filename</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>: <span class="hljs-variable">$image</span> = imagecreatefromjpeg(<span class="hljs-variable">$filename</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>: <span class="hljs-variable">$image</span> = imagecreatefrompng(<span class="hljs-variable">$filename</span>); <span class="hljs-keyword">break</span>; <span class="hljs-keyword">default</span> : } <span class="hljs-keyword">return</span> <span class="hljs-variable">$image</span>; }} <span class="hljs-variable">$thumb</span> = <span class="hljs-keyword">new</span> Thumb(<span class="hljs-number">725</span>,<span class="hljs-string">"D:/xampps/htdocs/test/test.jpg"</span>); <span class="hljs-variable">$thumb</span>->create_image();<span class="hljs-comment">//create_image</span><span class="hljs-preprocessor">?></span></span></code>
就是这么简单,
主要包括生成真彩图,
创建图形对象资源
图片进行合并,ok!