写文字水印,可选文字颜色、大小、宽度、字体、透明度、位置
图片水印,可以把另一张图片当水印写入
指定文件名保存
可以在浏览器显示(调试用的)
$cutedImageRes = $this ->cutImage($srcImage, $objWidth, $objHeight);
$endImageRes = $this ->writeMark($cutedImageRes);
//$this->showImage($endImageRes);
if ($this ->writeImage($endImageRes, $desImage))
{
return true ;
}
else
{
return false ;
}
}
function writeImage($res, $file)
{
switch ($this ->imageType)
{
case 'image/jpeg' :
{
if (imagejpeg($res, $file))
{
return true ;
}
else
{
return false ;
}
break ;
}
case 'image/gif' :
{
if (imagegif($res, $file))
{
return true ;
}
else
{
return false ;
}
break ;
}
case 'image/png' :
{
if (imagepng($res, $file))
{
return true ;
}
else
{
return false ;
}
break ;
}
}
}
/**
* 在浏览器里显示图片(调试用)
* @access private
* @param string $srcImage
* @param string $desImage
* @param int $objWidth
* @param int $objHeight
* @return bool 返回true or false
*/< br />
function showImage($res)
{
header('Content-Type: ' . $this ->imageType);
switch ($this ->imageType)
{
case 'image/jpeg' :
{
imagejpeg($res);
break ;
}
case 'image/gif' :
{
imagegif($res);
break ;
}
case 'image/png' :
{
imagepng($res);
break ;
}
}
exit;
}
/**
* 裁图
* @access private
* @param string $srcImage
* @param int $objWidth
* @param int $objHeight
* @return res 返回图像资源
*/< br />
function cutImage($srcImage, $objWidth, $objHeight)
{
$imageInfo = getimagesize($srcImage);
$srcImageWidth = $imageInfo[0];
$srcImageHeight = $imageInfo[1];
$this ->imageType = $imageInfo['mime' ];
$srcIM = $this ->readImageFile($srcImage);
if ($this ->zoomType)
{//按比例缩放
if ($objWidth < $objHeight)
{
$mu = $srcImageWidth / $objWidth;
$objHeight = ceil($srcImageHeight / $mu);
}
else
{
$mu = $srcImageHeight / $objHeight;
$objWidth = ceil($srcImageWidth / $mu);
}
$resd = imagecreatetruecolor($objWidth, $objHeight);
imagecopyresized($resd, $srcIM, 0, 0, 0, 0, $objWidth, $objHeight, $srcImageWidth, $srcImageHeight);
}
else
{
if ($objWidth < $objHeight)
{
$mu = $srcImageHeight / $objHeight;
$objWidthT = ceil($srcImageWidth / $mu);
$objHeightT = $objHeight;
}
else
{
$mu = $srcImageWidth / $objWidth;
$objWidthT = $objWidth;
$objHeightT = ceil($srcImageHeight / $mu);
}
$res = imagecreatetruecolor($objWidthT, $objHeightT);
imagecopyresized($res, $srcIM, 0, 0, 0, 0, $objWidthT, $objHeightT, $srcImageWidth, $srcImageHeight);
$resd = imagecreatetruecolor($objWidth, $objHeight);
if ($this ->zoomPos)
{
if ($objWidth < $objHeight)
{
$t = ceil($objWidthT / 2) - ceil($objWidth / 2);
imagecopy($resd, $res, 0, 0, $t, 0, $objWidth, $objHeight);
}
else
{
$t = ceil($objHeightT / 2) - ceil($objHeight / 2);
imagecopy($resd, $res, 0, 0, 0, $t, $objWidth, $objHeight);
}
}
else
{
imagecopy($resd, $res, 0, 0, 0, 0, $objWidth, $objHeight);
}
}
return $resd;
}
/**
* 加入水印
* @access private
* @param obj $res
* @return res 返回图像资源
*/< br />
function writeMark($res)
{
if ($this ->markType == 0)
{
return $res;
}
if ($this ->markType == 1)
{
//此段代码说明
//首先建立一个和最终图大小一样的透明图,并把文字写入到透明图相应位置,然后再把透明图合并到最终图上。这样就可以实现透明度了
//特别说明:因为GD要建立透明图时,是使用的imagecolortransparent函数实现,发现只能背景为黑色时,此函数才有效。
$R = hexdec(substr($this ->markTextColor, 1, 2));
$G = hexdec(substr($this ->markTextColor, 3, 2));
$B = hexdec(substr($this ->markTextColor, 5));
$textEn = iconv(“gb2312″ , “utf-8″ , $this ->markText); //如果你传入的是非UTF8中文,这里要转换
$curFont = $this ->fontArray[mt_rand(0, count($this ->fontArray) - 1)];
$imageW = imagesx($res);
$imageH = imagesy($res);
$dst = imagecreatetruecolor($imageW, $imageH);
$textColor = imagecolorallocate($dst, $R, $G, $B);
$black = imagecolorallocate($dst, 0, 0, 0);
imagecolortransparent($dst, $black);
$temp = imagettfbbox ($this ->markTextSize, 0, $curFont, $textEn); //取得使用 TrueType 字体的文本的范围
$markW = $temp[2] - $temp[6];
$markH = $temp[3] - $temp[7];
$pos = $this ->getMarkPos($res, $markW, $markH);
imagettftext($dst, $this ->markTextSize, 0, $pos['x' ], $pos['y' ], $textColor, $curFont, $textEn);
imagecopymerge($res, $dst, 0, 0, 0, 0, $imageW, $imageH, $this ->markAlpha);
}
if ($this ->markType == 2)
{
$markIM = $this ->readImageFile($this ->markImage);
$markW = imagesx($markIM);
$markH = imagesy($markIM);
$imageW = imagesx($res);
$imageH = imagesy($res);
$pos = $this ->getMarkPos($res, $markW, $markH);
imagecopymerge($res, $markIM, $pos['x' ], $pos['y' ], 0, 0, $imageW, $imageH, $this ->markAlpha);
}
return $res;
}
function getMarkPos($res, $markW, $markH)
{
$imageW = imagesx($res);
$imageH = imagesy($res);
switch ($this ->markTextAlignment)
{
case 1 : //1为顶端居左
$posX = 0 ;
$posY = 0 ;
break ;
case 2 : //2为顶端居中
$posX = ($imageW - $markW) / 2 ;
$posY = 0 ;
break ;
case 3 : //3为顶端居右
$posX = $imageW - $markW ;
$posY = 0 ;
break ;
case 4 : //4为中部居左
$posX = 0 ;
$posY = ($imageH - $markH) / 2 ;
break ;
case 5 : //5为中部居中
$posX = ($imageW - $markW) / 2 ;
$posY = ($imageH - $markH) / 2 ;
break ;
case 6 : //6为中部居右
$posX = $imageW - $markW ;
$posY = ($imageH - $markH) / 2 ;
break ;
case 7 : //7为底端居左
$posX = 0 ;
$posY = $imageH - $markH ;
break ;
case 8 : //8为底端居中
$posX = ($imageW - $markW) / 2 ;
$posY = $imageH - $markH ;
break ;
case 9 : //9为底端居右
$posX = $imageW - $markW ;
$posY = $imageH - $markH ;
break ;
default : //随机
$posX = rand (0, ($imageW - $markW));
$posY = rand (0, ($imageH - $markH));
break ;
}
$pos = array();
$pos['x' ] = $posX;
$pos['y' ] = $posY;
return $pos;
}
function readImageFile($file)
{
switch ($this ->imageType)
{
case 'image/jpeg' :
{
$res = imagecreatefromjpeg($file);
break ;
}
case 'image/gif' :
{
$res = imagecreatefromgif($file);
break ;
}
case 'image/png' :
{
$res = imagecreatefrompng($file);
break ;
}
}
return $res;
}
/**
* 水印相关参数检查
* @access private
* @return true或错误说明
*/< br />
function markSetCheck()
{
if ($this ->markType == 1)
{
if (empty($this ->markText))
{
return “水印文字未设置” ;
}
elseif (count($this ->fontArray) == 0)
{
return “未设置字体列表” ;
}
else
{
return true ;
}
}
elseif ($this ->markType == 2)
{
if (empty($this ->markImage))
{
return “水印图片未设置” ;
}
elseif (!is_file($this ->markImage))
{
return “水印图片有问题” ;
}
else
{
return true ;
}
}
else
{
return true ;
}
}
/**
* 增加字体,文字水印用
* @access public
* @param string $file 字体文件路径
*/< br />
function addFont($file)
{
array_push($this ->fontArray, $file);
}
/**
* 设置缩图类型
* @access public
* @param bool $zt 缩放类型
* @param bool $zp 裁图范围
*/< br />
function setZoom($zt, $zp)
{
$this ->zoomType = $zt;
$this ->zoomPos = $zp;
}
/**
* 设置水印参数
* @access public
* @param int $mt 写水印类型
* @param string $m 文字或图片
* @param float $ma 文字透明度
*/< br />
function setMark($mt, $m, $ma = 100)
{
$this ->markType = $mt;
if ($ma >= 0 && $ma <= 100)
{
$this ->markAlpha = $ma;
}
else
{
$this ->markAlpha = 1;
}
if ($this ->markType == 1)
{
$this ->markText = $m;
}
elseif ($this ->markType == 2)
{
$this ->markImage = $m;
}
else
{
$this ->markType = 0;
$this ->markText = “” ;
$this ->markImage = “” ;
}
}
/**
* 设置水印文字相关
* @access public
* @param string $fc 字体颜色,必须以#号开始的颜色号
* @param int $fz 字体大小
* @param int $fw 字宽,好像没什么用
* @param string $fa 默认右下角
*/< br />
function setFont($fc = “#111111″ , $fz = 10, $fw = 100)
{
$this ->markTextColor = $fc;
$this ->markTextSize = $fz;
$this ->markTextWidth = $fw;
}
/**
* 设置对齐方式
* @access public
* @param string $fa 默认右下角
*/< br />
function setAlignment($fa = 9)
{
$this ->markTextAlignment = $fa;
}
}
//类使用例子
/*
//文字水印
$iz = new ImageZoom();
$iz->setZoom(false, false);
$iz->addFont(”MSYH.TTF”);
$iz->addFont(”simsun.ttc”);
$iz->setMark(1, “http://blog.me94me.com”, 50);
$b = $iz->zoom(”1.jpg”, “4.jpg”, 700, 500);
*/< br />
/*
//图片水印
$iz = new ImageZoom();
$iz->setZoom(false, false);
$iz->setMark(2, “2.jpg”, 60);
$b = $iz->zoom(”3.jpg”, “4.jpg”, 500, 800);
*/< br />
?>