PHP水印制作

<?php
//打开本地图片添加水印
header("Content-type:image/jpeg");

//打开服务器或网络文件中已经存在的GIF,JPEG,PNG,WBMP格式图像
//	   imagecreatefromjpeg()
//	   imagecreatefrompng()
//	   imagecreatefromgif()
//	   imagecreatefromwbmp()
//imagecreatefromjpeg — 由文件或 URL 创建一个新图象。
//imagecreatefromjpeg( string $filename) : resource
$img = imagecreatefromjpeg("images/zcx.jpg");

//分配颜色
$color_str = imagecolorallocate($img,255,255,255);


//imagesx — 取得图像宽度
//imagesx( resource $image) : int
$width = imagesx($img);

//imagesy — 取得图像高度
$height = imagesY($img);

//imagettfbbox — 计算truetype字体所占区域
//imagettfbbox( float $size, float $angle, string $fontfile, string $text) : array
//imagettfbbox() 返回一个含有 8 个单元的数组表示了文本外框的四个角:
//0 左下角 X 位置
//1 左下角 Y 位置
//2 右下角 X 位置
//3 右下角 Y 位置
//4 右上角 X 位置
//5 右上角 Y 位置
//6 左上角 X 位置
//7 左上角 Y 位置

$position = imagettfbbox(20,0,'fonts/ywsfxs.ttf','王金龙');
//让右下角Y坐标减去左下角X坐标,就可以得到真字体所占宽度
$stringWidth = $position[2]-$position[0];

//imagettftext — 用 TrueType 字体向图像写入文本
//让X轴的坐标减去外边框的1像素,再减去真字体所占的宽度,再减去一定比例的边距,一定比例,可以适配不同大小图片的尺寸
imagettftext($img,20,0,$width-1-$stringWidth-($width/30),$height-1-($height/30),$color_str,'fonts/ywsfxs.ttf','王金龙');

//输出图片
imagejpeg($img);
//释放资源
imagedestroy($img);



![在这里插入图片描述](https://img-blog.csdnimg.cn/20191111004309570.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjE5MDg5Mw==,size_16,color_FFFFFF,t_70)






















原文地址:https://www.cnblogs.com/wjlbk/p/11839338.html