/**
* 生成水印,调用了生成水印文字和水印图片两个方法
*/
function _createmask()
{
if($this->mask_word)
{
// 获取字体信息
$this->_setfontinfo();
if($this->_isfull())
{
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,$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);
$this->_createmaskword($this->h_dst);
}
}
if($this->mask_img)
{
$this->_loadmaskimg();//加载时,取得宽高
if($this->_isfull())
{
// 将水印生成在原图上再拷
$this->_createmaskimg($this->h_src);
$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);
}
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,$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);
$this->_createmaskimg($this->h_dst);
}
}
if(empty($this->mask_word) && empty($this->mask_img))
{
$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,$color);// 填充背景色
}
}
/**
* 生成水印文字
*/
function _createmaskword($src)
{
$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($this->font))
{
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($src)
{
$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($this->h_mask);
}
/**
* 加载水印图
*/
function _loadmaskimg()
{
$mask_type = $this->_getimgtype($this->mask_img);
$this->_checkvalid($mask_type);
// file_get_contents函数要求php版本>4.3.0
$src = '';
if(function_exists(file_get_contents))
{
$src = file_get_contents($this->mask_img);
}
else
{
$handle = fopen ($this->mask_img, r);
while (!feof ($handle))
{
$src .= fgets($fd, 4096);
}
fclose ($handle);
}
if(empty($this->mask_img))
{
die(水印图片为空);
}
$this->h_mask = imagecreatefromstring($src);
$this->mask_w = $this->getimgwidth($this->h_mask);
$this->mask_h = $this->getimgheight($this->h_mask);
}
/**
* 图片输出
*/
function _output()
{
$img_type = $this->img_type;
$func_name = $this->all_type[$img_type]['output'];
if(function_exists($func_name))
{
// 判断浏览器,若是ie就不发送头
if(isset($_server['http_user_agent']))
{
$ua = strtoupper($_server['http_user_agent']);
if(!preg_match('/^.*msie.*\)$/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($color)
{
$arr = array();
for($ii=1; $ii {
$arr[] = hexdec(substr($color,$ii,2));
$ii++;
}
return $arr;
}
/**
* 计算出位置坐标
*/
?>