php gd 缩略图(自家用)

王朝php·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/** *说明: 这个类完成缩略图的生成,支持页面显示和生成文件

*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);//按照自己设计的长宽生成图象,保存或者显示在页面上

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航