TP5给图片添加水印(待更新)

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;
        // 设置水印文字颜色(RGB)
        $textColor = imagecolorallocate($image, 220, 20, 60);
        for($i = 20; $i <= 200; $i = $i + 40){
            // 设置水印文字的位置(x, y坐标)
            $x = $i;
            $y = imagefontheight($fontSize) + $i; // 加一些偏移量避免贴到边缘
            // 使用TrueType字体将文字写入图像
            imagettftext($image, $fontSize, 30, $x, $y, $textColor, $fontPath, $text);
        }
        // 将处理后的图像保存为文件
        imagejpeg($image, $saveImagePath);
        // 释放图像资源
        imagedestroy($image);
        // 返回处理结果或重定向
        $this->success('水印添加成功!', 'index/index');
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值