PHP之添加文字水印,两端文字分别设置不同的样式,支持透明度

 

/**
 * 给图片添加文字水印 可控制字体颜色透明度,默认是居中
 * @param string $imagePath 图片地址
 * @param string $outputPath 新图片地址 默认使用$imgurl
 * @param string $textArray 水印文字
 * @param string $fontPath 字体文件路径
 * @param int $fontSize 字体大小
 * @return boolean
 */
function addTextWatermark($imagePath, $outputPath, $textArray, $fontPath = '', $fontSize = 28)
{
    $fontPath = $fontPath ?: dirname(dirname(dirname(dirname(__FILE__)))) . "/images/fonts/msyh.ttc";
    // 获取图片信息
    $imageInfo = getimagesize($imagePath);
    $imageWidth = $imageInfo[0];
    $imageHeight = $imageInfo[1];

    // 创建图片资源
    $image = imagecreatefromstring(file_get_contents($imagePath));

    // 设置水印文字颜色和字体大小
    $textColor = imagecolorallocatealpha($image, 255, 255, 255, 60); // 白色,透明度为100
    $textColorTitle = imagecolorallocate($image, 255, 255, 255); // 白色
    $textSpacing = 6; // 间隔值

    $textName = $textArray['name'];
    $textTitle = $textArray['title'];

    // 调整文字水印大小,不管图片多大,文字都展示正常
    $imageRatio = $imageWidth / 930;
    $adjustedFontSize = ceil(($imageRatio) * $fontSize);
    $textSpacing = ceil(($imageRatio) * $textSpacing);
    //固定大小
    // $adjustedFontSize = $fontSize;

    // 计算文字水印的宽度和高度
    $textBoundingBoxName = imagettfbbox($adjustedFontSize, 0, $fontPath, $textName);
    $textWidthName = $textBoundingBoxName[2] - $textBoundingBoxName[0];
    $textHeightName = $textBoundingBoxName[1] - $textBoundingBoxName[7];

    $textBoundingBoxTitle = imagettfbbox($adjustedFontSize, 0, $fontPath, $textTitle);
    $textWidthTitle = $textBoundingBoxTitle[2] - $textBoundingBoxTitle[0];
    $textHeightTitle = $textBoundingBoxTitle[1] - $textBoundingBoxTitle[7];

    // 计算水印文字的位置
    $adjustedXName = ($imageWidth - $textWidthName - $textWidthTitle - $textSpacing) / 2;
    $adjustedYName = ($imageHeight + $textHeightName) / 2;
    $adjustedXTitle = $adjustedXName + $textWidthName + $textSpacing;
    $adjustedYTitle = ($imageHeight + $textHeightTitle) / 2;

    // 添加水印文字
    imagettftext($image, ($adjustedFontSize - 2), 0, $adjustedXName, $adjustedYName, $textColor, $fontPath, $textName);
    imagettftext($image, $adjustedFontSize, 0, $adjustedXTitle, $adjustedYTitle, $textColorTitle, $fontPath, $textTitle);
    /*    imagettftext($image, ($fontSize - 2), 0, $xName, $yName, $textColor, $fontPath, $textName);
        imagettftext($image, $fontSize, 0, $xTitle, $yTitle, $textColorTitle, $fontPath, $textTitle);*/

    // 保存图片
    imagepng($image, $outputPath);

    // 释放图片资源
    imagedestroy($image);
}
$imagePath = '../../image/up_image/20231214/231214551024943405.jpg';
$outputPath = '../../image/up_image/20231213/' . imgSuiYin('231214551024943405.jpg');
$textArray = array('name' => '@急急急', 'title' => '爱看');

addTextWatermark($imagePath, $outputPath, $textArray);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值