本文实例讲述了PHP图像处理 imagestring添加图片水印与文字水印操作。分享给大家供大家参考,具体如下:
imagestring添加图片水印
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<?php
header( "Content-Type: text/html;charset=utf-8" );
//指定图片路径
$src = '001.png' ;
//获取图片信息
$info = getimagesize ( $src );
//获取图片扩展名
$type = image_type_to_extension( $info [2],false);
//动态的把图片导入内存中
$fun = "imagecreatefrom{$type}" ;
$image = $fun ( '001.png' );
//指定字体颜色
$col = imagecolorallocatealpha( $image ,0,0,0,0); //R,G,B,透明度
//指定字体内容
$content = 'zhangsan' ;
//给图片添加文字
imagestring( $image ,5,190,255, $content , $col );
//指定字体内容
$content = '123456789' ;
//给图片添加文字
imagestring( $image ,5,190,285, $content , $col );
//指定字体内容
$content = '98.6' ;
//给图片添加文字
imagestring( $image ,5,190,320, $content , $col );
//指定输入类型
header( 'Content-type:' . $info [ 'mime' ]);
//动态的输出图片到浏览器中
$func = "image{$type}" ;
$func ( $image );
//销毁图片
imagedestroy( $image );
?>
|
这里我们使用了imagestring方法来添加文字,但是imagestring并不支持中文字符,添加中文可以使用imagettftext来添加。
效果图:
imagettftext添加中文水印
前面写了PHP图像处理 imagestring添加图片水印,但是imagestring方法不能添加中文,所以现在使用imagettftext这个方法来添加中文。相比imagestring,imagettftext需要指定字体文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php
//1. 打开要加水印的图片
$image = imagecreatefromjpeg( "001.jpg" );
//2. 在画布中绘制图像
$bai = imagecolorallocate( $image , 255, 255, 255);
//3. 设置水印文字
$text = 'abc我是水印123,。、
!@#dasdasda1231';
//使用指定的字体文件绘制文字
//参数2:字体大小
//参数3:字体倾斜的角度
//参数4、5:文字的x、y坐标
//参数6:文字的颜色
//参数7:字体文件
//参数8:绘制的文字
imagettftext( $image , 50, 0, 280, 1000, $bai , 'STXINGKA.TTF' , $text );
//4. 在浏览器直接输出图像资源
header( "Content-Type:image/jpeg" );
imagejpeg( $image );
//5. 销毁图像资源
imagedestroy( $image );
?>
|
效果图:
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_17497931/article/details/86712977