標題: php的縮放圖像類使用
作者:李英江
日期: 2006-11-25 14:54:05
轉載請保留作者內容: http://www.cgsir.com
使用縮略圖有個好處就是可以減少用戶下載的時間,為此要在上傳圖片時生成一張比原圖小的圖片,一般的PHP空間都支持GD圖形庫,我的網站上GD版本是2.0.28,你可以自己寫圖像生成程序,php手冊有現成的例子,應該不難,但為了更快完成這功能,試用網上下載的一個縮放圖像類,感覺也蠻方便的。
注意:確認你的php支持GD庫,你可以通過使用 <?php phpinfo(); ?>查看你的GD版本,以下我的網站GD版本
如果你的PHP沒有這些內容,那么說明你的php還不支持這個庫,我針對windows 和linux分別作說明。
Windows:
找到你的php.ini的配置文件,打開GD動態鏈接庫就可以了。也就是說找到“;extension=php_gd2.dll”把分號去掉,重起apache就可以了。
Linux:
# tar -zxf gd-2.0.33.tar.gz
# cd gd-2.0.33
# ./configure --prefix=/usr/local/gd2 --with-jpeg=/usr/local/jpeg6/ --with-png --with-zlib --with-freetype=/usr/local/freetype/
# make; make install
使用縮放圖像類:
1. 包含類 include_once('image_class.php');?? // 用于生成縮略圖類
2. 生成縮略圖 120*90? 0為不載圖 $small_img = new resizeimage($image_full_dir, 120, 90, 0); ($image_full_dir圖像存儲路徑)
以下是完整圖像類image_class.php 源碼:
/***************************************/
/*功? 能:利用PHP的GD庫生成高質量的縮略圖*/
/*運行環境:PHP5.01/GD2*/
/*類說明:可以選擇是/否裁圖。
如果裁圖則生成的圖的尺寸與您輸入的一樣。
原則:盡可能多保持原圖完整
如果不裁圖,則按照原圖比例生成新圖
原則:根據比例以輸入的長或者寬為基準*/
/*參 數:$img:源圖片地址
$wid:新圖的寬度
$hei:新圖的高度
$c:是否裁圖,1為是,0為否*/
/***************************************/
class resizeimage
{
//圖片類型
var $type;
//實際寬度
var $width;
//實際高度
var $height;
//改變后的寬度
var $resize_width;
//改變后的高度
var $resize_height;
//是否裁圖
var $cut;
//源圖象
var $srcimg;
//目標圖象地址
var $dstimg;
//臨????建的圖象
var $im;
function resizeimage($img, $wid, $hei,$c)??? {??????? $this->srcimg = $img;??????? $this->resize_width = $wid;??????? $this->resize_height = $hei;??????? $this->cut = $c;??????? //圖片的類型??????? $this->type = substr(strrchr($this->srcimg,"."),1);??????? //初始化圖象??????? $this->initi_img();??????? //目標圖象地址??????? $this -> dst_img();??????? //--??????? $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);??????? //實際圖象的比例??????? $ratio = ($this->width)/($this->height);??????? if(($this->cut)=="1")??????? //裁圖??????? {??????????? if($ratio>=$resize_ratio)??????????? //高度優先??????????? {??????????????? $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($ratioresize_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($ratioresize_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);??????? }??? }??? //圖象目標地址??? function dst_img()??? {??????? $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 = $name."s.".$this->type;??? }}?>