随笔 - 61  文章 - 48  trackbacks - 0
<2007年10月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

留言簿(10)

随笔档案

收藏网站

搜索

  •  

最新评论

阅读排行榜

评论排行榜

PHP图片处理类CImage

/*
此类包含以下功能
A.生成缩略图
B.给图片添加文字水印(包括中文)
C.将指定的图片旋转90度/180度/270度,并保存至文件
D.将图片水平/垂直翻转,并保存至文件
E.在线裁剪图片任意部分矩形(正在编写之中)
*/
成员函数说明
---------------------------------------------------------------------------------------------
public void CImage::__construct(string $src_image_file)
功能:类CImage的构造函数
参数 
$src_image_file 字符串,源图片文件名 注意,目前只支持gif,png,jpeg,jpg格式,这是GD库的限制,并非本程序的局限
---------------------------------------------------------------------------------------------
public bool CImage: thumb($image_dist,$x)
功能:根据源图片生成缩略图,并保存至文件
$image_dist 字符串  目标缩略图片路径及文件名 如/images/th.jpg
$x 整型 目标缩略图片的尺寸限制即当原始图片的宽大于高时,那么新的缩略图的宽为$x,反之高为$x
---------------------------------------------------------------------------------------------
public bool image_press($image_dist,$str,$font="simkai.ttf")
函数功能:
图片生成水印并保存新图片至目标文件
参数说明:
$image_dist字符串 目标图片名
$str 字符串 要写入到图片水印的字符串
$font 字符串 合法的系统字体名或WEB目录中正确的字体文件名
---------------------------------------------------------------------------------------------
public bool rotate($image_dist,$angle)
函数功能:
将源图片旋转一定角度并将新图片保存至文件
参数说明:
$image_dist 字符串 目标图片文件名 HP0-094 70-500  
$angle 整数 要旋转的角度 只能是90或180或270度
---------------------------------------------------------------------------------------------
public bool rotate_h($image_dist)
函数功能:
将源图片水平翻转,并将新图片保存至文件
参数说明:
$image_dist 字符串 目标图片文件名
---------------------------------------------------------------------------------------------
public bool rotate_v($image_dist)
函数功能:
将源图片垂直翻转,并将新图片保存至文件
参数说明:
$image_dist 字符串 目标图片文件名
---------------------------------------------------------------------------------------------
使用范例:
$p=new CImage("s.jpg"); //创建一个图片处理对象
$p->thumb("thumb.jpg",300); //生成缩略图 限制尺寸为300,保存为MB4-174 HP0-302 .jpg
$p->rotate("rt.jpg",90); //旋转90度,并保存为rt.jpg,类似地,你可以将90换成180,270进行旋转
$p->rotate_h("h.jpg"); //水平翻转
$p->rotate_v("v.jpg"); //垂直翻转
说明定义完毕
此类还在进一步完善之中,欢迎朋友们一起交流,提出宝贵意见
*/
class CImage
{
private $src_image;
private $width;
private $height;
private $image_type;
private $img;
private $src_x;
private $src_y;

function__construct($image_file)
{
$info=GetImageSize($image_file);
$this->src_image=$image_file;
$this->width=$info[0];
$this->height=$info[1];

switch($info[2])
{
case1:
$this->image_type="gif";
break;
case2:
$this->image_type="jpeg";
break;
case3:
$this->image_type="png";
break;
default:
return false;
//echo("Unsurport Image type.");
break;
} //swith end
//echo"ok";
$new_function='ImageCreateFrom'.ucfirst($this->image_type);
$this->img=$new_function($this->src_image);
$this->src_x=ImageSX($this->img);
$this->src_y=ImageSY($this->img);
}
function thumb($image_dist,$x) //$x为新图的限制边的尺寸
{
$src_x=ImageSX($this->img);
$src_y=ImageSY($this->img);
$scale=min($x/$src_x,$x/$src_y);

if($scale<1)
{
$new_x=floor($scale*$src_x);
$new_y=floor($scale*$src_y);
$img_tmp=ImageCreateTrueColor($new_x,$new_y); //set the size of Canvas for the new Image
ImageCopyResampled($img_tmp,$this->img,0,0,0,0,$new_x,$new_y,$src_x,$src_y); //Resampled
ImageDestroy($this->img);
$new_function="Image".ucfirst($this->image_type);
return$new_function($img_tmp,$image_dist);
}
} // thumb end

public function image_press($image_dist,$str,$font="simkai.ttf") //给图片生成文字水印
{
$str=iconv("GB2312","utf-8",$str);
$blue=ImageColorAllocate($this->img,90,255,255);
$white=ImageColorAllocate($this->img,255,0,0);
ImageTTFText($this->img,20,0,$this->src_x/2/2,$this->src_y-80,$white,$font,$str);
$new_function="Image".ucfirst($this->image_type);
return$new_function($this->img,$image_dist);
}
public function rotate($image_dist,$angle)
{
$img_tmp=null;
$new_function="Image".ucfirst($this->image_type);
if(($angle!=90)&&($angle!=180)&&($angle!=270))
{
echo("Un-valid angle on calling CImage::rotate(\$image_dist,\$angle) .
The valid angle must be 90 or 180 or 270.");
return false;
}
if(($angle==90)||($angle==270))
{
$img_tmp=ImageCreateTrueColor($this->src_y,$this->src_x);
}
else
{
$img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
}

switch($angle)
{
case90:
for($i=0;$i<$this->src_x;$i++)
{
for($j=0;$j<$this->src_y;$j++)
{
ImageSetPixel($img_tmp,$this->src_y-$j-1,$i,ImageColorAt($this->img,$i,$j));
}
}
return$new_function($img_tmp,$image_dist);
break;
case180:
for($i=0;$i<$this->src_x;$i++)
{
for($j=0;$j<$this->src_y;$j++)
{
ImageSetPixel($img_tmp,$this->src_x-$i-1,$this->src_y-$j-1,ImageColorAt($this->img,$i,$j));
}
}
return$new_function($img_tmp,$image_dist);
break;
case270:
for($i=0;$i<$this->src_x;$i++)
{
for($j=0;$j<$this->src_y;$j++)
{
ImageSetPixel($img_tmp,$j,$this->src_x-$i-1,ImageColorAt($this->img,$i,$j));
}
}
return$new_function($img_tmp,$image_dist);
break;
} //end switch

} //end rotate

public function rotate_h($image_dist)
{
$new_function="Image".ucfirst($this->image_type);
$img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
ImageCopyResampled($img_tmp,$this->img,0,0,$this->src_x-1,0,$this->src_x,$this->src_y,-$this->src_x,$this->src_y); //水平翻转
return $new_function($img_tmp,$image_dist);
}

function rotate_v($image_dist)
{
$new_function="Image".ucfirst($this->image_type);
$img_tmp=ImageCreateTrueColor($this->src_x,$this->src_y);
ImageCopyResampled($img_tmp,$this->img,0,0,0,$this->src_y-1,$this->src_x,$this->src_y,$this->src_x,-$this->src_y);
return$new_function($img_tmp,$image_dist);
}
} //end CImage
?>

 

 

posted on 2007-10-07 10:18 php一道 阅读(1599) 评论(0)  编辑 收藏 引用 网摘

只有注册用户登录后才能发表评论。
网站导航: