<?
/**
*
* 對圖像進行縮小,也可對png, gif, jpeg, wbmp格式的圖像進行轉換
* 需要GD庫的支持才可以,若要進行gif圖像的輸出需要GD2.0.28或更高版本才支持(或* gif的動畫轉了之后動畫變成靜畫,不知為什么!
*
* @date 2004.08.16
* @author zhouxh#im.ac.cn
*
*/
class ResizeImage {
const ResizeImageInfo = "本類對圖像進行縮小,也可對png, gif, jpeg, wbmp格式的圖像進行轉換";
//設置目標圖像的寬和高
private $height = 100;
private $width = 100;
//源圖像文件和目標圖像文件,若只是輸出至瀏覽器則目標圖像文件可不設置
private $sourceFile = '';
private $dstFile = '';
//圖像類型“image/gif、image/jpeg、image/png...”
private $imgType;
//源圖像句柄和目標圖像句柄
private $sim;
private $dim;
//是否保存圖像,用public void saveFlag(boolean $flag)方法設置
private $saveFlag = true;
function __construct() {
if (!function_exists('imagecreate')) {
throw new Exception('你的系統不支持GD庫');
}
}
function __toString() {
return ReSizeImage::ResizeImageInfo;
}
//設置目標圖像的寬
public function setWidth($width) {
if ($width <= 0) {
throw new Exception('目標圖像寬度不能小于0');
return ;
}
$this->width = $width;
}
//設置目標圖像的高
public function setHeight($height) {
if ($height <= 0) {
throw new Exception('目標圖像高度不能小于0');
return ;
}
$this->height = $height;
}
//設置源圖像文件
public function setSourceFile($file) {
if (!file_exists($file)) {
throw new Exception('源圖像文件不存在');
return ;
}
$this->sourceFile = $file;
}
//設置目標圖像文件
public function setDstFile($file) {
$this->dstFile = $file;
}
//設置是否生成新文件
public function saveFile($flag) {
$this->saveFlag = (boolean)$flag;
}
//執行繪圖操作,$quality參數表示生成圖像的效果,數字越高,效果越好,不過僅用于jpeg類型的圖像
public function draw($quality = 95) {
$sourceImgInfo = @getimagesize($this->sourceFile);
if (!is_array($sourceImgInfo)) {
throw new Exception('源圖像文件不存在');
return ;
}
switch($sourceImgInfo[2]){
case 1:
$this->imgType="image/gif";
$this->sim = imagecreatefromgif($this->sourceFile);
break;
case 2:
$this->imgType="image/jpeg";
$this->sim = imagecreatefromjpeg($this->sourceFile);
break;
case 3:
$this->imgType="image/png";
$this->sim = imagecreatefrompng($this->sourceFile);
break;
case 15:
$this->imgType="image/wbmp";
$this->sim = imagecreatefromwbmp($this->sourceFile);
break;
default:
return '不支持的圖像格式';
break;
}
//設置目標圖像的實際寬和高
$dstWidth = $sourceWidth = $sourceImgInfo[0];
$dstHeight = $sourceHeight = $sourceImgInfo[1];
if ($sourceHeight > $this->height && $sourceWidth > $this->width) {
if ($sourceHeight > $sourceWidth) {
$zoom = $this->height / $sourceHeight;
$dstHeight = $this->height;
$dstWidth = $sourceWidth * $zoom;
} else {
$zoom = $this->width / $sourceWidth;
$dstWidth = $this->width;
$dstHeight = $sourceHeight * $zoom;
}
}
//建立目標圖像的句柄
$this->dim = @imagecreatetruecolor($dstWidth, $dstHeight) or imagecreate($dstWidth, $dstHeight);
//將真彩色圖像轉換為調色板圖像
imagetruecolortopalette($this->sim, false, 256);
//根據源圖像顏色的總數并把它分配到目標圖像上
$palsize = ImageColorsTotal($this->sim);
for ($i = 0; $i<$palsize; $i++) {
$colors = ImageColorsForIndex($this->sim, $i);
ImageColorAllocate($this->dim, $colors['red'], $colors['green'], $colors['blue']);
}
//進行圖像的縮放
imagecopyresampled($this->dim, $this->sim, 0, 0, 0, 0, $dstWidth, $dstHeight, $sourceWidth, $sourceHeight);
//生成新的目標圖像
if ($this->saveFlag) {
$imgExt = substr($this->dstFile, strrpos($this->dstFile, '.') + 1);
switch(strtolower($imgExt)){
case 'gif':
if (!function_exists('imagegif')) {
throw new Exception('你的GD庫不支持gif圖像的輸出');
return ;
}
imagegif($this->dim, $this->dstFile);
break;
case 'jpeg':
case 'jpg':
imagejpeg($this->dim, $this->dstFile, $quality);
break;
case 'png':
imagepng($this->dim, $this->dstFile);
break;
case 'wbmp':
imagewbmp($this->dim, $this->dstFile);
break;
default:
return '目標圖像文件為空或者格式不對,無法進行保存';
break;
}
//直接輸出目標圖像至瀏覽器
} else {
header ("Content-type: " . $this->imgType);
switch($sourceImgInfo[2]){
case 1:
imagegif($this->dim);
break;
case 2:
imagejpeg($this->dim, '', $quality);
break;
case 3:
imagepng($this->dim);
break;
case 15:
imagewbmp($this->dim);
break;
default:
return '不支持的圖像格式';
break;
}
}
return ;
}
function __destruct() {
@ImageDestroy($this->sim);
@ImageDestroy($this->dim);
}
}
?>
例子1:縮小圖像后直接輸出至瀏覽器
$obj = new ReSizeImage();
$obj->setSourceFile('win.png');
$obj->saveFile(false);
$obj->setWidth(250);
$obj->setHeight(250);
$obj->draw();
例子2:縮小圖像后保存新圖像文件為“new.png”
$obj = new ReSizeImage();
$obj->setSourceFile('win.png');
$obj->setDstFile('new.png');
$obj->setWidth(250);
$obj->setHeight(250);
$obj->draw();
例子3:縮小圖像后保存新圖像文件為“new.jpg”,并設置其quality值為“100”
$obj = new ReSizeImage();
$obj->setSourceFile('win.png');
$obj->setDstFile('new.jpg');
$obj->setWidth(250);
$obj->setHeight(250);
$obj->draw(100);
例子4:捕捉程序中的異常
try {
$obj = new ReSizeImage();
$obj->setSourceFile('no.png');
$obj->saveFile(false);
$obj->setWidth(250);
$obj->setHeight(250);
$obj->draw();
} catch (Exception $ex) {
echo $ex;
}