测试工程费用

回复列表(336|隐藏机器人聊天)
  • @Ta / 2023-10-07 / /

    @坏人,如果你想将第一行文字水印的背景颜色改成透明浅蓝色,你可以使用imagecolorallocatealpha()函数来创建一个带有透明度的颜色。以下是如何修改代码:

    // 第一行文字
    $line1Text = '第一行文字';
    $line1BackgroundColor = imagecolorallocatealpha($image, 0, 153, 255, 50); // 透明浅蓝色背景,50是透明度值
    
    // 添加第一行文字水印
    $line1TextWidth = imagettfbbox($line1FontSize, 0, $font, $line1Text);
    $line1TextWidth = $line1TextWidth[2] - $line1TextWidth[0];
    $line1X = $ground_w - $line1TextWidth - 20; // 计算X坐标
    $line1Y = $ground_h - $ground_h / 3; // 计算Y坐标
    
    // 启用混合模式,实现透明效果
    imagealphablending($image, true);
    
    // 添加水印文字和背景
    imagettftext($image, $line1FontSize, 0, $line1X, $line1Y, $textColor, $font, $line1Text);
    imagefilledrectangle($image, $line1X, $line1Y - $line1FontSize, $ground_w, $ground_h, $line1BackgroundColor);
    

    在这里,imagecolorallocatealpha()函数用于创建一个带有透明度的颜色,其中的50是透明度值,可以根据需要进行调整。然后,通过imagealphablending()函数启用混合模式,以实现透明效果。接着,使用修改后的颜色添加水印文字和背景。

  • @Ta / 2023-10-07 / /
    @ChatGPT,<?php
    $uploadDir = 'images/'; // 上传目录路径,请确保该目录存在并有写入权限
    $groundImage = $uploadDir . '6520bc082f92d.jpg'; // 生成唯一的文件名,以避免重复文件名覆盖问题
    //move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $groundImage); // 将临时文件移动到目标位置

    $textFont = "100"; //文字大小
    // 打开原图像
    if (!empty($groundImage) && file_exists($groundImage)) {
        $ground_info = getimagesize($groundImage);
        $ground_w = $ground_info[0]; //取得背景图片的宽
        $ground_h = $ground_info[1]; //取得背景图片的高
        switch ($ground_info[2]) //取得背景图片的格式
            {
            case 1:
                $image = imagecreatefromgif($groundImage);
                break;
            case 2:
                $image = imagecreatefromjpeg($groundImage);
                break;
            case 3:
                $image = imagecreatefrompng($groundImage);
                break;
            default:
                die($formatMsg);
        }
    } else {
        die("需要加水印的图片不存在!");
    }

    // 定义水印文字颜色和字体
    $textColor = imagecolorallocate($image, 255, 255, 255);
    $font = 'ttff.ttf'; // 使用自己的字体文件路径
    // 第一行文字
    $line1Text = '第一行文字';
    $line1BackgroundColor = imagecolorallocatealpha($image, 0, 153, 255, 80); // 天空蓝色
    $line1FontSize = $textFont+34;
    // 第二行文字(系统时间)
    $line2Text = '时间:' . date('Y-m-d H:i:s'); // 默认系统时间
    $line2ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line2FontSize = $textFont+14;
    // 第三行文字(经纬度)
    $line3Text = '经度: 123.456, 纬度: 78.910';
    $line3ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line3FontSize = $textFont+14;
    // 第四行文字(位置名称)
    $line4Text = '位置名称';
    $line4ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line4FontSize = $textFont+14;
    // 第五行文字(备注)
    $line5Text = '备注';
    $line5ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line5FontSize = $textFont+14;
    // 添加第一行文字水印
    $line1TextWidth = imagettfbbox($line1FontSize, 0, $font, $line1Text);
    $line1TextWidth = $line1TextWidth[2] - $line1TextWidth[0];

    $line1X = $ground_w - $line1TextWidth - 40-$textFont; // 计算X坐标
    $line1Y = $ground_h - 180-$textFont; // 计算Y坐标

    // 启用混合模式,实现透明效果
    imagealphablending($image, true);

    imagefilledrectangle($image, $line1X - 5, $line1Y - $line1FontSize - 5, $ground_w +
        5, $line1Y + 5, $line1BackgroundColor);
    imagettftext($image, $line1FontSize, 0, $line1X, $line1Y, $textColor, $font, $line1Text);
    /**

     * // 添加第一行文字水印
     * imagefilledrectangle($image,$line1X, 40 - $line1FontSize, 1000, 40, $line1BackgroundColor);
     * imagettftext($image, $line1FontSize, 0,$line1X, 40, $textColor, $font, $line1Text);
     */
    // 添加第二行文字水印
    imagettftext($image, $line2FontSize, 0, $line1X, $line1Y + 30, $line2ShadowColor,
        $font, $line2Text);
    imagettftext($image, $line2FontSize, 0, $line1X + 2, $line1Y + 32, $textColor, $font,
        $line2Text);
    // 添加第三行文字水印
    imagettftext($image, $line3FontSize, 0, $line1X, $line1Y + 60, $line3ShadowColor,
        $font, $line3Text);
    imagettftext($image, $line3FontSize, 0, $line1X + 2, $line1Y + 62, $textColor, $font,
        $line3Text);
    // 添加第四行文字水印
    imagettftext($image, $line4FontSize, 0, $line1X, $line1Y + 90, $line4ShadowColor,
        $font, $line4Text);
    imagettftext($image, $line4FontSize, 0, $line1X + 2, $line1Y + 92, $textColor, $font,
        $line4Text);
    // 添加第五行文字水印
    imagettftext($image, $line5FontSize, 0, $line1X, $line1Y + 120, $line5ShadowColor,
        $font, $line5Text);
    imagettftext($image, $line5FontSize, 0, $line1X + 2, $line1Y + 122, $textColor,
        $font, $line5Text);
    // 保存处理后的图像,覆盖原图像
    if ($image === false) {
        die('Unable to create image from file.');
    }
    if (!imagejpeg($image, $groundImage)) {
        die('Unable to save processed image.');
    }
    // 生成水印后的图片
    @unlink($groundImage);
    switch ($ground_info[2]) //取得背景图片的格式
        {
        case 1:
            imagegif($image, $groundImage);
            break;
        case 2:
            imagejpeg($image, $groundImage);
            break;
        case 3:
            imagepng($image, $groundImage);
            break;
        default:
            die($errorMsg);
    }
    // 释放内存
    if (isset($image)) {
        imagedestroy($image);
    }
    //unset($ground_info);//删除文件
    // 文件上传和水印添加成功
    echo '' . $groundImage . '?' . uniqid() . '<img src="' . $groundImage . '?' .
        uniqid() . '" alt="处理后的图像" width="500">';
    ?>把水印位置根据$textFont的大小来计算好合适位置
  • @Ta / 2023-10-07 / /
    待审核
    发言待审核,仅管理员和作者本人可见。
  • @Ta / 2023-10-07 / /
    待审核
    发言待审核,仅管理员和作者本人可见。
  • @Ta / 2023-10-07 / /

    @坏人,你已经成功地将第一行文字的水印文字大小和其他行文字的水印文字大小进行了修改,这看起来很不错。你的代码已经按照你的需求进行了调整。如果你需要进一步的修改或有其他问题,请随时提出。

  • @Ta / 2023-10-07 / /
    待审核
    发言待审核,仅管理员和作者本人可见。
  • @Ta / 2023-10-07 / /

    @坏人,要根据图片大小来动态调整水印文字大小和位置,你可以通过计算图片的宽度和高度,然后根据比例来调整文字大小和位置。以下是修改后的代码示例:

    <?php
    $uploadDir = 'images/'; // 上传目录路径,请确保该目录存在并有写入权限
    $groundImage = $uploadDir . uniqid() . '.jpg'; // 生成唯一的文件名,以避免重复文件名覆盖问题
    move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $groundImage); // 将临时文件移动到目标位置
    
    // 打开原图像
    if (!empty($groundImage) && file_exists($groundImage)) {
        $ground_info = getimagesize($groundImage);
        $ground_w = $ground_info[0]; // 取得背景图片的宽
        $ground_h = $ground_info[1]; // 取得背景图片的高
        switch ($ground_info[2]) // 取得背景图片的格式
        {
            case 1:
                $image = imagecreatefromgif($groundImage);
                break;
            case 2:
                $image = imagecreatefromjpeg($groundImage);
                break;
            case 3:
                $image = imagecreatefrompng($groundImage);
                break;
            default:
                die($formatMsg);
        }
    } else {
        die("需要加水印的图片不存在!");
    }
    
    // 定义水印文字颜色和字体
    $textColor = imagecolorallocate($image, 255, 255, 255);
    $font = 'ttff.ttf'; // 使用自己的字体文件路径
    
    // 计算文字大小
    $textFontSize = min($ground_w, $ground_h) / 20; // 可根据需要调整比例
    
    // 第一行文字
    $line1Text = '第一行文字';
    $line1BackgroundColor = imagecolorallocatealpha($image, 0, 153, 255, 80); // 天空蓝色
    $line1FontSize = $textFontSize + 10; // 可根据需要调整字体大小
    
    // 第二行文字(系统时间)
    $line2Text = '时间:' . date('Y-m-d H:i:s'); // 默认系统时间
    $line2ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line2FontSize = $textFontSize + 6; // 可根据需要调整字体大小
    
    // 添加第一行文字水印
    $line1TextWidth = imagettfbbox($line1FontSize, 0, $font, $line1Text);
    $line1TextWidth = $line1TextWidth[2] - $line1TextWidth[0];
    
    $line1X = $ground_w - $line1TextWidth - 40 - ($textFontSize * 5); // 计算X坐标
    $line1Y = $ground_h - 180 - ($textFontSize * 5); // 计算Y坐标
    
    // 启用混合模式,实现透明效果
    imagealphablending($image, true);
    
    imagefilledrectangle($image, $line1X - 5, $line1Y - $line1FontSize - 5, $ground_w +
        5, $line1Y + 5, $line1BackgroundColor);
    imagettftext($image, $line1FontSize, 0, $line1X, $line1Y, $textColor, $font, $line1Text);
    
    // 添加第二行文字水印
    $line2TextHeight = $line1Y + 40 + $line2FontSize; // 计算第二行文字的高度
    imagettftext($image, $line2FontSize, 0, $line1X, $line2TextHeight, $line2ShadowColor,
        $font, $line2Text);
    imagettftext($image, $line2FontSize, 0, $line1X + 2, $line2TextHeight + 2, $textColor,
        $font, $line2Text);
    
    // 保存处理后的图像,覆盖原图像
    if ($image === false) {
        die('Unable to create image from file.');
    }
    if (!imagejpeg($image, $groundImage)) {
        die('Unable to save processed image.');
    }
    
    // 生成水印后的图片
    @unlink($groundImage);
    switch ($ground_info[2]) // 取得背景图片的格式
    {
        case 1:
            imagegif($image, $groundImage);
            break;
        case 2:
            imagejpeg($image, $groundImage);
            break;
        case 3:
            imagepng($image, $groundImage);
            break;
        default:
            die($errorMsg);
    }
    
    // 释放内存
    if (isset($image)) {
        imagedestroy($image);
    }
    
    // 文件上传和水印添加成功
    echo '' . $groundImage . '?' . uniqid() . '<img src="' . $groundImage . '?' .
        uniqid() . '" alt="处理后的图像" width="400">';
    ?>
    

    这个代码会根据图片的宽度和高度动态计算水印文字的大小,并将水印文字添加到图片的底部。你可以根据需要进一步调整文字大小和位置。

  • @Ta / 2023-10-07 / /
    @ChatGPT,把完整代码发出来
  • @Ta / 2023-10-07 / /

    @坏人,以下是完整的 PHP 代码,用于上传图片并在图片底部添加水印文字,根据图片大小自动调整文字大小和位置:

    <?php
    $uploadDir = 'images/'; // 上传目录路径,请确保该目录存在并有写入权限
    $groundImage = $uploadDir . uniqid() . '.jpg'; // 生成唯一的文件名,以避免重复文件名覆盖问题
    move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $groundImage); // 将临时文件移动到目标位置
    
    // 打开原图像
    if (!empty($groundImage) && file_exists($groundImage)) {
        $ground_info = getimagesize($groundImage);
        $ground_w = $ground_info[0]; // 取得背景图片的宽
        $ground_h = $ground_info[1]; // 取得背景图片的高
        switch ($ground_info[2]) // 取得背景图片的格式
        {
            case 1:
                $image = imagecreatefromgif($groundImage);
                break;
            case 2:
                $image = imagecreatefromjpeg($groundImage);
                break;
            case 3:
                $image = imagecreatefrompng($groundImage);
                break;
            default:
                die($formatMsg);
        }
    } else {
        die("需要加水印的图片不存在!");
    }
    
    // 定义水印文字颜色和字体
    $textColor = imagecolorallocate($image, 255, 255, 255);
    $font = 'ttff.ttf'; // 使用自己的字体文件路径
    
    // 计算文字大小
    $textFontSize = min($ground_w, $ground_h) / 20; // 可根据需要调整比例
    
    // 第一行文字
    $line1Text = '第一行文字';
    $line1BackgroundColor = imagecolorallocatealpha($image, 0, 153, 255, 80); // 天空蓝色
    $line1FontSize = $textFontSize + 10; // 可根据需要调整字体大小
    
    // 第二行文字(系统时间)
    $line2Text = '时间:' . date('Y-m-d H:i:s'); // 默认系统时间
    $line2ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line2FontSize = $textFontSize + 6; // 可根据需要调整字体大小
    
    // 添加第一行文字水印
    $line1TextWidth = imagettfbbox($line1FontSize, 0, $font, $line1Text);
    $line1TextWidth = $line1TextWidth[2] - $line1TextWidth[0];
    
    $line1X = $ground_w - $line1TextWidth - 40 - ($textFontSize * 5); // 计算X坐标
    $line1Y = $ground_h - 180 - ($textFontSize * 5); // 计算Y坐标
    
    // 启用混合模式,实现透明效果
    imagealphablending($image, true);
    
    imagefilledrectangle($image, $line1X - 5, $line1Y - $line1FontSize - 5, $ground_w +
        5, $line1Y + 5, $line1BackgroundColor);
    imagettftext($image, $line1FontSize, 0, $line1X, $line1Y, $textColor, $font, $line1Text);
    
    // 添加第二行文字水印
    $line2TextHeight = $line1Y + 40 + $line2FontSize; // 计算第二行文字的高度
    imagettftext($image, $line2FontSize, 0, $line1X, $line2TextHeight, $line2ShadowColor,
        $font, $line2Text);
    imagettftext($image, $line2FontSize, 0, $line1X + 2, $line2TextHeight + 2, $textColor,
        $font, $line2Text);
    
    // 保存处理后的图像,覆盖原图像
    if ($image === false) {
        die('Unable to create image from file.');
    }
    if (!imagejpeg($image, $groundImage)) {
        die('Unable to save processed image.');
    }
    
    // 生成水印后的图片
    @unlink($groundImage);
    switch ($ground_info[2]) // 取得背景图片的格式
    {
        case 1:
            imagegif($image, $groundImage);
            break;
        case 2:
            imagejpeg($image, $groundImage);
            break;
        case 3:
            imagepng($image, $groundImage);
            break;
        default:
            die($errorMsg);
    }
    
    // 释放内存
    if (isset($image)) {
        imagedestroy($image);
    }
    
    //unset($ground_info);//删除文件
    // 文件上传和水印添加成功
    echo '' . $groundImage . '?' . uniqid() . '<img src="' . $groundImage . '?' .
        uniqid() . '" alt="处理后的图像" width="400">';
    ?>
    

    这段代码会根据上传的图片动态调整水印文字的大小和位置,然后将处理后的图片显示出来。确保将字体文件 ttff.ttf 放在相应的路径上以便使用。

  • @Ta / 2023-10-07 / /
    待审核
    发言待审核,仅管理员和作者本人可见。
  • @Ta / 2023-10-07 / /

    @坏人,以下是修改后的完整 PHP 代码,根据上传的图片大小自动调整水印文字大小和位置:

    <?php
    $uploadDir = 'images/'; // 上传目录路径,请确保该目录存在并有写入权限
    $groundImage = $uploadDir . uniqid() . '.jpg'; // 生成唯一的文件名,以避免重复文件名覆盖问题
    move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $groundImage); // 将临时文件移动到目标位置
    
    // 打开原图像
    if (!empty($groundImage) && file_exists($groundImage)) {
        $ground_info = getimagesize($groundImage);
        $ground_w = $ground_info[0]; // 取得背景图片的宽
        $ground_h = $ground_info[1]; // 取得背景图片的高
        switch ($ground_info[2]) // 取得背景图片的格式
        {
            case 1:
                $image = imagecreatefromgif($groundImage);
                break;
            case 2:
                $image = imagecreatefromjpeg($groundImage);
                break;
            case 3:
                $image = imagecreatefrompng($groundImage);
                break;
            default:
                die($formatMsg);
        }
    } else {
        die("需要加水印的图片不存在!");
    }
    
    // 定义水印文字颜色和字体
    $textColor = imagecolorallocate($image, 255, 255, 255);
    $font = 'ttff.ttf'; // 使用自己的字体文件路径
    
    // 计算文字大小
    $textFontSize = min($ground_w, $ground_h) / 20; // 可根据需要调整比例
    
    // 第一行文字
    $line1Text = '第一行文字';
    $line1BackgroundColor = imagecolorallocatealpha($image, 0, 153, 255, 80); // 天空蓝色
    $line1FontSize = $textFontSize + 10; // 可根据需要调整字体大小
    
    // 第二行文字(系统时间)
    $line2Text = '时间:' . date('Y-m-d H:i:s'); // 默认系统时间
    $line2ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line2FontSize = $textFontSize + 6; // 可根据需要调整字体大小
    
    // 第三行文字(经纬度)
    $line3Text = '经度: 123.456, 纬度: 78.910';
    $line3ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line3FontSize = $textFontSize + 6; // 可根据需要调整字体大小
    
    // 第四行文字(位置名称)
    $line4Text = '位置名称';
    $line4ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line4FontSize = $textFontSize + 6; // 可根据需要调整字体大小
    
    // 第五行文字(备注)
    $line5Text = '备注';
    $line5ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line5FontSize = $textFontSize + 6; // 可根据需要调整字体大小
    
    // 添加第一行文字水印
    $line1TextWidth = imagettfbbox($line1FontSize, 0, $font, $line1Text);
    $line1TextWidth = $line1TextWidth[2] - $line1TextWidth[0];
    
    $line1X = $ground_w - $line1TextWidth - 40 - ($textFontSize * 5); // 计算X坐标
    $line1Y = $ground_h - 180 - ($textFontSize * 5); // 计算Y坐标
    
    // 启用混合模式,实现透明效果
    imagealphablending($image, true);
    
    imagefilledrectangle($image, $line1X - 5, $line1Y - $line1FontSize - 5, $ground_w +
        5, $line1Y + 5, $line1BackgroundColor);
    imagettftext($image, $line1FontSize, 0, $line1X, $line1Y, $textColor, $font, $line1Text);
    
    // 添加第二行文字水印
    $line2TextHeight = $line1Y + 40 + $line2FontSize; // 计算第二行文字的高度
    imagettftext($image, $line2FontSize, 0, $line1X, $line2TextHeight, $line2ShadowColor,
        $font, $line2Text);
    imagettftext($image, $line2FontSize, 0, $line1X + 2, $line2TextHeight + 2, $textColor,
        $font, $line2Text);
    
    // 添加第三行文字水印
    $line3TextHeight = $line2TextHeight + 40 + $line3FontSize; // 计算第三行文字的高度
    imagettftext($image, $line3FontSize, 0, $line1X, $line3TextHeight, $line3ShadowColor,
        $font, $line3Text);
    imagettftext($image, $line3FontSize, 0, $line1X + 2, $line3TextHeight + 2, $textColor,
        $font, $line3Text);
    
    // 添加第四行文字水印
    $line4TextHeight = $line3TextHeight + 40 + $line4FontSize; // 计算第四行文字的高度
    imagettftext($image, $line4FontSize, 0, $line1X, $line4TextHeight, $line4ShadowColor,
        $font, $line4Text);
    imagettftext($image, $line4FontSize, 0, $line1X + 2, $line4TextHeight + 2, $textColor,
        $font, $line4Text);
    
    // 添加第五行文字水印
    $line5TextHeight = $line4TextHeight + 40 + $line5FontSize; // 计算第五行文字的高度
    imagettftext($image, $line5FontSize, 0, $line1X, $line5TextHeight, $line5ShadowColor,
        $font, $line5Text);
    imagettftext($image, $line5FontSize, 0, $line1X + 2, $line5TextHeight + 2,
    
  • @Ta / 2023-10-07 / /
    @ChatGPT,把的下半部分发出来!// 添加第五行文字水印
    $line5TextHeight = $line4TextHeight + 40 + $line5FontSize; // 计算第五行文字的高度
    imagettftext($image, $line5FontSize, 0, $line1X, $line5TextHeight, $line5ShadowColor,
        $font, $line5Text);
    imagettftext($image, $line5FontSize, 0, $line1X + 2, $line5TextHeight + 2,
  • @Ta / 2023-10-07 / /

    @坏人,抱歉,上面的代码似乎被截断了。以下是代码的下半部分:

    $textColor,
        $font,
        $line5Text
    );
    
    // 保存处理后的图像,覆盖原图像
    if ($image === false) {
        die('Unable to create image from file.');
    }
    if (!imagejpeg($image, $groundImage)) {
        die('Unable to save processed image.');
    }
    
    // 生成水印后的图片
    @unlink($groundImage);
    switch ($ground_info[2]) // 取得背景图片的格式
    {
        case 1:
            imagegif($image, $groundImage);
            break;
        case 2:
            imagejpeg($image, $groundImage);
            break;
        case 3:
            imagepng($image, $groundImage);
            break;
        default:
            die($errorMsg);
    }
    
    // 释放内存
    if (isset($image)) {
        imagedestroy($image);
    }
    
    // 文件上传和水印添加成功
    echo '' . $groundImage . '?' . uniqid() . '<img src="' . $groundImage . '?' .
        uniqid() . '" alt="处理后的图像" width="400">';
    ?>
    

    这是完整的 PHP 代码,根据上传的图片大小来调整水印文字的大小和位置。请确保你有一个名为 ttff.ttf 的字体文件以供使用,并将其放置在与这个 PHP 文件相同的目录中。

  • @Ta / 2023-10-07 / /
    @坏人, 在处理的图片大小是711 × 400 px的时候字体行间隔不会相对缩小
  • @Ta / 2023-10-07 / /
    @ChatGPT,在处理的小图片的时候字体行间隔不会相对缩小
  • @Ta / 2023-10-07 / /

    @坏人,如果在处理小图片时字体行间隔不会相对缩小,你可以尝试根据图片的高度来计算每一行文字的垂直位置,以确保行间距适应不同大小的图片。这可以通过以下方式来实现:

    // 计算每一行文字的垂直位置
    $lineHeight = $line1FontSize + 40; // 设置行高,可根据需要调整
    
    $line1Y = $ground_h - 180 - ($textFont * 5); // 第一行文字的Y坐标
    $line2Y = $line1Y + $lineHeight; // 第二行文字的Y坐标
    $line3Y = $line2Y + $lineHeight; // 第三行文字的Y坐标
    $line4Y = $line3Y + $lineHeight; // 第四行文字的Y坐标
    $line5Y = $line4Y + $lineHeight; // 第五行文字的Y坐标
    
    // 添加第一行文字水印
    imagettftext($image, $line1FontSize, 0, $line1X, $line1Y, $textColor, $font, $line1Text);
    
    // 添加第二行文字水印
    imagettftext($image, $line2FontSize, 0, $line1X, $line2Y, $line2ShadowColor, $font, $line2Text);
    imagettftext($image, $line2FontSize, 0, $line1X + 2, $line2Y + 2, $textColor, $font, $line2Text);
    
    // 添加其他行文字水印,依次类推
    imagettftext($image, $line3FontSize, 0, $line1X, $line3Y, $line3ShadowColor, $font, $line3Text);
    imagettftext($image, $line3FontSize, 0, $line1X + 2, $line3Y + 2, $textColor, $font, $line3Text);
    
    imagettftext($image, $line4FontSize, 0, $line1X, $line4Y, $line4ShadowColor, $font, $line4Text);
    imagettftext($image, $line4FontSize, 0, $line1X + 2, $line4Y + 2, $textColor, $font, $line4Text);
    
    imagettftext($image, $line5FontSize, 0, $line1X, $line5Y, $line5ShadowColor, $font, $line5Text);
    imagettftext($image, $line5FontSize, 0, $line1X + 2, $line5Y + 2, $textColor, $font, $line5Text);
    

    上述代码会根据行高 $lineHeight 来计算每一行文字的垂直位置,确保行间距相对缩放,适应不同大小的图片。你可以根据需要调整行高的数值。

  • @Ta / 2023-10-07 / /
    @ChatGPT,把完整代码发出来
  • @Ta / 2023-10-07 / /

    @坏人,我将提供完整的PHP代码,以根据图片的高度来动态调整水印文字的垂直位置,以确保行间距适应不同大小的图片。以下是完整的代码:

    <?php
    $uploadDir = 'images/'; // 上传目录路径,请确保该目录存在并有写入权限
    $groundImage = $uploadDir . uniqid() . '.jpg'; // 生成唯一的文件名,以避免重复文件名覆盖问题
    move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $groundImage); // 将临时文件移动到目标位置
    
    $textFont = "100"; //文字大小
    $textFont2 = "60"; //文字大小
    // 打开原图像
    if (!empty($groundImage) && file_exists($groundImage)) {
        $ground_info = getimagesize($groundImage);
        $ground_w = $ground_info[0]; //取得背景图片的宽
        $ground_h = $ground_info[1]; //取得背景图片的高
        switch ($ground_info[2]) //取得背景图片的格式
            {
            case 1:
                $image = imagecreatefromgif($groundImage);
                break;
            case 2:
                $image = imagecreatefromjpeg($groundImage);
                break;
            case 3:
                $image = imagecreatefrompng($groundImage);
                break;
            default:
                die($formatMsg);
        }
    } else {
        die("需要加水印的图片不存在!");
    }
    
    // 定义水印文字颜色和字体
    $textColor = imagecolorallocate($image, 255, 255, 255);
    $font = 'ttff.ttf'; // 使用自己的字体文件路径
    // 第一行文字
    $line1Text = '第一行文字';
    $line1BackgroundColor = imagecolorallocatealpha($image, 0, 153, 255, 80); // 天空蓝色
    $line1FontSize = $textFont+34;
    // 第二行文字(系统时间)
    $line2Text = '时间:' . date('Y-m-d H:i:s'); // 默认系统时间
    $line2ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line2FontSize = $textFont2+14;
    // 第三行文字(经纬度)
    $line3Text = '经度: 123.456, 纬度: 78.910';
    $line3ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line3FontSize = $textFont2+14;
    // 第四行文字(位置名称)
    $line4Text = '位置名称';
    $line4ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line4FontSize = $textFont2+14;
    // 第五行文字(备注)
    $line5Text = '备注';
    $line5ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line5FontSize = $textFont2+14;
    
    // 计算每一行文字的垂直位置
    $lineHeight = $line1FontSize + 40; // 设置行高,可根据需要调整
    
    $line1Y = $ground_h - 180 - ($textFont * 5); // 第一行文字的Y坐标
    $line2Y = $line1Y + $lineHeight; // 第二行文字的Y坐标
    $line3Y = $line2Y + $lineHeight; // 第三行文字的Y坐标
    $line4Y = $line3Y + $lineHeight; // 第四行文字的Y坐标
    $line5Y = $line4Y + $lineHeight; // 第五行文字的Y坐标
    
    // 添加第一行文字水印
    imagettftext($image, $line1FontSize, 0, $line1X, $line1Y, $textColor, $font, $line1Text);
    
    // 添加第二行文字水印
    imagettftext($image, $line2FontSize, 0, $line1X, $line2Y, $line2ShadowColor, $font, $line2Text);
    imagettftext($image, $line2FontSize, 0, $line1X + 2, $line2Y + 2, $textColor, $font, $line2Text);
    
    // 添加其他行文字水印,依次类推
    imagettftext($image, $line3FontSize, 0, $line1X, $line3Y, $line3ShadowColor, $font, $line3Text);
    imagettftext($image, $line3FontSize, 0, $line1X + 2, $line3Y + 2, $textColor, $font, $line3Text);
    
    imagettftext($image, $line4FontSize, 0, $line1X, $line4Y, $line4ShadowColor, $font, $line4Text);
    imagettftext($image, $line4FontSize, 0, $line1X + 2, $line4Y + 2, $textColor, $font, $line4Text);
    
    imagettftext($image, $line5FontSize, 0, $line1X, $line5Y, $line5ShadowColor, $font, $line5Text);
    imagettftext($image, $line5FontSize, 0, $line1X + 2, $line5Y + 2, $textColor, $font, $line5Text);
    
    // 保存处理后的图像,覆盖原图像
    if ($image === false) {
        die('Unable to create image from file.');
    }
    if (!imagejpeg($image, $groundImage)) {
        die('Unable to save processed image.');
    }
    // 生成水印后的图片
    @unlink($groundImage);
    switch ($ground_info[2]) //取得背景图片的格式
        {
        case 1:
            imagegif($image, $groundImage);
            break;
        case 2:
            imagejpeg($image, $groundImage);
            break;
        case 3:
            imagepng($image, $groundImage);
            break;
        default:
            die($errorMsg);
    }
    // 释放内存
    if (isset($image)) {
        imagedestroy($image);
    }
    //unset($ground_info);//删除文件
    // 文件上传和水印添加成功
    echo '' . $groundImage . '?' . uniqid() . '<img src="' . $groundImage
    
  • @Ta / 2023-10-07 / /
    @ChatGPT,你这把
    // 启用混合模式,实现透明效果
    imagealphablending($image, true);

    imagefilledrectangle($image, $line1X - 5, $line1Y - $line1FontSize - 5, $ground_w +
        5, $line1Y + 5, $line1BackgroundColor);
    这些给删除了!补回来
  • @Ta / 2023-10-07 / /

    @坏人,我已将缺少的代码补充回来。以下是完整的PHP代码:

    <?php
    $uploadDir = 'images/'; // 上传目录路径,请确保该目录存在并有写入权限
    $groundImage = $uploadDir . uniqid() . '.jpg'; // 生成唯一的文件名,以避免重复文件名覆盖问题
    move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $groundImage); // 将临时文件移动到目标位置
    
    $textFont = "100"; //文字大小
    $textFont2 = "60"; //文字大小
    // 打开原图像
    if (!empty($groundImage) && file_exists($groundImage)) {
        $ground_info = getimagesize($groundImage);
        $ground_w = $ground_info[0]; //取得背景图片的宽
        $ground_h = $ground_info[1]; //取得背景图片的高
        switch ($ground_info[2]) //取得背景图片的格式
            {
            case 1:
                $image = imagecreatefromgif($groundImage);
                break;
            case 2:
                $image = imagecreatefromjpeg($groundImage);
                break;
            case 3:
                $image = imagecreatefrompng($groundImage);
                break;
            default:
                die($formatMsg);
        }
    } else {
        die("需要加水印的图片不存在!");
    }
    
    // 定义水印文字颜色和字体
    $textColor = imagecolorallocate($image, 255, 255, 255);
    $font = 'ttff.ttf'; // 使用自己的字体文件路径
    // 第一行文字
    $line1Text = '第一行文字';
    $line1BackgroundColor = imagecolorallocatealpha($image, 0, 153, 255, 80); // 天空蓝色
    $line1FontSize = $textFont+34;
    // 第二行文字(系统时间)
    $line2Text = '时间:' . date('Y-m-d H:i:s'); // 默认系统时间
    $line2ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line2FontSize = $textFont2+14;
    // 第三行文字(经纬度)
    $line3Text = '经度: 123.456, 纬度: 78.910';
    $line3ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line3FontSize = $textFont2+14;
    // 第四行文字(位置名称)
    $line4Text = '位置名称';
    $line4ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line4FontSize = $textFont2+14;
    // 第五行文字(备注)
    $line5Text = '备注';
    $line5ShadowColor = imagecolorallocate($image, 0, 0, 0); // 阴影颜色
    $line5FontSize = $textFont2+14;
    
    // 计算每一行文字的垂直位置
    $lineHeight = $line1FontSize + 40; // 设置行高,可根据需要调整
    
    $line1Y = $ground_h - 180 - ($textFont * 5); // 第一行文字的Y坐标
    $line2Y = $line1Y + $lineHeight; // 第二行文字的Y坐标
    $line3Y = $line2Y + $lineHeight; // 第三行文字的Y坐标
    $line4Y = $line3Y + $lineHeight; // 第四行文字的Y坐标
    $line5Y = $line4Y + $lineHeight; // 第五行文字的Y坐标
    
    // 启用混合模式,实现透明效果
    imagealphablending($image, true);
    
    imagefilledrectangle($image, $line1X - 5, $line1Y - $line1FontSize - 5, $ground_w +
        5, $line1Y + 5, $line1BackgroundColor);
    
    // 添加第一行文字水印
    imagettftext($image, $line1FontSize, 0, $line1X, $line1Y, $textColor, $font, $line1Text);
    
    // 添加第二行文字水印
    imagettftext($image, $line2FontSize, 0, $line1X, $line2Y, $line2ShadowColor, $font, $line2Text);
    imagettftext($image, $line2FontSize, 0, $line1X + 2, $line2Y + 2, $textColor, $font, $line2Text);
    
    // 添加其他行文字水印,依次类推
    imagettftext($image, $line3FontSize, 0, $line1X, $line3Y, $line3ShadowColor, $font, $line3Text);
    imagettftext($image, $line3FontSize, 0, $line1X + 2, $line3Y + 2, $textColor, $font, $line3Text);
    
    imagettftext($image, $line4FontSize, 0, $line1X, $line4Y, $line4ShadowColor, $font, $line4Text);
    imagettftext($image, $line4FontSize, 0, $line1X + 2, $line4Y + 2, $textColor, $font, $line4Text);
    
    imagettftext($image, $line5FontSize,
    
添加新回复
回复需要登录