<?
php
/* *
*说明: 这个类完成缩略图的生成,支持页面显示和生成文件,文字水印,图象水印
*version 2.0
*@author sanshi(叁石)
*QQ: 35047205
*MSN: sanshi0815@tom.com
*Create 2007/5/11
*******************************************************
*@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 $isPrint = true ; // 是否加水印
var $setTxt = " www.金库.com " ; // 水印的文字
var $setPrintFile = "" ; // 设置水印图片
var $font ; // 设置字体
var $imgType = array ( " jpg " , // 文件类型定义
" gif " ,
" png " ,
" bmp " );
function MakeMiniature()
{
$this -> font = FONT_PATH . " simhei.ttf " ;
}
// 取得显示的文字
function getTxt( $instring )
{
// return iconv("GB2312","UTF-8",$instring);
$outstring = "" ;
$max = strlen ( $instring );
for ( $i = 0 ; $i < $max ; $i ++ )
{
$h = ord ( $instring [ $i ]);
if ( $h >= 160 && $i < $max - 1 )
{
$outstring .= " &# " . base_convert ( bin2hex ( iconv ( " gb2312 " , " ucs-2 " , substr ( $instring , $i , 2 ))) , 16 , 10 ) . " ; " ;
$i ++ ;
}
else
{
$outstring .= $instring [ $i ];
}
}
// echo $outstring;exit();
return $outstring ;
}
// 图片水印的使用
function makeImgPrint( $im )
{
$src = $this -> loadImg( ' jpg ' , $this -> setPrintFile);
$src_x = 0 ;
$src_y = 0 ;
$src_w = $this -> getImgWidth( $src );
$src_h = $this -> getImgHeight( $src );
$dst_x = $this -> getImgWidth( $im );
$dst_y = $this -> getImgHeight( $im );
$height = $dst_y - $src_h ;
$width = ( $dst_x - $src_w ) / 2 ;
imagealphablending( $im , true );
imagecopymerge( $im , $src , $width , $height , 0 , 0 , $src_w , $src_h , 70 );
return $im ;
}
// 文字水印的使用
function makePrint( $im )
{
// 旋转角度
$angle = 20 ;
$width = $this -> getImgWidth( $im ) / 10 ;
$size = $width ;
$height = $this -> getImgHeight( $im ) - $this -> getImgHeight( $im ) / 50 ;
$black = imagecolorallocate( $im , 0 , 0 , 0 );
$grey = imagecolorallocate( $im , 180 , 180 , 180 );
for ( $i = 1 ; $i <= 3 ; $i ++ )
{
// $height +=$height*3/10;
$height = $height - $this -> getImgHeight( $im ) / 5 ;
imagettftext ( $im , $size , $angle , $width , $height , $grey , $this -> font , $this -> getTxt( $this -> setTxt));
}
return $im ;
}
/* *
*说明: 取得文件类型
*@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!<br> " ;
}
} else {
echo " file type error!<br> " ;
}
for ( $i = 0 ; $i <= count ( $this -> imgType); $i ++ )
{
if ( Strcmp ( $this -> imgType[ $i ] , $var ) == 0 )
{
$this -> fileType = $var ;
return true ;
}
}
return false ;
}
/* *
*@param $fileType 文件类型
*@return resource 打开图片的资源句柄
*/
function loadImg( $fileType , $srcFile = "" )
{
$type = $this -> isNull( $fileType );
if ( $srcFile == "" ) $srcFile = $this -> srcFile;
switch ( $type )
{
case " jpg " :
$im = imageCreateFromjpeg( $srcFile );
break ;
case " gif " :
$im = imageCreateFromGIF( $srcFile );
break ;
case " png " :
$im = imagecreatefrompng( $srcFile );
break ;
case " bmp " :
$im = imagecreatefromwbmp( $srcFile );
break ;
default :
$im = 0 ;
echo " not you input file type!<br> " ;
break ;
}
$this -> im = $im ;
return $im ;
}
/* *
*说明: 判断标量是否为空,不为空返回变量
*/
function isNull( $var )
{
if ( ! isset ( $var ) || empty ( $var ))
{
echo " 变量值为null!<br> " ;
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!<br> " ;
}
}
/* *
*说明 取得图象的宽度
*@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 );
// om 为生成的图片资源
$this -> showImg( $om , $this -> fileType , $page );
}
/* *
*说明 输出图象建立失败的提示
*@param boolean boolean 判断是否输出
*/
function inputError( $boolean )
{
if ( ! $boolean )
{
echo " img input error!<br> " ;
}
}
/* *
*说明 根据条件显示图片输出位置和类型
*@param resource $om 图象输出的资源
*@param String $type 输出图象的类型,现在使用源图象的类型
*@param boolean $page 是否在页面上显示
*/
function showImg( $om , $type , $page )
{
$om = $this -> isNull( $om );
if ( $this -> isPrint)
{
$om = $this -> makePrint( $om );
if ( $this -> setPrintFile)
$om = $this -> makeImgPrint( $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!<br> " ;
break ;
}
}
}
?>
<? php
// $file=new MakeMiniature();
//echo $file->getTxt('jinku');
//$file->setParam("Logo.jpg","p.jpg");//设置源文件,跟生成文件
//$file->createImg($file->im,200,true);//按比例生成图象,比例为200%,在页面上显示
//$file->createImg($file->im,200,false);//按比例生成图象,比例为200%,生成图片保存到上面设置的名字和路径
//$file->createNewImg($file->im,20,10,false);//按照自己设计的长宽生成图象,保存或者显示在页面上
?>
/* *
*说明: 这个类完成缩略图的生成,支持页面显示和生成文件,文字水印,图象水印
*version 2.0
*@author sanshi(叁石)
*QQ: 35047205
*MSN: sanshi0815@tom.com
*Create 2007/5/11
*******************************************************
*@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 $isPrint = true ; // 是否加水印
var $setTxt = " www.金库.com " ; // 水印的文字
var $setPrintFile = "" ; // 设置水印图片
var $font ; // 设置字体
var $imgType = array ( " jpg " , // 文件类型定义
" gif " ,
" png " ,
" bmp " );
function MakeMiniature()
{
$this -> font = FONT_PATH . " simhei.ttf " ;
}
// 取得显示的文字
function getTxt( $instring )
{
// return iconv("GB2312","UTF-8",$instring);
$outstring = "" ;
$max = strlen ( $instring );
for ( $i = 0 ; $i < $max ; $i ++ )
{
$h = ord ( $instring [ $i ]);
if ( $h >= 160 && $i < $max - 1 )
{
$outstring .= " &# " . base_convert ( bin2hex ( iconv ( " gb2312 " , " ucs-2 " , substr ( $instring , $i , 2 ))) , 16 , 10 ) . " ; " ;
$i ++ ;
}
else
{
$outstring .= $instring [ $i ];
}
}
// echo $outstring;exit();
return $outstring ;
}
// 图片水印的使用
function makeImgPrint( $im )
{
$src = $this -> loadImg( ' jpg ' , $this -> setPrintFile);
$src_x = 0 ;
$src_y = 0 ;
$src_w = $this -> getImgWidth( $src );
$src_h = $this -> getImgHeight( $src );
$dst_x = $this -> getImgWidth( $im );
$dst_y = $this -> getImgHeight( $im );
$height = $dst_y - $src_h ;
$width = ( $dst_x - $src_w ) / 2 ;
imagealphablending( $im , true );
imagecopymerge( $im , $src , $width , $height , 0 , 0 , $src_w , $src_h , 70 );
return $im ;
}
// 文字水印的使用
function makePrint( $im )
{
// 旋转角度
$angle = 20 ;
$width = $this -> getImgWidth( $im ) / 10 ;
$size = $width ;
$height = $this -> getImgHeight( $im ) - $this -> getImgHeight( $im ) / 50 ;
$black = imagecolorallocate( $im , 0 , 0 , 0 );
$grey = imagecolorallocate( $im , 180 , 180 , 180 );
for ( $i = 1 ; $i <= 3 ; $i ++ )
{
// $height +=$height*3/10;
$height = $height - $this -> getImgHeight( $im ) / 5 ;
imagettftext ( $im , $size , $angle , $width , $height , $grey , $this -> font , $this -> getTxt( $this -> setTxt));
}
return $im ;
}
/* *
*说明: 取得文件类型
*@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!<br> " ;
}
} else {
echo " file type error!<br> " ;
}
for ( $i = 0 ; $i <= count ( $this -> imgType); $i ++ )
{
if ( Strcmp ( $this -> imgType[ $i ] , $var ) == 0 )
{
$this -> fileType = $var ;
return true ;
}
}
return false ;
}
/* *
*@param $fileType 文件类型
*@return resource 打开图片的资源句柄
*/
function loadImg( $fileType , $srcFile = "" )
{
$type = $this -> isNull( $fileType );
if ( $srcFile == "" ) $srcFile = $this -> srcFile;
switch ( $type )
{
case " jpg " :
$im = imageCreateFromjpeg( $srcFile );
break ;
case " gif " :
$im = imageCreateFromGIF( $srcFile );
break ;
case " png " :
$im = imagecreatefrompng( $srcFile );
break ;
case " bmp " :
$im = imagecreatefromwbmp( $srcFile );
break ;
default :
$im = 0 ;
echo " not you input file type!<br> " ;
break ;
}
$this -> im = $im ;
return $im ;
}
/* *
*说明: 判断标量是否为空,不为空返回变量
*/
function isNull( $var )
{
if ( ! isset ( $var ) || empty ( $var ))
{
echo " 变量值为null!<br> " ;
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!<br> " ;
}
}
/* *
*说明 取得图象的宽度
*@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 );
// om 为生成的图片资源
$this -> showImg( $om , $this -> fileType , $page );
}
/* *
*说明 输出图象建立失败的提示
*@param boolean boolean 判断是否输出
*/
function inputError( $boolean )
{
if ( ! $boolean )
{
echo " img input error!<br> " ;
}
}
/* *
*说明 根据条件显示图片输出位置和类型
*@param resource $om 图象输出的资源
*@param String $type 输出图象的类型,现在使用源图象的类型
*@param boolean $page 是否在页面上显示
*/
function showImg( $om , $type , $page )
{
$om = $this -> isNull( $om );
if ( $this -> isPrint)
{
$om = $this -> makePrint( $om );
if ( $this -> setPrintFile)
$om = $this -> makeImgPrint( $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!<br> " ;
break ;
}
}
}
?>
<? php
// $file=new MakeMiniature();
//echo $file->getTxt('jinku');
//$file->setParam("Logo.jpg","p.jpg");//设置源文件,跟生成文件
//$file->createImg($file->im,200,true);//按比例生成图象,比例为200%,在页面上显示
//$file->createImg($file->im,200,false);//按比例生成图象,比例为200%,生成图片保存到上面设置的名字和路径
//$file->createNewImg($file->im,20,10,false);//按照自己设计的长宽生成图象,保存或者显示在页面上
?>
同时支持了,中文,还有图片做水印的功能,有bug请提出
使用的时候请注意,默认字体的使用,好多字体都不能正常显示中文,一般使用simhei.ttf 这个字体,反复测试没有问题的