基于GD2圖形庫(kù)的PHP生成圖片縮略圖類(lèi)代碼分享
更新時(shí)間:2015年02月08日 14:26:47 投稿:junjie
這篇文章主要介紹了基于GD2圖形庫(kù)的PHP生成圖片縮略圖類(lèi)代碼分享,本文直接給出實(shí)現(xiàn)代碼和使用方法,需要的朋友可以參考下
要使用PHP生成圖片縮略圖,要保證你的PHP服務(wù)器安裝了GD2圖形庫(kù) 使用一個(gè)類(lèi)生成圖片的縮略圖
1.使用方法
$resizeimage = new resizeimage("圖片源文件地址", "200", "100", "0","縮略圖地址"); //就只用上面的一句話(huà),就能生成縮略圖,其中,源文件和縮略圖地址可以相同,200,100分別代表寬和高
2. 縮略圖類(lèi)代碼
//使用如下類(lèi)就可以生成圖片縮略圖, <?php class resizeimage { //圖片類(lèi)型 var $type; //實(shí)際寬度 var $width; //實(shí)際高度 var $height; //改變后的寬度 var $resize_width; //改變后的高度 var $resize_height; //是否裁圖 var $cut; //源圖象 var $srcimg; //目標(biāo)圖象地址 var $dstimg; //臨時(shí)創(chuàng)建的圖象 var $im; function resizeimage($img, $wid, $hei,$c,$dstpath) { $this->srcimg = $img; $this->resize_width = $wid; $this->resize_height = $hei; $this->cut = $c; //圖片的類(lèi)型 $this->type = strtolower(substr(strrchr($this->srcimg,"."),1)); //初始化圖象 $this->initi_img(); //目標(biāo)圖象地址 $this -> dst_img($dstpath); //-- $this->width = imagesx($this->im); $this->height = imagesy($this->im); //生成圖象 $this->newimg(); ImageDestroy ($this->im); } function newimg() { //改變后的圖象的比例 $resize_ratio = ($this->resize_width)/($this->resize_height); //實(shí)際圖象的比例 $ratio = ($this->width)/($this->height); if(($this->cut)=="1") //裁圖 { if($ratio>=$resize_ratio) //高度優(yōu)先 { $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height); ImageJpeg ($newimg,$this->dstimg); } if($ratio<$resize_ratio) //寬度優(yōu)先 { $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio)); ImageJpeg ($newimg,$this->dstimg); } } else //不裁圖 { if($ratio>=$resize_ratio) { $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height); ImageJpeg ($newimg,$this->dstimg); } if($ratio<$resize_ratio) { $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height); ImageJpeg ($newimg,$this->dstimg); } } } //初始化圖象 function initi_img() { if($this->type=="jpg") { $this->im = imagecreatefromjpeg($this->srcimg); } if($this->type=="gif") { $this->im = imagecreatefromgif($this->srcimg); } if($this->type=="png") { $this->im = imagecreatefrompng($this->srcimg); } } //圖象目標(biāo)地址 function dst_img($dstpath) { $full_length = strlen($this->srcimg); $type_length = strlen($this->type); $name_length = $full_length-$type_length; $name = substr($this->srcimg,0,$name_length-1); $this->dstimg = $dstpath; //echo $this->dstimg; } } ?>
您可能感興趣的文章:
- PHP圖像處理技術(shù)實(shí)例總結(jié)【繪圖、水印、驗(yàn)證碼、圖像壓縮】
- php實(shí)現(xiàn)算術(shù)驗(yàn)證碼功能
- PHP封裝的驗(yàn)證碼工具類(lèi)定義與用法示例
- PHP實(shí)現(xiàn)隨機(jī)數(shù)字、字母的驗(yàn)證碼功能
- php生成復(fù)雜驗(yàn)證碼(傾斜,正弦干擾線,黏貼,旋轉(zhuǎn))
- PHP驗(yàn)證碼類(lèi)文件及調(diào)用方式代碼詳解
- PHP使用GD庫(kù)制作驗(yàn)證碼的方法(點(diǎn)擊驗(yàn)證碼或看不清會(huì)刷新驗(yàn)證碼)
- 一個(gè)漂亮的php驗(yàn)證碼類(lèi)(分享)
- PHP生成圖片驗(yàn)證碼、點(diǎn)擊切換實(shí)例
- 如何打開(kāi)php的gd2庫(kù)
- PHP基于GD2函數(shù)庫(kù)實(shí)現(xiàn)驗(yàn)證碼功能示例
相關(guān)文章
PHP實(shí)現(xiàn)支持GET,POST,Multipart/form-data的HTTP請(qǐng)求類(lèi)
這篇文章主要介紹了PHP實(shí)現(xiàn)支持GET,POST,Multipart/form-data的HTTP請(qǐng)求類(lèi),包括了連接與處理方式及相關(guān)的技巧,需要的朋友可以參考下2014-09-09PHP中preg_match正則匹配中的/u、/i、/s含義
這篇文章主要介紹了PHP中preg_match正則匹配中的/u、/i、/s含義,本文分別講解了這三個(gè)參數(shù)的含義,需要的朋友可以參考下2015-04-04php配置php-fpm啟動(dòng)參數(shù)及配置詳解
php-fpm 啟動(dòng)參數(shù)及重要配置詳解,這個(gè)不用多說(shuō),做PHP開(kāi)發(fā)的同學(xué)都需要用。2013-11-11PHP var關(guān)鍵字相關(guān)原理及使用實(shí)例解析
這篇文章主要介紹了PHP var關(guān)鍵字相關(guān)原理及使用實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Thinkphp結(jié)合ajaxFileUpload實(shí)現(xiàn)異步圖片傳輸示例
這篇文章主要給大家介紹了利用Thinkphp結(jié)合ajaxFileUpload實(shí)現(xiàn)異步圖片傳輸?shù)姆椒?,文中給出了詳細(xì)的示例代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03