1、后端代码给图片添加水印
<?php
namespace app\water\controller;
use think\Controller;
use think\Image;
class WaterPicture extends Controller
{
public function index(){
$sourceImagePath = 'D:/www/output/water.jpg';
$saveImagePath = 'D:/www/output/water_image4.jpg';
$image = imagecreatefromjpeg($sourceImagePath);
$text = "这里是水印文字";
$fontPath = 'D:/www/wxtp5/application/water/font/HPSimplifiedHansLight.ttf';
$fontSize = 20;
$textColor = imagecolorallocate($image, 220, 20, 60);
for($i = 20; $i <= 200; $i = $i + 40){
$x = $i;
$y = imagefontheight($fontSize) + $i;
imagettftext($image, $fontSize, 30, $x, $y, $textColor, $fontPath, $text);
}
imagejpeg($image, $saveImagePath);
imagedestroy($image);
$this->success('水印添加成功!', 'index/index');
}
}