php 固定水印大小_PHP 批量给图片添加水印(多图,压缩水印大小)

这个例子是给图片批量添加图片水印, 文字水印道理相同,改一下就好了:

方法里传的参数是图片路径,传逗号字符串就好。

因为项目要求水印的比例要保持一直一样,所以想了个办法:

压缩水印或放大水印,生成一张新的水印图片,把水印添到图片上,再把生成的水印图片删掉。

function image_water($pathdata){

$path = explode(',', $pathdata);

$src_path = 'Public/Console/images/sss.png'; //这个是水印图片路径

array_pop($path); //因为我传的逗号字符串最后还有一个逗号,所以需要去掉最后一个空数组

foreach($path as $p){

$p = substr($p,1); //处理一下路径,看项目具体情况操作

list($width,$height,$type)=getimagesize($p); // 获取图片信息

list($src_w,$src_h)=getimagesize($src_path);

$bi = $src_w / $src_h;

$new_width = $width*0.3; //压缩水印是图片的 0.3

$new_height = $new_width / $bi;

$image_wp=imagecreatetruecolor($new_width, $new_height);

$c=imagecolorallocatealpha($image_wp , 0 , 0 , 0 ,127);//拾取一个完全透明的颜色

imagealphablending($image_wp ,false);//关闭混合模式,以便透明颜色能覆盖原画布

imagefill($image_wp , 0 , 0, $c);//填充

imagesavealpha($image_wp ,true);//设置保存PNG时保留透明通道信息

$image = imagecreatefrompng($src_path);

imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $src_w, $src_h);

header('content-Type:image/png');

imagepng($image_wp,'Public/Console/images/sss_s.png');

$s_path = 'Public/Console/images/sss_s.png';

$dst = imagecreatefromstring(file_get_contents($p));

$src = imagecreatefromstring(file_get_contents($s_path));

list($s_width,$h_height)=getimagesize($s_path);

$newwidth = $width - $s_width;

$newheight = $height - $h_height;

imagecopy($dst, $src, $newwidth, $newheight, 0, 0, $new_width, $new_height);

switch($type){

case 1://GIF

header("content-type:image/gif");

imagegif($dst,$p);

unlink($s_path);

break;

case 2://JPG

header("content-type:image/jpeg");

imagejpeg($dst,$p);

unlink($s_path);

break;

case 3://PNG

header("content-type:image/png");

imagepng($dst,$p);

unlink($s_path);

break;

/*imagepng--以PNG格式将图像输出到浏览器或文件

imagepng()将GD图像流(image)以png格式输出到标注输出(通常为浏览器),或者如果用filename给出了文件名则将其输出到文件*/

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值