麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 語言 > PHP > 正文

PHP5:圖像縮小及格式轉換CLASS

2024-09-04 11:50:10
字體:
來源:轉載
供稿:網友
<?
/**
*
* 對圖像進行縮小,也可對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;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 在线影院av | 全黄毛片 | 91在线色 | 国产色视频免费 | 中文字幕 日本 | 中文字幕在线观看精品 | 欧美日韩亚洲在线观看 | 天天舔天天插 | 欧美日韩一区三区 | 亚州成人在线观看 | 毛片免费观看完整版 | 国产一级91 | 久久久成人动漫 | 国产在线精品一区二区 | 一级成人欧美一区在线观看 | 91成人影院 | 久久毛片免费 | 嗯~啊~弄嗯~啊h高潮视频 | 在线2区 | 久草在线观看福利视频 | 精品国产乱码久久久久久久 | 黄网站免费观看视频 | 成人性生活视频在线观看 | 日韩视频www| 国产中出视频 | 欧美日韩精品一区二区三区蜜桃 | 国产精品区一区二区三区 | 国产黄网 | 黄色毛片视频在线观看 | 日韩视频在线不卡 | 海外中文字幕在线观看 | 一级电影中文字幕 | 免费毛片播放 | 国产精品一区二区三区99 | 国产成年人在线观看 | 欧美亚洲国产成人综合在线 | 成人免费毛片片v | 亚洲日本高清 | 男女羞羞视频在线观看免费 | 欧美视频在线一区二区三区 | 国产一级二级毛片 |