日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php缩放库,php的缩放图像类使用

發(fā)布時(shí)間:2025/3/8 php 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php缩放库,php的缩放图像类使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

標(biāo)題: php的縮放圖像類使用

作者:李英江

日期: 2006-11-25 14:54:05

轉(zhuǎn)載請保留作者內(nèi)容: http://www.cgsir.com

使用縮略圖有個(gè)好處就是可以減少用戶下載的時(shí)間,為此要在上傳圖片時(shí)生成一張比原圖小的圖片,一般的PHP空間都支持GD圖形庫,我的網(wǎng)站上GD版本是2.0.28,你可以自己寫圖像生成程序,php手冊有現(xiàn)成的例子,應(yīng)該不難,但為了更快完成這功能,試用網(wǎng)上下載的一個(gè)縮放圖像類,感覺也蠻方便的。

注意:確認(rèn)你的php支持GD庫,你可以通過使用 <?php phpinfo(); ?>查看你的GD版本,以下我的網(wǎng)站GD版本

如果你的PHP沒有這些內(nèi)容,那么說明你的php還不支持這個(gè)庫,我針對windows 和linux分別作說明。

Windows:

找到你的php.ini的配置文件,打開GD動(dòng)態(tài)鏈接庫就可以了。也就是說找到“;extension=php_gd2.dll”把分號(hào)去掉,重起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圖像存儲(chǔ)路徑)

以下是完整圖像類image_class.php 源碼:

/***************************************/

/*功? 能:利用PHP的GD庫生成高質(zhì)量的縮略圖*/

/*運(yùn)行環(huán)境:PHP5.01/GD2*/

/*類說明:可以選擇是/否裁圖。

如果裁圖則生成的圖的尺寸與您輸入的一樣。

原則:盡可能多保持原圖完整

如果不裁圖,則按照原圖比例生成新圖

原則:根據(jù)比例以輸入的長或者寬為基準(zhǔn)*/

/*參 數(shù):$img:源圖片地址

$wid:新圖的寬度

$hei:新圖的高度

$c:是否裁圖,1為是,0為否*/

/***************************************/

class resizeimage

{

//圖片類型

var $type;

//實(shí)際寬度

var $width;

//實(shí)際高度

var $height;

//改變后的寬度

var $resize_width;

//改變后的高度

var $resize_height;

//是否裁圖

var $cut;

//源圖象

var $srcimg;

//目標(biāo)圖象地址

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();??????? //目標(biāo)圖象地址??????? $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);??????? //實(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($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);??????? }??? }??? //圖象目標(biāo)地址??? 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;??? }}?>

總結(jié)

以上是生活随笔為你收集整理的php缩放库,php的缩放图像类使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。