/* $path图片路径
* $file_name图片名称
* $font 字体路径
* $water_text 水印文字
* $over_flag 是否覆盖原来的图片 false 不覆盖 true 覆盖
* $font_size 水印文字字体大小
* $text_color 水印文字颜色
* $text_bg_color 水印文字背景颜色
* */
function RunWater($path, $file_name, $font, $water_text="实牛游戏 00000000000001", $over_flag = false, $font_size=10, $text_color="#ffffff", $text_bg_color="#393D49") {
$BaseWidth = 200;
if ($file_name == '' || $water_text == '') return '文件名为空或者水印为空!';
//检测是否安装GD库
if (false == function_exists('gd_info')) return '系统没有安装GD库,不能给图片加水印.';
//设置输入、输出图片路径名
$arr_in_name = explode(".", $file_name);
$in_img = $path . $file_name;
$out_img = $in_img;
$tmp_img = $path . $arr_in_name[0] . '_tmp.' . $arr_in_name[1]; //临时处理的图片,很重要
if (!$over_flag) $out_img = $path . $arr_in_name[0] . "_water_text2." . $arr_in_name[1];
//检测图片是否存在
if (!file_exists($in_img)) return '图片不存在!';
//获得图片的属性
$groundImageType = @getimagesize($in_img);
$imgWidth = $groundImageType[0];
$imgHeight = $groundImageType[1];
$img_type = $groundImageType[2];
//小于基本宽度,不处理
if ($imgWidth < $BaseWidth) return '水印宽度大于图片宽度';
//图片不是jpg/jpeg/gif/png时,不处理
switch ($img_type) {
case 1:
$image = imagecreatefromgif($in_img);
$TextBgColor = "#ffffff"; //gif图片字体只能为黑,所以背景颜色就设置为白色
break;
case 2:
$image = imagecreatefromjpeg($in_img);
break;
case 3:
$image = imagecreatefrompng($in_img);
break;
default:
return '图片格式不是jpg/jpeg/gif/png';
break;
}
//创建颜色
$color = @imagecolorallocate($image, hexdec(substr($text_color, 1, 2)), hexdec(substr($text_color, 3, 2)), hexdec(substr($text_color, 5, 2))); //文字颜色
//生成一个空的图片,它的高度在底部增加水印高度
$newHeight = $imgHeight + 20;
$objTmpImg = @imagecreatetruecolor($imgWidth, $newHeight);
$colorBg = @imagecolorallocate($objTmpImg, hexdec(substr($text_bg_color, 1, 2)), hexdec(substr($text_bg_color, 3, 2)), hexdec(substr($text_bg_color, 5, 2))); //背景颜色
填充图片的背景颜色
@imagefill($objTmpImg, 0, 0, $colorBg);
//把原图copy到临时图片中
@imagecopy($objTmpImg, $image, 0, 0, 0, 0, $imgWidth, $imgHeight);
//创建要写入的水印文字对象
$x = 5;
$y = $newHeight - 5;
//写入文字水印
@imagettftext($objTmpImg, $font_size, 0, $x, $y, $color, $font, $water_text);
//生成新的图片,临时图片
switch ($img_type) {
case 1:
imagegif($objTmpImg, $tmp_img);
break;
case 2:
imagejpeg($objTmpImg, $tmp_img);
break;
case 3:
imagepng($objTmpImg, $tmp_img);
break;
default:
return;
break;
}
//释放资源
@imagedestroy($objTmpImg);
@imagedestroy($image);
//重新命名文件
if ($over_flag) {
//覆盖原图
@unlink($in_img);
@rename($tmp_img, $out_img);
} else {
//不覆盖原图
@rename($tmp_img, $out_img);
}
}