{
if($this->_isfull())
{
switch($this->mask_position)
{
case 1:
// 左上
$this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
$this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
break;
case 2:
// 左下
$this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
$this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
break;
case 3:
// 右上
$this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
$this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
break;
case 4:
// 右下
$this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
$this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
break;
default:
// 默认将水印放到右下,偏移指定像素
$this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x;
$this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y;
break;
}
}
else
{
switch($this->mask_position)
{
case 1:
// 左上
$this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
$this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
break;
case 2:
// 左下
$this->mask_pos_x = $this->mask_offset_x + $this->img_border_size;
$this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
break;
case 3:
// 右上
$this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
$this->mask_pos_y = $this->mask_offset_y + $this->img_border_size;
break;
case 4:
// 右下
$this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
$this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
break;
default:
// 默认将水印放到右下,偏移指定像素
$this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size;
$this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size;
break;
}
}
}
/**
* 设置字体信息
*/
function _setfontinfo()
{
if(is_numeric($this->font))
{
$this->font_w = imagefontwidth($this->font);
$this->font_h = imagefontheight($this->font);
// 计算水印字体所占宽高
$word_length = strlen($this->mask_word);
$this->mask_w = $this->font_w*$word_length;
$this->mask_h = $this->font_h;
}
else
{
$arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word);
$this->mask_w = abs($arr[0] - $arr[2]);
$this->mask_h = abs($arr[7] - $arr[1]);
}
}
/**
* 设置新图尺寸
*
* @param integer $img_w 目标宽度
* @param integer $img_h 目标高度
*/
function _setnewimgsize($img_w, $img_h=null)
{
$num = func_num_args();
if(1 == $num)
{
$this->img_scale = $img_w;// 宽度作为比例
$this->fill_w = round($this->src_w * $this->img_scale / 100) - $this->img_border_size*2;
$this->fill_h = round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2;
// 源文件起始坐标
$this->src_x = 0;
$this->src_y = 0;
$this->copy_w = $this->src_w;
$this->copy_h = $this->src_h;
// 目标尺寸
$this->dst_w = $this->fill_w + $this->img_border_size*2;
$this->dst_h = $this->fill_h + $this->img_border_size*2;
}
if(2 == $num)
{
$fill_w = (int)$img_w - $this->img_border_size*2;
$fill_h = (int)$img_h - $this->img_border_size*2;
if($fill_w {
die(图片边框过大,已超过了图片的宽度);
}
$rate_w = $this->src_w/$fill_w;
$rate_h = $this->src_h/$fill_h;
switch($this->cut_type)
{
case 0:
// 如果原图大于缩略图,产生缩小,否则不缩小
if($rate_w {
$this->fill_w = (int)$this->src_w;
$this->fill_h = (int)$this->src_h;
}
else
{
if($rate_w >= $rate_h)
{
$this->fill_w = (int)$fill_w;
$this->fill_h = round($this->src_h/$rate_w);
}
else
{
$this->fill_w = round($this->src_w/$rate_h);
$this->fill_h = (int)$fill_h;
}
}
$this->src_x = 0;
$this->src_y = 0;
$this->copy_w = $this->src_w;
$this->copy_h = $this->src_h;
// 目标尺寸
$this->dst_w = $this->fill_w + $this->img_border_size*2;
$this->dst_h = $this->fill_h + $this->img_border_size*2 - 10 ;//补偿round误差 消除白边;
break;
// 自动裁切
case 1:
// 如果图片是缩小剪切才进行操作
if($rate_w >= 1 && $rate_h >=1)
{
if($this->src_w > $this->src_h)
{
$src_x = round($this->src_w-$this->src_h)/2;
$this->setsrccutposition($src_x, 0);
$this->setrectanglecut($fill_h, $fill_h);
$this->copy_w = $this->src_h;
$this->copy_h = $this->src_h;
}
elseif($this->src_w src_h)
{
$src_y = round($this->src_h-$this->src_w)/2;
$this->setsrccutposition(0, $src_y);
$this->setrectanglecut($fill_w, $fill_h);
$this->copy_w = $this->src_w;
$this->copy_h = $this->src_w;
}
else
{
$this->setsrccutposition(0, 0);
$this->copy_w = $this->src_w;
$this->copy_h = $this->src_w;
$this->setrectanglecut($fill_w, $fill_h);
}
}
else
{
$this->setsrccutposition(0, 0);
$this->setrectanglecut($this->src_w, $this->src_h);
$this->copy_w = $this->src_w;
$this->copy_h = $this->src_h;
}
// 目标尺寸
$this->dst_w = $this->fill_w + $this->img_border_size*2;
$this->dst_h = $this->fill_h + $this->img_border_size*2;
break;
// 手工裁切
case 2:
$this->copy_w = $this->fill_w;
$this->copy_h = $this->fill_h;
// 目标尺寸
$this->dst_w = $this->fill_w + $this->img_border_size*2;
$this->dst_h = $this->fill_h + $this->img_border_size*2;
break;
default:
break;
}
}
// 目标文件起始坐标
$this->start_x = $this->img_border_size;
$this->start_y = $this->img_border_size;
}
/**
* 检查水印图是否大于生成后的图片宽高
*/
function _isfull()
{
return ( $this->mask_w + $this->mask_offset_x > $this->fill_w
|| $this->mask_h + $this->mask_offset_y > $this->fill_h)
?true:false;
}
/**
* 检查水印图是否超过原图
*/
function _checkmaskvalid()
{
if( $this->mask_w + $this->mask_offset_x > $this->src_w
|| $this->mask_h + $this->mask_offset_y > $this->src_h)
{
die(水印图片尺寸大于原图,请缩小水印图);
}
}
/**
* 取得图片类型
*
* @param string $file_path 文件路径
*/
function _getimgtype($file_path)
{
$type_list = array(1=>gif,2=>jpg,3=>png,4=>swf,5 => psd,6=>bmp,15=>wbmp);
if(file_exists($file_path))
{
$img_info = @getimagesize ($file_path);
if(isset($type_list[$img_info[2]]))
{
return $type_list[$img_info[2]];
}
}
else
{
die(文件不存在,不能取得文件类型!);
}
}
/**
* 检查图片类型是否合法,调用了array_key_exists函数,此函数要求
* php版本大于4.1.0
*
* @param string $img_type 文件类型
*/
function _checkvalid($img_type)
{
if(!array_key_exists($img_type, $this->all_type))
{
return false;
}
}
/**
* 按指定路径生成目录
*
* @param string $path 路径
*/
function _mkdirs($path)
{
$adir = explode('/',$path);
$dirlist = '';
$rootdir = array_shift($adir);
if(($rootdir!='.'||$rootdir!='..')&&!file_exists($rootdir))
{
@mkdir($rootdir);
}
foreach($adir as $key=>$val)
{
if($val!='.'&&$val!='..')
{
$dirlist .= /.$val;
$dirpath = $rootdir.$dirlist;
if(!file_exists($dirpath))
{
@mkdir($dirpath);
@chmod($dirpath,0777);
}
}
}
}
/**
* 垂直翻转
*
* @param string $src 图片源
*/
function _flipv($src)
{
$src_x = $this->getimgwidth($src);
$src_y = $this->getimgheight($src);
$new_im = imagecreatetruecolor($src_x, $src_y);
for ($y = 0; $y {
imagecopy($new_im, $src, 0, $src_y - $y - 1, 0, $y, $src_x, 1);
}
$this->h_src = $new_im;
}
/**
* 水平翻转
*
* @param string $src 图片源
*/
function _fliph($src)
{
$src_x = $this->getimgwidth($src);
$src_y = $this->getimgheight($src);
$new_im = imagecreatetruecolor($src_x, $src_y);
for ($x = 0; $x {
imagecopy($new_im, $src, $src_x - $x - 1, 0, $x, 0, 1, $src_y);
}
$this->h_src = $new_im;
}
}
?>
下面来看使用方法:
require_once('lib/imagehandler.phpcls');
$imghandler_obj = new imagehandler();
/* 默认缩小放大 */
$imghandler_obj->setsrcimg(img/xrk.jpg);
$imghandler_obj->setdstimg(tmp/new_image_0.jpg);
$imghandler_obj->setcuttype(0);
// 指定缩放比例
$imghandler_obj->createimg(20);//50 100 200 试试
/* 1, 文字水印图片缩小
$imghandler_obj->setsrcimg(img/xrk.jpg);
$imghandler_obj->setmaskfont(fonts/stxingka.ttf);
$imghandler_obj->setmaskfontsize(15);
$imghandler_obj->setmaskfontcolor(#0061a7);
$imghandler_obj->setmasktxtpct(20);
$imghandler_obj->setdstimgborder(0,#dddddd);
$imghandler_objext = happy漫步者;
//$str = mb_convert_encoding($imghandler_objext,utf-8,gbk);
$imghandler_obj->setmaskword($imghandler_objext);
// 指定固定宽高 输出到浏览器
$imghandler_obj->createimg(400,400);
*/
/* 2, 加水印图片缩小
$imghandler_obj->setsrcimg(img/test.jpg);
$imghandler_obj->setdstimg(tmp/new_image_2.jpg);
$imghandler_obj->setmaskimg(img/t.gif);
$imghandler_obj->setmaskposition(0);//0为在图片右下角加入水印
$imghandler_obj->setmaskimgpct(60);
$imghandler_obj->setdstimgborder(1,#dddddd);
// 指定缩放比例
$imghandler_obj->createimg(400,400);
*/
/* 3, 同步缩小
$imghandler_obj->setsrcimg(img/test.jpg);
$imghandler_obj->setcuttype(0);//同步缩小
$imghandler_obj->setdstimg(tmp/new_image_3.jpg);
$imghandler_obj->createimg(150,160);
*/
/* 4, 剪裁图片
$imghandler_obj->setsrcimg(img/test.jpg);
$imghandler_obj->setcuttype(2);//指明为手工裁切
$imghandler_obj->setsrccutposition(50, 50);// 源图起点坐标
$imghandler_obj->setrectanglecut(300, 200);// 裁切尺寸
$imghandler_obj->setdstimg(tmp/new_image_4.jpg);
$imghandler_obj->createimg(300,200);
*/
