php给图片加水印六

?

复制代码 代码如下:setSrcImg;
$t->setCutType;//这一句就OK了 $t->setDstImg; $t->createImg;
手工业裁切: 程序会依据钦点的岗位从源图上取图 $t->setSrcImg;
$t->setCutType;//指明为手工业裁切 $t->setSrcCutPosition;//
源图起源坐标 $t->setRectangleCut;// 裁切尺寸 $t->setDstImg;
$t->createImg; */ class ThumbHandler { var $dst_img;// 目的文件 var
$h_src; // 图片能源句柄 var $h_dst;// 新图句柄 var $h_mask;//
水印句柄 var $img_create_quality = 100;// 图片生成质量 var
$img_display_quality = 80;// 图片显示品质,默以为75 var $img_scale =
0;// 图片缩放比例 var $src_w = 0;// 原图宽度 var $src_h = 0;//
原图中度 var $dst_w = 0;// 新图总宽度 var $dst_h = 0;// 新图总高度 var
$fill_w;// 填充图形宽 var $fill_h;// 填充图形高 var $copy_w;//
拷贝图形宽 var $copy_h;// 拷贝图形高 var $src_x = 0;//
原图绘制起头横坐标 var $src_y = 0;// 原图绘制初始纵坐标 var
$start_x;// 新图绘制初叶横坐标 var $start_y;// 新图绘制开始纵坐标 var
$mask_word;// 水印文字 var $mask_img;// 水印图片 var $mask_pos_x =
0;// 水印横坐标 var $mask_pos_y = 0;// 水印纵坐标 var $mask_offset_x
= 5;// 水印横向偏移 var $mask_offset_y = 5;// 水印纵向偏移 var
$font_w;// 水印字体宽 var $font_h;// 水印字体高 var $mask_w;// 水印宽
var $mask_h;// 水印高 var $mask_font_color = “#ffffff”;//
水印文字颜色 var $mask_font = 2;// 水印字体 var $font_size;// 尺寸 var
$mask_position = 0;// 水印地点 var $mask_img_pct = 50;//
图片归拢程度,值越大,归总程序越低 var $mask_txt_pct = 50;//
文字统生龙活虎程度,值越小,合併程序越低 var $img_border_size = 0;//
图片边框尺寸 var $img_border_color;// 图片边框颜色 var $_flip_x=0;//
水平翻转次数 var $_flip_y=0;// 垂直翻转次数 var $cut_type=0;//
剪切类型 var $img_type;// 文件类型 //
文件类型定义,并提议了出口图片的函数 var $all_type = array( “jpg” =>
array(“output”=>”imagejpeg”), “gif” => array, “png” => array,
“wbmp” => array(“output”=>”image2wbmp”), “jpeg” =>
array(“output”=>”imagejpeg”)); /** * 结构函数 */ function
ThumbHandler() { $this->mask_font_color = “#ffffff”;
$this->font = 2; $this->font_size = 12; } /** * 获得图片的宽
*/ function getImgWidth { return imagesx; } /** * 拿到图片的高 */
function getImgHeight { return imagesy; } /** * 设置图片生成路线 *
* @param string $src_img 图片生成路线 */ function
setSrcImg($src_img, $img_type=null) { if(!file_exists { die; } if {
$this->img_type = $img_type; } else { $this->img_type =
$this->_getImgType; } $this->_checkValid; //
file_get_contents函数要求php版本>4.3.0 $src = ”;
if(function_exists { $src = file_get_contents; } else { $handle =
fopen ; while { $src .= fgets; } fclose ; } if { die; } $this->h_src
= @ImageCreateFromString; $this->src_w = $this->getImgWidth;
$this->src_h = $this->getImgHeight; } /** * 设置图片生成路线
* * @param string $dst_img 图片生成路线 */ function setDstImg { $arr
= explode; $last = array_pop; $path = implode; $this->_mkdirs;
$this->dst_img = $dst_img; } /** * 设置图片的显得质量 * *
@param string $n 质量 */ function setImgDisplayQuality {
$this->img_display_quality = $n; } /** * 设置图片的成形品质 *
* @param string $n 质量 */ function setImgCreateQuality {
$this->img_create_quality = $n; } /** * 设置文字水印 * *
@param string $word 水印文字 * @param integer $font 水印字体 * @param
string $color 水印字体颜色 */ function setMaskWord {
$this->mask_word .= $word; } /** * 设置字体颜色 * * @param
string $color 字体颜色 */ function setMaskFontColor {
$this->mask_font_color = $color; } /** * 设置水印字体 * *
@param string|integer $font 字体 */ function setMaskFont { if &&
!file_exists { die; } $this->font = $font; } /** *
设置文字字体大小,仅对truetype字体有效 */ function setMaskFontSize {
$this->font_size = $size; } /** * 设置图片水印 * * @param
string $img 水印图片源 */ function setMaskImg { $this->mask_img =
$img; } /** * 设置水印横向偏移 * * @param integer $x 横向偏移量 */
function setMaskOffsetX { $this->mask_offset_x = $x; } /** *
设置水印纵向偏移 * * @param integer $y 纵向偏移量 */ function
setMaskOffsetY { $this->mask_offset_y = $y; } /** * 内定水印地点
* * @param integer $position 位置,1:左上,2:左下,3:右上,0/4:右下 */
function setMaskPosition { $this->mask_position = $position; } /**
* 设置图片归并程度 * * @param integer $n 合并程度 */ function
setMaskImgPct { $this->mask_img_pct = $n; } /** *
设置文字统意气风发程度 * * @param integer $n 归并程度 */ function
setMaskTxtPct { $this->mask_txt_pct = $n; } /** * 设置缩略图边框
* * @param */ function setDstImgBorder($size=1, $color=”#000000″) {
$this->img_border_size = $size; $this->img_border_color =
$color; } /** * 水平翻转 */ function flipH() {
$this->_flip_x++; } /** * 垂直翻转 */ function flipV() {
$this->_flip_y++; } /** * 设置剪切类型 * * @param */ function
setCutType { $this->cut_type = $type; } /** * 设置图片剪切 * *
@param integer $width 矩形剪切 */ function setRectangleCut {
$this->fill_w = $width; $this->fill_h = $height; } /** *
设置源图剪切初始坐标点 * * @param */ function setSrcCutPosition {
$this->src_x = $x; $this->src_y = $y; } /** * 创造图片,主函数
* @param integer $a 当贫乏第四个参数时,此参数将用作百分比, *
不然作为宽度值 * @param integer $b 图片缩放后的冲天 */ function
createImg { $num = func_num_args { $r = { die; } $this->img_scale
= $r; $this->_setNewImgSize { $w = $b; if { die; } if { die; }
$this->_setNewImgSize; } if { $this->_flipH; } if {
$this->_flipV; } $this->_createMask; // 释放 if(imagedestroy &&
imagedestroy { Return true; } else { Return false; } } /** *
生成水印,调用了变动水印文字和水印图片三个法子 */ function
_createMask(卡塔尔国 { if { // 获取字体消息 $this->_setFontInfo(); if {
die; } else { $this->h_dst = imagecreatetruecolor($this->dst_w,
$this->dst_h); $white =
ImageColorAllocate($this->h_dst,255,255,255);
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$whiteState of Qatar;//
填充背景象 $this->_drawBorder(); imagecopyresampled(
$this->h_dst, $this->h_src, $this->start_x,
$this->start_y, $this->src_x, $this->src_y,
$this->fill_w, $this->fill_h, $this->copy_w,
$this->copy_h); $this->_createMaskWord; } } if {
$this->_loadMaskImg(卡塔尔(قطر‎;//加载时,获得宽高 if { //
将水印生成在原图上再拷 $this->_createMaskImg; $this->h_dst =
imagecreatetruecolor($this->dst_w, $this->dst_h); $white =
ImageColorAllocate($this->h_dst,255,255,255);
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$whiteState of Qatar;//
填充背景象 $this->_drawBorder(); imagecopyresampled(
$this->h_dst, $this->h_src, $this->start_x,
$this->start_y, $this->src_x, $this->src_y,
$this->fill_w, $this->start_y, $this->copy_w,
$this->copy_hState of Qatar; } else { // 创制新图并拷贝 $this->h_dst =
imagecreatetruecolor($this->dst_w, $this->dst_h); $white =
ImageColorAllocate($this->h_dst,255,255,255);
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$whiteState of Qatar;//
填充背景象 $this->_drawBorder(); imagecopyresampled(
$this->h_dst, $this->h_src, $this->start_x,
$this->start_y, $this->src_x, $this->src_y,
$this->fill_w, $this->fill_h, $this->copy_w,
$this->copy_h); $this->_createMaskImg; } } if(empty && empty {
$this->h_dst = imagecreatetruecolor($this->dst_w,
$this->dst_h); $white =
ImageColorAllocate($this->h_dst,255,255,255);
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white卡塔尔;//
填充背景观 $this->_drawBorder(); imagecopyresampled(
$this->h_dst, $this->h_src, $this->start_x,
$this->start_y, $this->src_x, $this->src_y,
$this->fill_w, $this->fill_h, $this->copy_w,
$this->copy_h); } } /** * 画边框 */ function _drawBorder() {
if(!empty($this->img_border_size)) { $c =
$this->_parseColor($this->img_border_color); $color =
ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]);
imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$colorState of Qatar;//
填充背景象 } } /** * 生成水印文字 */ function _createMaskWord {
$this->_countMaskPos(); $this->_checkMaskValid(); $c =
$this->_parseColor($this->mask_font_color); $color =
imagecolorallocatealpha($src, $c[0], $c[1], $c[2],
$this->mask_txt_pct); if(is_numeric { imagestring($src,
$this->font, $this->mask_pos_x, $this->mask_pos_y,
$this->mask_word, $color); } else { imagettftext($src,
$this->font_size, 0, $this->mask_pos_x, $this->mask_pos_y,
$color, $this->font, $this->mask_word); } } /** * 生成水印图
*/ function _createMaskImg { $this->_countMaskPos();
$this->_checkMaskValid(); imagecopymerge($src, $this->h_mask,
$this->mask_pos_x ,$this->mask_pos_y, 0, 0, $this->mask_w,
$this->mask_h, $this->mask_img_pct); imagedestroy; } /** *
加载水印图 */ function _loadMaskImg() { $mask_type =
$this->_getImgType; $this->_checkValid; //
file_get_contents函数必要php版本>4.3.0 $src = ”;
if(function_exists { $src = file_get_contents; } else { $handle =
fopen ; while { $src .= fgets; } fclose ; } if(empty { die; }
$this->h_mask = ImageCreateFromString; $this->mask_w =
$this->getImgWidth; $this->mask_h = $this->getImgHeight; }
/** * 图片输出 */ function _output() { $img_type =
$this->img_type; $func_name =
$this->all_type[$img_type][‘output’]; if(function_exists { //
决断浏览器,就算IE就不发送头 if(isset($_SERVER[‘HTTP_USER_AGENT’]))
{ $ua = strtoupper($_SERVER[‘HTTP_USER_AGENT’]);
if(!preg_match$/i’,$ua)) { header(“Content-type:$img_type”); } }
$func_name($this->h_dst, $this->dst_img,
$this->img_display_quality); } else { Return false; } } /** *
解析颜色 * * @param string $color 十一进制颜色 */ function
_parseColor { $arr = array(); for($ii=1; $ii_isFull { switch { 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 { 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_w =
imagefontwidth; $this->font_h = imagefontheight; //
计算水印字体所占宽高 $word_length = strlen; $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;
$this->mask_h = abs; } } /** * 设置新图尺寸 * * @param integer
$img_w 目的宽度 * @param integer $img_h 指标中度 */ function
_setNewImgSize { $num = func_num_args { $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 { $fill_w = $img_w – $this->img_border_size*2; $fill_h =
$img_h – $this->img_border_size*2; if($fill_w < 0 || $fill_h
< 0) { die; } $rate_w = $this->src_w/$fill_w; $rate_h =
$this->src_h/$fill_h; switch { case 0: //
假使原图大于缩略图,爆发减少,不然不减少 if($rate_w < 1 && $rate_h
< 1) { $this->fill_w = $this->src_w; $this->fill_h =
$this->src_h; } else { if { $this->fill_w = $fill_w;
$this->fill_h = round; } else { $this->fill_w = round;
$this->fill_h = $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; 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;
$this->setRectangleCut; $this->copy_w = $this->src_h;
$this->copy_h = $this->src_h; } elseif($this->src_w <
$this->src_h) { $src_y =
round($this->src_h-$this->src_w)/2; $this->setSrcCutPosition;
$this->setRectangleCut; $this->copy_w = $this->src_w;
$this->copy_h = $this->src_w; } else {
$this->setSrcCutPosition; $this->copy_w = $this->src_w;
$this->copy_h = $this->src_w; $this->setRectangleCut; } }
else { $this->setSrcCutPosition;
$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 { $type_list =
array(“1″=>”gif”,”2″=>”jpg”,”3″=>”png”,”4″=>”swf”,”5″ =>
“psd”,”6″=>”bmp”,”15″=>”wbmp”); if(file_exists { $img_info =
@getimagesize ; 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 { if(!array_key_exists($img_type, $this->all_type))
{ Return false; } } /** * 按钦点路线生成目录 * * @param string
$path 路径 */ function _mkdirs { $adir = explode; $dirlist = ”;
$rootdir = array_shift;
if(($rootdir!=’.’||$rootdir!=’..’)&&!file_exists { @mkdir; } foreach {
if($val!=’.’&&$val!=’..’) { $dirlist .= “/”.$val; $dirpath =
$rootdir.$dirlist; if(!file_exists { @mkdir; @chmod; } } } } /** *
垂直翻转 * * @param string $src 图片源 */ function _flipV { $src_x
= $this->getImgWidth; $src_y = $this->getImgHeight; $new_im =
imagecreatetruecolor; for ($y = 0; $y < $src_y; $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_x = $this->getImgWidth;
$src_y = $this->getImgHeight; $new_im = imagecreatetruecolor; for
($x = 0; $x < $src_x; $x++) { imagecopy($new_im, $src, $src_x – $x

function _countMaskPos() { 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

  • 1, 0, $x, 0, 1, $src_y); } $this->h_src = $new_im; } } ?>
  • $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 0 || $fill_h 0卡塔尔 {
    die(图片边框过大,已当先了图片的小幅度卡塔尔国; } $rate_w =
    $this-src_w/$fill_w; $rate_h = $this-src_h/$fill_h;
    switch($this-cut_type卡塔尔 { case 0: //
    假使原图大于缩略图,发生缩短,不然不减弱 if($rate_w 1 $rate_h 1) {
    $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 $this-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(水印图片尺寸大于原图,请缩短水印图State of Qatar; } } /** * 获得图片类型 * *
    @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 $src_y; $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 $src_x; $x++) {
    imagecopy($new_im, $src, $src_x – $x – 1, 0, $x, 0, 1, $src_y); }
    $this-h_src = $new_im; }}?

函数描述及例子使用实例: 复制代码
代码如下:< ?php require_once(‘lib/thumb.class.php’卡塔尔国; $t = new
ThumbHandler(卡塔尔国; $t->setSrcImg; $t->setDstImg; $t->setMaskImg;
$t->setMaskPosition; $t->setMaskImgPct; $t->setDstImgBorder; //
钦赐缩放比例 $t->createImg; ?> 复制代码 代码如下:setSrcImg;
$t->setMaskWord; $t->setDstImgBorder; // 钦点缩放比例
$t->createImg; ?> 复制代码
代码如下:setSrcImg; $t->setMaskWord; // 钦赐固定宽高
$t->createImg; ?> 复制代码
代码如下:setSrcImg; $t->setDstImg; $t->setMaskWord; //
钦点固定宽高 $t->createImg; ?> 复制代码 代码如下:setSrcImg; //
指定字体文件地方 $t->setMaskFont(“c:/winnt/fonts/arial.ttf”卡塔尔;
$t->setMaskFontSize; $t->setMaskFontColor; $t->setMaskWord;
$t->setDstImgBorder; $t->createImg; ?> 复制代码 代码如下:setSrcImg;
$t->setMaskOffsetX; $t->setMaskOffsetY; $t->setMaskPosition;
//$t->setMaskPosition; //$t->setMaskPosition;
//$t->setMaskPosition; $t->setMaskFontColor; $t->setMaskWord;
// 钦点固定宽高 $t->createImg; ?> 复制代码 代码如下:setSrcImg;
$t->setMaskFont(“c:/winnt/fonts/simyou.ttf”卡塔尔国; $t->setMaskFontSize;
$t->setMaskFontColor; $t->setMaskTxtPct; $t->setDstImgBorder;
$text = “中文”; $str = mb_convert_encoding($text, “UTF-8”, “gb2312″State of Qatar;
$t->setMaskWord; $t->setMaskWord; // 内定固定宽高
$t->createImg; ?>

上边来看使用方式:

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(0State of Qatar;//
钦赐缩放比例$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, 50State of Qatar;// 源图起源坐标
$imgHandler_Obj-setRectangleCut(300, 200卡塔尔国;// 裁切尺寸
$imgHandler_Obj-setDstImg(tmp/New_Image_4.jpg);
$imgHandler_Obj-createImg(300,200);*/

发表评论

电子邮件地址不会被公开。 必填项已用*标注