在很多网站上,相同的图片有很多,以至于有部分人员使用他人的图片,而造成侵权,所以接下来我们来学习怎样将自己的图片添加水印,防止他人使用图片
首先加载图片
$imgfile = "xiaobei.jpg";
其次 运用 file_get_contents 函数将整个文件读入一个字符串中,将自己的图片读入一个字符串中
$str = file_get_contents("xiaobei.jpg");
其次 创建图像 运用 imagecreatefromstring 函数创建图像
并判断图像创建是否成功(如若创建失败则终止代码的运行,并提示创建图像失败)
$img = imagecreatefromstring($str);
if($img === false){
die('创建图像失败');
}
``
最后加水印,1.导入字体库文件2.设置水印的内容3.设置水印字体的大小、颜色4.导出添加水印之后的图片,在同一个文件夹下生成
f
o
n
t
=
"
S
T
C
A
I
Y
U
N
.
T
T
F
"
;
i
f
(
!
f
i
l
e
e
x
i
s
t
s
(
font = "STCAIYUN.TTF"; if(!file_exists(
font="STCAIYUN.TTF";if(!fileexists(font)){
die(‘字体库文件不存在’);
}
$text = “xx版权所”;
c
o
l
o
r
=
i
m
a
g
e
c
o
l
o
r
a
l
l
o
c
a
t
e
(
color = imagecolorallocate(
color=imagecolorallocate(img, 255, 255, 255);
imagettftext(
i
m
g
,
30
,
0
,
50
,
50
,
img,30,0,50,50,
img,30,0,50,50,color,
f
o
n
t
,
font,
font,text);
imagepng($img,‘new-’ .
i
m
g
f
i
l
e
)
;
i
m
a
g
e
d
e
s
t
r
o
y
(
imgfile); imagedestroy(
imgfile);imagedestroy(img);
echo ‘生成图像文件成功!!!’;
最后生成带水印的图片
