第九章 php图像处理(水印)

版权声明:转载请附上文章地址 https://blog.csdn.net/weixin_38134491/article/details/85870810
给图片加文字水印
<?php

header('Content-type:image/jpeg');
$img=imagecreatefromjpeg('images/lzs.jpg');
$color=imagecolorallocate($img, 209, 224, 239);

$width=imagesx($img);
$height=imagesy($img);

// getimagesize();此函数也可以取得图片长和宽,但输入的是图片路径

// 计算文字的宽度
$position=imagettfbbox(20,0,'font/Elements.ttf','LTEsoft.com');
$stringWidth=$position[2]-$position[0];

// 控制文字水印的位置
imagettftext($img, 24, 0, $width-1-$stringWidth-($width/20), $height-1-($height/10), $color, 'font/Elements.ttf', 'LTEsoft.com');

imagejpeg($img); //输出图片
imagedestroy($img);

给图片加图片透明水印
<?php

header('Content-type:image/jpeg');
$img=imagecreatefromjpeg('images/lzs.jpg');

$waterMark=imagecreatefrompng('images/water1.png');
$color=imagecolorallocate($img, 255, 255, 255);

$width=imagesx($img);
$height=imagesy($img);

$waterMarkWidth=imagesx($waterMark);
$waterMarkHeight=imagesy($waterMark);

$position=imagettfbbox(20,0,'font/Elements.ttf','huntfox.net');
$stringWidth=$position[2]-$position[0];

//文字水印
imagettftext($img, 24, 0, $width-1-$stringWidth, $height-1, $color, 'font/Elements.ttf', 'huntfox.net');

imagecopymerge($img, $waterMark, $width-1-$waterMarkWidth, $height-1-$waterMarkHeight, 0, 0, $waterMarkWidth, $waterMarkHeight,70);
/* 100:所拷贝到目标图像资源上面的坐标(x轴)
 100:所拷贝到目标图像资源上面的坐标(y轴)
 0:从水印图像资源的x坐标为0的位置开始拷贝
 0:从水印图像资源的y坐标为0的位置开始拷贝
 因为imagecopy()函数是拷贝水印图像的部分,而我们现在要拷贝的是全部水印图像 */

imagejpeg($img); //输出图片
imagedestroy($img);

猜你喜欢

转载自blog.csdn.net/weixin_38134491/article/details/85870810