PHP图片处理封装类
作者:leolu 日期:2010-3-25 1:14:14
本代码是一款php5.x下的图片处理封装类,实现了图片处理的很多方便的功能,值得收藏!
<?
/*
图片处理类功能列表:
1. 生成固定大小的略缩图(JPG/GIF/PNG),图像比例不正确会失真。
2. 生成固定大小的略缩图,并保证不失真(保证大图按比例缩小,不足宽高则用指定背景颜色填充)
3. 生成指定宽或者高的略缩图,并保证不失真。(保证图像比例,剪切图像中间部分填满略缩图)
4. 略缩图可加指定颜色的边框。
5. 文字水印,可调整位置。
6. PNG透明图像水印,可调整位置。
环境:php5.x / gd2.0.x
*/
class Pic{
//构造函数
function __construct($ThumbAry) {
$this->SourcePicFile = $ThumbAry['SourcePicFile'];
$this->AimPicFile = $ThumbAry['AimPicFile'];
$this->Width = $ThumbAry['Width'];
$this->Height = $ThumbAry['Height'];
$this->Background = $ThumbAry['Background']=='' ? '#ffffff' : $ThumbAry['Background'];
$this->PaiBorderColor= $ThumbAry['PaiBorderColor'];
//水印类配置
if(is_array($ThumbAry['Mark'])){
$this->MarkText = $ThumbAry['Mark']['MarkText'];
$this->MarkTextSize = $ThumbAry['Mark']['MarkTextSize'];
$this->MarkColor = $ThumbAry['Mark']['MarkColor'];
$this->MarkBack = $ThumbAry['Mark']['MarkBack'];
$this->MarkAlpha = $ThumbAry['Mark']['MarkAlpha'];
$this->MarkTTF = $ThumbAry['Mark']['MarkTTF'];
$this->MarkPicFile = $ThumbAry['Mark']['MarkPicFile'];
$this->MarkLocation = $ThumbAry['Mark']['MarkLocation'];
}
$this->PicDetails();
}
//获取以及检验文件基本信息
function PicDetails(){
//判断文件是否存在
if(is_file($this->SourcePicFile) === false){
$this->err(__FUNCTION__,'101');
}
//获取图片宽高以及类型, 并且检查是否为一个正常的图片文件。
$Info = getimagesize($this->SourcePicFile);
list($this->w,$this->h,$this->p) = $Info;
if($this->w < 1){
$this->err(__FUNCTION__,'102');
}
//检验图片类型 1= gif 2=jpg 3=png
switch($this->p){
default:
$this->err(__FUNCTION__,'103');
break;
case "1": //gif
$this->type = 'gif';
break;
case "2": //jpg
$this->type = 'jpg';
break;
case "3": //png
$this->type = 'png';
break;
}
}
/*
文字水印
*/
function MarkText(){
if(is_file($this->MarkTTF)===false){
$this->err(__FUNCTION__,'201');
}
//读取图像源
$Image = imagecreatefromjpeg($this->SourcePicFile);
$Color = $this->Color2RGB($this->MarkColor);
$grey = imagecolorallocatealpha ($Image, $Color['r'], $Color['g'], $Color['b'],0);
//获取文字水印的8个边角坐标
$Size = imagettfbbox($this->MarkTextSize, 0, $this->MarkTTF, iconv('gb2312','utf-8',$this->MarkText));
$TextWidth = $Size[4];
$TextHeight = abs($Size[7]);
//确定水印的XY轴
$ary = $this->GetMarkLocation($TextWidth, $TextHeight , $Image);
list($Mark_X,$Mark_Y) = $ary;
//做一个有透明度的背景
$Image = $this->MarkBox($TextWidth, $TextHeight, $Image, $Mark_X, $Mark_Y);
//将文字写入图片
imagettftext($Image, $this->MarkTextSize, 0, $Mark_X, $Mark_Y, $grey, $this->MarkTTF, iconv('gb2312','utf-8',$this->MarkText));
//保存水印后的图像文件
$this->SavePicFile($Image);
}
/*
创造一个指定宽高的背景,主要用于文字位置
*/
function MarkBox($MarkWidth, $MarkHeight, $Image, $x, $y){
//宽高来源于文字水印的8个边角
$w = $MarkWidth + 5;
$h = $MarkHeight + 5;
$y = $y - $h + 5;
//创建图像,并赋予半透明的背景颜色
$Box = imagecreatetruecolor($w, $h);
$rgb = $this->Color2RGB($this->MarkBack);
$background = imagecolorallocate($Box, $rgb['r'], $rgb['g'], $rgb['b']);
imagefill($Box, 0, 0, $background);
//合并图片
imagecopymerge($Image, $Box, $x, $y, 0, 0, $w, $h, $this->MarkAlpha);
imagedestroy($Box);
return $Image;
}
/*
图片水印
*/
function MarkPic(){
if(is_file($this->MarkPicFile)===false){
$this->err(__FUNCTION__,'202');
}
$Mark = imagecreatefrompng($this->MarkPicFile);
$w = imagesx($Mark);
$h = imagesy($Mark);
$Image = imagecreatefromjpeg($this->SourcePicFile);
/*为了保持PNG的透明效果 使用imagecopy*/
$ary = $this->GetMarkLocation($w, $h , $Image, 1);
list($Mark_X,$Mark_Y) = $ary;
imagecopy($Image, $Mark, $Mark_X, $Mark_Y, 0, 0, $w, $h);
//imagecopyresampled($Image, $Image, 0,0,0,0, $this->W, $this->H,$this->W, $this->H );
imagedestroy($Mark);
$this->SavePicFile($Image);
}
/*
返回水印位置,1-9对应9个位置 0表示随机位置
$MarkWidth 为水印宽 / $MarkHeight 为水印高 $Image 为图像源 $IsText 0=文字 1=图片
*/
function GetMarkLocation($MarkWidth, $MarkHeight, $Image, $IsText=0){
$w = imagesx($Image);
$h = imagesy($Image);
//水印长宽的一半值,为了让水印绝对居中
$MarkHalf_W = round($MarkWidth / 2, 2);
$MarkHalf_H = round($MarkHeight / 2, 2);
//左 中 右的坐标
$Left = 1;
$Center = $w / 2 - $MarkHalf_W;
$Right = $w - $MarkWidth;
//上中下的坐标
$Top = $IsText==0 ? $MarkHeight : 1;
$Middle = $IsText==0 ? ceil($h / 2) + $MarkHalf_H : ceil($h / 2) - $MarkHalf_H;
$Bottom = $IsText==0 ? $h-5 : $h - $MarkHeight;
//MarkLocation 参数为0 的话则表示随机位置
if($this->MarkLocation==0){
$MarkLocation = mt_rand(1,9);
}else{
$MarkLocation = $this->MarkLocation;
}
switch($MarkLocation){
case "1": //左上
$ary[0] = $Left;
$ary[1] = $Top;
break;
case "2": //左中
$ary[0] = $Left;
$ary[1] = $Middle;
break;
case "3": //左下
$ary[0] = $Left;
$ary[1] = $Bottom;
break;
case "4": //中上
$ary[0] = $Center;
$ary[1] = $Top;
break;
case "5": //中中
$ary[0] = $Center;
$ary[1] = $Middle;
break;
case "6": //中下
$ary[0] = $Center;
$ary[1] = $Bottom;
break;
case "7": //右上
$ary[0] = $Right;
$ary[1] = $Top;
break;
case "8": //右中
$ary[0] = $Right;
$ary[1] = $Middle;
break;
case "9": //右下
$ary[0] = $Right;
$ary[1] = $Bottom;
break;
}
return $ary;
}
/*
生成标准略缩图,直接压缩
$Width = 略缩图宽,为空则以初始定义为准
$Height = 略缩图高,为空则以初始定义为准
$IsFile = 0 输出文件 =1 则返回略缩图的图像源。
$SourcePic = 0 则读取原图,作为图像源,否则请指定一个图像源 $im。
*/
function Thumb($Width=0, $Height=0, $IsFile=0, $SourcePic=0){
$Width = $Width==0 ? $this->Width : $Width;
$Height = $Height==0 ? $this->Height : $Height;
$IsSource = $SourcePic == 0 ? 0 : 1;
switch($this->p){
case "1":
$ThumbPic = imagecreatetruecolor($Width, $Height);
$SourcePic = $SourcePic == 0 ? imagecreatefromgif($this->SourcePicFile) : $SourcePic;
break;
case "2":
$ThumbPic = imagecreatetruecolor($Width, $Height);
$SourcePic = $SourcePic == 0 ? imagecreatefromjpeg($this->SourcePicFile) : $SourcePic;
break;
case "3":
$ThumbPic = imagecreatetruecolor($Width, $Height);
$SourcePic = $SourcePic == 0 ? imagecreatefrompng($this->SourcePicFile) : $SourcePic;
break;
}
if($IsSource==0){ //直接生成略缩图,不考虑是否失真问题
$is = imagecopyresampled($ThumbPic, $SourcePic, 0, 0, 0, 0, $Width, $Height, $this->w, $this->h);
}elseif($IsSource==1){ //ThumbPro使用,拷贝原图的一部分到背景
$rgb = $this->Color2RGB($this->Background);
$background = imagecolorallocate($ThumbPic, $rgb['r'], $rgb['g'], $rgb['b']);
//填充背景颜色
imagefill($ThumbPic, 0, 0, $background);
//拷贝图片到背景
$is = imagecopymerge($ThumbPic, $SourcePic, $this->Pro_X, $this->Pro_Y, 0, 0, $this->Pro_W, $this->Pro_H, 100);
}
//是否给略缩图画边框
if($this->PaiBorderColor != '' && $IsFile==0){
$rgb = $this->Color2RGB($this->PaiBorderColor);
$Color = imagecolorallocate($ThumbPic, $rgb['r'], $rgb['g'], $rgb['b']);
$ThumbPic = $this->MakeBorder($ThumbPic,$this->Width, $this->Height, $Color);
}
if($is===false){
$this->err(__FUNCTION__,104);
}
if($IsFile==0){
imagedestroy($SourcePic);
$this->SavePicFile($ThumbPic);
}elseif($IsFile==1){
return $ThumbPic;
}
}
/*
生成不失真的略缩图,可以指定Background 参数作为背景色,默认为白色。
$IsPro = 0 保证图片全显,背景图留空
$IsPro = 1 取图片的一部分,保证背景图填满
$IsFile = 0 直接输出略缩图文件 、 =1 则返回一个图像源
*/
function ThumbPro($IsPro=0, $IsFile=0){
//计算比例
$ProW = round($this->w / $this->Width,2);
$ProH = round($this->h / $this->Height,2);
$this->Pro_W = round($this->w / $ProH, 2);
$this->Pro_H = round($this->h / $ProW, 2);
$this->Pro_X = round(($this->Width - $this->Pro_W) / 2, 2);
$this->Pro_Y = round(($this->Height - $this->Pro_H) / 2, 2);
if($ProW < $ProH){
if($IsPro==0){
$this->Pro_H = $this->Height;
$this->Pro_Y = 0;
}elseif($IsPro==1){
$this->Pro_W = $this->Width;
$this->Pro_X = 0;
}
$Image = $this->Thumb($this->Pro_W, $this->Pro_H, 1);
}elseif($ProW > $ProH){
if($IsPro==0){
$this->Pro_W = $this->Width;
$this->Pro_X = 0;
}elseif($IsPro==1){
$this->Pro_H = $this->Height;
$this->Pro_Y = 0;
}
$Image = $this->Thumb($this->Pro_W, $this->Pro_H, 1);
}else{
$Image = $this->Thumb($this->Width, $this->Height, 1);
}
$Image = $this->Thumb(0, 0, $IsFile, $Image);
if($IsFile==1){
return $Image;
}else{
return true;
}
}
/*
为指定图像画一个边框线
*/
function MakeBorder($image, $w, $h, $color){
imagerectangle($image, 0, 0, $w-1, $h-1, $color);
return $image;
}
/*
根据指定的图像源,保存图像。
*/
function SavePicFile($image){
switch($this->p){
case "1":
$IsSave = imagegif($image, $this->AimPicFile);
break;
case "2":
$IsSave = imagejpeg($image, $this->AimPicFile, 100);
break;
case "3":
$IsSave = imagepng($image, $this->AimPicFile);
break;
}
imagedestroy($image);
if($IsSave===false){
$this->err(__FUNCTION__, '105');
}
echo "<img src='".$this->AimPicFile."'><p>";
}
//16进制的颜色转为RGB颜色代码
function Color2RGB($hexColor) {
$color = str_replace('#', '', $hexColor);
if (strlen($color) > 3) {
$rgb = array(
'r' => hexdec(substr($color, 0, 2)),
'g' => hexdec(substr($color, 2, 2)),
'b' => hexdec(substr($color, 4, 2))
);
} else {
$color = str_replace('#', '', $hexColor);
$r = substr($color, 0, 1) . substr($color, 0, 1);
$g = substr($color, 1, 1) . substr($color, 1, 1);
$b = substr($color, 2, 1) . substr($color, 2, 1);
$rgb = array(
'r' => hexdec($r),
'g' => hexdec($g),
'b' => hexdec($b)
);
}
return $rgb;
}
//抛出错误方法以及错误码
function err($fun, $code){
/*
101 = 不是一个正常文件
102 = 获取图像数据出错,可能文件不是一个图片
103 = 非指定的图片类型 1 = GIF,2 = JPG,3 = PNG
104 = 生成略缩图时失败
105 = 保存图像时失败
201 = 字体文件不存在
202 = 水印文件不存在
*/
throw new Exception($fun, $code);
}
}
/*
实例
*/
$dir = 'd:/web7/www/code/thumb/pic/';
//配置数组
$ThumbAry = array(
'SourcePicFile' => $dir.'a2.jpg', //来源图像
'AimPicFile' => $dir.'a2.jpg.jpg', //操作后图像
'Width' => 200, //略缩图宽
'Height' => 200, //略缩图高
'Background' => '#cccfff', //背景空白处的填充颜色
'PaiBorderColor' => '#000000', //略缩图边框颜色,为空则无边框
'Mark' => array( //水印用的配置
'MarkText' => "Http://www.baidu.com _ BAIDU中文站", //水印文字
'MarkTextSize' => 20, //文字大小
'MarkColor' => '#000000', //文字颜色
'MarkBack' => '#ffffff', //背景颜色
'MarkAlpha' => 50, //背景透明度
'MarkTTF' => 'd:/web7/www/code/thumb/font/汉仪柏青体简.ttf', //字体文件
'MarkPicFile' =>'mark/mark.png', //图片水印地址
'MarkLocation' => 0, //水印位置
//1=左上 2=左中 3=左下 4=中上 5=中中 6=中下 7=右上 8=右中 9=右下 0=随机
),
);
try{
$p = new Pic($ThumbAry);
/*
直接生成一个可能失真的略缩图
必须配置 SourcePicFile / AimPicFile / Width / Height
可选配置 PaiBorderColor
*/
//$p->Thumb();
/*
生成一个不会失真的略缩图
必须配置 SourcePicFile / AimPicFile / Width / Height
可选配置 Background / PaiBorderColor
附加参数
0 = 保证显示全图,不足宽高之处以Background填充
1 = 保证填充宽高,图片多余部分丢弃(裁剪原图中间部分)
*/
//$p->ThumbPro(0);
/*
生成一个文字水印,比较啰嗦
主要是生成一个带背景的文字框,需要iconv函数支持,可以随即定义位置。
必须配置 SourcePicFile / AimPicFile / mark{MarkText/MarkTextSize/MarkColor/MarkTTF/MarkLocation}
可选:MarkBack / MarkAlpha
*/
//$p->MarkText();
/*
图片水印必须配置 SourcePicFile / AimPicFile / mark{MarkPicFile/MarkLocation}
*/
$p->MarkPic();
}
//抛出错误码的后续处理
catch(Exception $e){
echo '错误码:'.$e->getCode();
echo ' / 所在方法:'.$e->getMessage();
}
?>
本文原标题:PHP图片处理封装类
请转载的朋友加上本文的链接地址:)
我的博客地址[http://blog.csscss.org/]
Tags: php
相关日志:
上一篇
下一篇