php gd 缩略图(自家用)

/** *说明: 这个类完成缩略图的生成,支持页面显示和生成文件
*version 1.0
*@author sanshi(叁石)
*QQ: 35047205
*MSN: sanshi0815@tom.com
*Create 2005/6/18
*******************************************************
*@param string $srcFile 源文件
*@param string $dstFile 目标文件
*@param string $fileType 当前文件类型
*@param string $im 图片打开资源句柄
*@param array $imgType 文件类型定义
*/
class MakeMiniature
{
var $srcFile; //源文件
var $dstFile; //目标文件
var $fileType; //文件类型
var $im; //图片打开资源句柄
var $imgType=array("jpg", //文件类型定义
"gif",
"png",
"bmp");
/**
*说明: 取得文件类型
*@param string $fileName 文件名
*@return boolean 符合return true
*/
function findType($fileName)
{
if($type=strstr($fileName,"."))
{
$type=substr($type,1);
if(!strstr($type,"."))
{
$var=$type;
}else{
echo "file type error!
";
}
}else{
echo "file type error!
";
}
for($i=0;$i<=count($this->imgType);$i++)
{
if(Strcmp($this->imgType[$i],$var)==0)
{
$this->fileType=$var;
return true;
}else{
return false;
}
}
}
/**
*@param $fileType 文件类型
*@return resource 打开图片的资源句柄
*/
function loadImg($fileType)
{
$type=$this->isNull($fileType);
switch($type)
{
case "jpg":
$im=ImageCreateFromjpeg($this->srcFile);
break;
case "gif":
$im=ImageCreateFromGIF($this->srcFile);
break;
case "png":
$im=imagecreatefrompng($this->srcFile);
break;
case "bmp":
$im=imagecreatefromwbmp($this->srcFile);
break;
default:
$im=0;
echo "not you input file type!
"; break;
}
$this->im=$im;
return $im;
}
/**
*说明: 判断标量是否为空,不为空返回变量
*/
function isNull($var)
{
if(!isset($var)||empty($var))
{
echo "变量值为null!
"; exit(0);
}
return $var;
} /**
*说明: 设置源文件名和生成文件名,同时完成了文件类型的确定
* 还有对文件的打开
*@param string srcFile 目标文件
*@param String dstFile 建立文件
*/
function setParam($srcFile,$dstFile)
{
$this->srcFile=$this->isNull($srcFile);
$this->dstFile=$this->isNull($dstFile);
if(!$this->findType($srcFile))
{
echo "file type error!";
}
if(!$this->loadImg($this->fileType))
{
echo "open ".$this->srcFile."error!
";
}
}
/**
*说明 取得图象的宽度
*@param resource im 打开图象成功的资源
*@return int width 图象的宽度
*/
function getImgWidth($im)
{
$im=$this->isNull($im);
$width=imagesx($im);
return $width;
}
/**
*说明 取得图象的高度
*@param resource im 打开图象成功的资源
*@return int height 图象的高度
*/
function getImgHeight($im)
{
$im=$this->isNull($im);
$height=imagesy($im);
return $height;
}
/**
*说明 建立图象
*@param resource im 打开图象成功的资源
*@param int scale 生成图象是与原图象的比例为百分比
*@param boolean page 是否输出到页面
*/
function createImg($im,$scale,$page)
{
$im=$this->isNull($im);
$scale=$this->isNull($scale);
$srcW=$this->getImgWidth($im);
$srcH=$this->getImgHeight($im);
$detW=round($srcW*$scale/100);
$detH=round($srcH*$scale/100);
//$om=ImageCreate($detW,$detH);//普通的使用
$om=imagecreatetruecolor($detW,$detH);//真色彩对gb库有要求
//ImageCopyResized
($om,$im,0,0,0,0,$detW,$detH,$srcW,$srcH);
imagecopyresampled
($om,$im,0,0,0,0,$detW,$detH,$srcW,$srcH);
$this->showImg($om,$this->fileType,$page);
}
/**
*说明 建立图象
*@param resource im 打开图象成功的资源
*@param int scale 生成图象是与原图象的比例为百分比
*@param boolean page 是否输出到页面
*/
function createNewImg($im,$width,$height,$page)
{
$im=$this->isNull($im);
//$scale=$this->isNull($scale);
$srcW=$this->getImgWidth($im);
$srcH=$this->getImgHeight($im);
$detW=$this->isNull($width);
$detH=$this->isNull($height);
//$om=ImageCreate($detW,$detH);//普通的使用
$om=imagecreatetruecolor($detW,$detH);//真色彩对gb库有要求
//ImageCopyResized
($om,$im,0,0,0,0,$detW,$detH,$srcW,$srcH);
imagecopyresampled
($om,$im,0,0,0,0,$detW,$detH,$srcW,$srcH);
$this->showImg($om,$this->fileType,$page);
}
/**
*说明 输出图象建立失败的提示
*@param boolean boolean 判断是否输出
*/
function inputError($boolean)
{
if(!$boolean)
{
echo "img input error!
";
}
}
/**
*说明 根据条件显示图片输出位置和类型
*@param resource $om 图象输出的资源
*@param String $type 输出图象的类型,现在使用源图象的类型
*@param boolean $page 是否在页面上显示
*/
function showImg($om,$type,$page)
{
$om=$this->isNull($om);
$type=$this->isNull($type);
switch($type)
{
case "jpg":
if($page)
{
$suc=imagejpeg($om);
$this->inputError($suc);
}else{
$suc=imagejpeg($om,$this->dstFile);
$this->inputError($suc);
}
break;
case "gif":
if($page)
{
$suc=imagegif($om);
$this->inputError($suc);
}else{
$suc=imagegif($om,$this->dstFile);
$this->inputError($suc);
}
break;
case "png":
if($page)
{
$suc=imagepng($om);
$this->inputError($suc);
}else{
$suc=imagepng($om,$this->dstFile);
$this->inputError($suc);
}
break;
case "bmp":
if($page)
{
$suc=imagewbmp($om);
$this->inputError($suc);
}else{
$suc=imagewbmp($om,$this->dstFile);
$this->inputError($suc);
}
break;
default:
echo "not you input file type!
"; break;
}
}
}
使用
$file=new MakeMiniature();
$file->setParam("img/Logo.jpg","img/Logo1.jpg");//设置源文件,跟生成文件
$file->createImg($file->im,200,true);//按比例生成图象,比例为200%,在页面上显示
$file->createImg($file->im,200,false);//按比例生成图象,比例为200%,生成图片保存到上面设置的名字和路径
$file->createNewImg($file->im,100,100,true);//按照自己设计的长宽生成图象,保存或者显示在页面上
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值