支持png透明圖片的php生成縮略圖類分享_第1頁
支持png透明圖片的php生成縮略圖類分享_第2頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、這篇文章主要介紹了支持png 透明圖片的 php 生成縮略圖類分享,本文代碼基于 GD2 圖形庫,實(shí)現(xiàn)支持 png 透明圖片生成縮略圖,需要的朋友可以參考下注:此功能依賴 GD2 圖形庫最近要用 php 生成縮略圖,在網(wǎng)上找了一下,發(fā)現(xiàn)了這篇文章:PHP 生成圖片縮略圖試用了一下后,發(fā)現(xiàn)有這樣幾個問題:1、 png 圖片生成的縮略圖是 jpg 格式的2、png 圖片生成的縮略圖沒有了透明(半透明)效果(填充了黑色背景)3、代碼語法比較老因此,在這個版本的基礎(chǔ)上簡單修改優(yōu)化了一下。PHP 生成縮略圖類& t;?php/* desc: Resize lmage(p ng, jpg, gif

2、)* author:十年后的盧哥哥* date: 2014.11.13*/class ResizeImage /圖片類型private $type;/實(shí)際寬度private $width;/實(shí)際高度private $height;/改變后的寬度private $resize_width;/改變后的高度private $resize_height;/是否裁圖private $cut;/源圖象private $srcimg;/目標(biāo)圖象地址private $dstimg;/臨時(shí)創(chuàng)建的圖象private $im;fun ctio n _con struct($imgPath, $width, $hei

3、ght, $isCut, $savePath) $this->srcimg = $imgPath;$this->resize_width = $width;$this->resize_height = $height;$this->cut = $isCut;/圖片的類型$this->type = strtolower(substr(strrchr($this->srcimg,.),1);/初始化圖象$this-> in iti_img();/目標(biāo)圖象地址$this -> dst_im

4、g($savePath);/-$this->width = imagesx($this->im);$this->height = imagesy($this->im);/生成圖象$this-> newimg();ImageDestroy ($this-& gt;im);private fun cti on n ewimg() /改變后的圖象的比例$resize_ratio = ($this->resize_width)/($this-& gt;resize_height);/實(shí)際圖象的比例$ra

5、tio = ($this->width)/($this-& gt;height);if($this->cut) /裁圖$n ewimg = imagecreatetruecolor($this-& gt;resize_width,$this->resize_height);if($this->type=p ng) imagefill($ newimg, 0, 0, imagecolorallocatealpha($ newimg, 0, 0, 0, 127);if($ratio>=$resize_ratio)

6、 高度優(yōu)先imagecopyresampled($ newimg,$this-& gt;im,0,0,0,0,$this->resize_width,$this->resize_height,($this-& gt;height)*$resize_ratio),$this-& gt;height); else /寬度優(yōu)先imagecopyresampled($ newimg,$this-& gt;im,0,0, 0, 0,$this->resize_width,$this->resize_height, $

7、this->width, ($this->width)/$resize_ratio); else /不裁圖if($ratio>=$resize_ratio) $n ewimg = imagecreatetruecolor($this-& gt;resize_width,($this->resize_width)/$ratio);if($this->type=p ng) imagefill($ newimg, 0, 0, imagecolorallocatealpha($ newimg, 0, 0, 0, 127);i

8、magecopyresampled($ newimg,$this-& gt;im,0,0, 0, 0,$this->resize_width,($this->resize_width)/$ratio, $this->width, $this-& gt;height); else $n ewimg = imagecreatetruecolor($this->resize_height)*$ratio,$this->resize_height);if($this->type=p ng) imagef

9、ill($ newimg, 0, 0, imagecolorallocatealpha($ newimg, 0, 0, 0, 127);imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio,$this->resize_height, $this->width, $this->height);if($this->type=p ng) imagesavealpha($ newimg, true);imagep

10、 ng ($n ewimg,$this-& gt;dstimg); else imagejpeg ($n ewimg,$this->dstimg);/初始化圖象private fun cti on in iti_img() if($this->type=jpg) $this->im = imagecreatefromjpeg($this->srcimg);if($this->type=gif) $this->im = imagecreatefromgif($this->srcimg);if

11、($this->type=p ng) $this->im = imagecreatefromp ng($this-& gt;srcimg);/圖象目標(biāo)地址private fun cti on dst_img($dstpath) $full_le ngth = strle n($this->srcimg);$type_le ngth = strle n($this->type);$n ame_le ngth = $fullen gth-$type_le ngth;$n ame = substr($this-& gt;srci

12、mg,0,$ name_le ngth-1);$this->dstimg = $dstpath;?>使用使用時(shí),直接調(diào)用類的構(gòu)造函數(shù)即可,構(gòu)造函數(shù)如下:$resizeimage = new resizeimage($imgPath, $width, $height, $isCut, $savePath);參數(shù)$imgPath :原圖片地址$width :縮略圖寬$height :縮略圖高$isCut :是否裁剪,bool 值$savePath:縮略圖地址(可以跟原圖片地址相同)示例& t;?phpin elude Resizelmage.php;/jpg$jpgResize = new

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論