<?php
// 1)加载图片为画布:
$image = imagecreatefromjpeg('images/003.jpg');
// 2)在图片上写字:
//读取图片的宽和高
$width = imagesx($image); //宽度
$height = imagesy($image); //高度
//文字的宽高
$size = 25;
$angle = 0;
$color = imagecolorallocate($image, 255, 255, 255);
$fontfile = __DIR__ . '/SFMono-Regular.otf';
$text = 'ABC123';
//新的: 获取文字所在盒子的4个角的坐标
$info = imagettfbbox($size, $angle, $fontfile, $text);
$box_w = $info[2] - $info[0];
$box_h = $info[1] - $info[5];
//计算出文字盒子左下角出现在 图片右下角的锚点位置
$x = $width - $box_w - 10;
$y = $height - 10;
//水印居中时的x
$x = $width / 2 - $box_w / 2;
imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
// 3)导出到浏览器:
header('content-type:image/jpeg');
imagejpeg($image);
// 4)保存到本地:
imagejpeg($image, 'water/111.jpg');
// 5)销毁图片对象:
imagedestroy($image);
// 注意:原理为加载图片为画布, 然后画布上写字即可。