php语言写背景颜色,php图片加文字水印底部背景色水印-Go语言中文社区

/* $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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值