首頁  >  文章  >  後端開發  >  php如何產生持透明png的圖片縮圖

php如何產生持透明png的圖片縮圖

WBOY
WBOY原創
2016-08-08 09:33:301241瀏覽

註:此功能依賴GD2圖形庫

最近要用php產生縮圖,在網路上找了一下,發現了這篇文章:PHP生成圖片縮圖

試用了一下後,發現有這樣幾個問題:

1.png圖片產生的縮圖是jpg格式的

2.png圖片產生的縮圖沒有了透明(半透明)效果(填充了黑色背景)

3.代碼語法比較老

因此,在這個版本的基礎上簡單修改優化了一下。

PHP產生縮圖類

<?<span>php </span><span>/*</span><span> * desc: Resize Image(png, jpg, gif)
     * author: 十年后的卢哥哥(http://www.cnblogs.com/lurenjiashuo/)
     * date: 2014.11.13
     * base from: http://www.oschina.net/code/snippet_5189_2491 </span><span>*/</span><span>class</span><span> ResizeImage { </span><span>//</span><span>图片类型</span><span>private</span><span>$type</span><span>; </span><span>//</span><span>实际宽度</span><span>private</span><span>$width</span><span>; </span><span>//</span><span>实际高度</span><span>private</span><span>$height</span><span>; </span><span>//</span><span>改变后的宽度</span><span>private</span><span>$resize_width</span><span>; </span><span>//</span><span>改变后的高度</span><span>private</span><span>$resize_height</span><span>; </span><span>//</span><span>是否裁图</span><span>private</span><span>$cut</span><span>; </span><span>//</span><span>源图象</span><span>private</span><span>$srcimg</span><span>; </span><span>//</span><span>目标图象地址</span><span>private</span><span>$dstimg</span><span>; </span><span>//</span><span>临时创建的图象</span><span>private</span><span>$im</span><span>; </span><span>function</span> __construct(<span>$imgPath</span>, <span>$width</span>, <span>$height</span>, <span>$isCut</span>, <span>$savePath</span><span>) { </span><span>$this</span>->srcimg = <span>$imgPath</span><span>; </span><span>$this</span>->resize_width = <span>$width</span><span>; </span><span>$this</span>->resize_height = <span>$height</span><span>; </span><span>$this</span>->cut = <span>$isCut</span><span>; </span><span>//</span><span>图片的类型</span><span>$this</span>->type = <span>strtolower</span>(<span>substr</span>(<span>strrchr</span>(<span>$this</span>->srcimg,"."),1<span>)); </span><span>//</span><span>初始化图象</span><span>$this</span>-><span>initi_img(); </span><span>//</span><span>目标图象地址</span><span>$this</span> -> dst_img(<span>$savePath</span><span>); </span><span>//</span><span>--</span><span>$this</span>->width = imagesx(<span>$this</span>-><span>im); </span><span>$this</span>->height = imagesy(<span>$this</span>-><span>im); </span><span>//</span><span>生成图象</span><span>$this</span>-><span>newimg();
            ImageDestroy (</span><span>$this</span>-><span>im);
        } </span><span>private</span><span>function</span><span> newimg() { </span><span>//</span><span>改变后的图象的比例</span><span>$resize_ratio</span> = (<span>$this</span>->resize_width)/(<span>$this</span>-><span>resize_height); </span><span>//</span><span>实际图象的比例</span><span>$ratio</span> = (<span>$this</span>->width)/(<span>$this</span>-><span>height); </span><span>if</span>(<span>$this</span>-><span>cut) { </span><span>//</span><span>裁图</span><span>$newimg</span> = imagecreatetruecolor(<span>$this</span>->resize_width,<span>$this</span>-><span>resize_height); </span><span>if</span>(<span>$this</span>->type=="png"<span>) {
                    imagefill(</span><span>$newimg</span>, 0, 0, imagecolorallocatealpha(<span>$newimg</span>, 0, 0, 0, 127<span>));
                } </span><span>if</span>(<span>$ratio</span>>=<span>$resize_ratio</span><span>) { </span><span>//</span><span>高度优先</span> imagecopyresampled(<span>$newimg</span>, <span>$this</span>->im, 0, 0, 0, 0, <span>$this</span>->resize_width,<span>$this</span>->resize_height, ((<span>$this</span>->height)*<span>$resize_ratio</span>), <span>$this</span>-><span>height);
                } </span><span>else</span><span> { </span><span>//</span><span>宽度优先</span> imagecopyresampled(<span>$newimg</span>, <span>$this</span>->im, 0, 0, 0, 0, <span>$this</span>->resize_width, <span>$this</span>->resize_height, <span>$this</span>->width, ((<span>$this</span>->width)/<span>$resize_ratio</span><span>));
                }
            } </span><span>else</span><span> { </span><span>//</span><span>不裁图</span><span>if</span>(<span>$ratio</span>>=<span>$resize_ratio</span><span>) { </span><span>$newimg</span> = imagecreatetruecolor(<span>$this</span>->resize_width,(<span>$this</span>->resize_width)/<span>$ratio</span><span>); </span><span>if</span>(<span>$this</span>->type=="png"<span>) {
                        imagefill(</span><span>$newimg</span>, 0, 0, imagecolorallocatealpha(<span>$newimg</span>, 0, 0, 0, 127<span>));
                    }
                    imagecopyresampled(</span><span>$newimg</span>, <span>$this</span>->im, 0, 0, 0, 0, <span>$this</span>->resize_width, (<span>$this</span>->resize_width)/<span>$ratio</span>, <span>$this</span>->width, <span>$this</span>-><span>height);
                } </span><span>else</span><span> { </span><span>$newimg</span> = imagecreatetruecolor((<span>$this</span>->resize_height)*<span>$ratio</span>,<span>$this</span>-><span>resize_height); </span><span>if</span>(<span>$this</span>->type=="png"<span>) {
                        imagefill(</span><span>$newimg</span>, 0, 0, imagecolorallocatealpha(<span>$newimg</span>, 0, 0, 0, 127<span>));
                    }
                    imagecopyresampled(</span><span>$newimg</span>, <span>$this</span>->im, 0, 0, 0, 0, (<span>$this</span>->resize_height)*<span>$ratio</span>, <span>$this</span>->resize_height, <span>$this</span>->width, <span>$this</span>-><span>height);
                }
            } </span><span>if</span>(<span>$this</span>->type=="png"<span>) {
                imagesavealpha(</span><span>$newimg</span>, <span>true</span><span>);
                imagepng (</span><span>$newimg</span>,<span>$this</span>-><span>dstimg);
            } </span><span>else</span><span> {
                imagejpeg (</span><span>$newimg</span>,<span>$this</span>-><span>dstimg);
            }
        } </span><span>//</span><span>初始化图象</span><span>private</span><span>function</span><span> initi_img() { </span><span>if</span>(<span>$this</span>->type=="jpg"<span>) { </span><span>$this</span>->im = imagecreatefromjpeg(<span>$this</span>-><span>srcimg);
            } </span><span>if</span>(<span>$this</span>->type=="gif"<span>) { </span><span>$this</span>->im = imagecreatefromgif(<span>$this</span>-><span>srcimg);
            } </span><span>if</span>(<span>$this</span>->type=="png"<span>) { </span><span>$this</span>->im = imagecreatefrompng(<span>$this</span>-><span>srcimg);
            }
        } </span><span>//</span><span>图象目标地址</span><span>private</span><span>function</span> dst_img(<span>$dstpath</span><span>) { </span><span>$full_length</span> = <span>strlen</span>(<span>$this</span>-><span>srcimg); </span><span>$type_length</span> = <span>strlen</span>(<span>$this</span>-><span>type); </span><span>$name_length</span> = <span>$full_length</span>-<span>$type_length</span><span>; </span><span>$name</span> = <span>substr</span>(<span>$this</span>->srcimg,0,<span>$name_length</span>-1<span>); </span><span>$this</span>->dstimg = <span>$dstpath</span><span>;
        }
    } </span>?>

使用

使用時,直接呼叫類別的建構函式即可,建構函式如下:

<span>$resizeimage</span> = <span>new</span> resizeimage($imgPath, $width, $height, $isCut, $savePath);

參數

$imgPath:原圖片地址

$width:縮圖寬

$height:縮圖高

$isCut:是否裁剪,bool值

$savePath:縮圖地址(可以跟原圖片地址相同)

範例

<?<span>php </span><span>include</span> "ResizeImage.php"<span>; </span><span>//</span><span>jpg</span><span>$jpgResize</span> = <span>new</span> ResizeImage("img/test_1920_1200.jpg", 320, 240, <span>false</span>, "img/test_320_240.jpg"<span>); </span><span>//</span><span>png</span><span>$pngResize</span> = <span>new</span> ResizeImage("img/test_1024_746.png", 320, 240, <span>false</span>, "img/test_320_240.png"<span>); </span>?>

效果

  

以上就介紹了php如何產生持透明png的圖片縮圖,包含了php產生縮圖方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP與ASP的差異下一篇:PHP與ASP的差異