测试工程费用

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

    @坏人,看起来您的代码中文字水印没有打印到图片上。这个问题可能是由于字体文件路径不正确或字体文件不可用导致的。

    在您的代码中,字体文件路径是这样设置的:

    $temp = imagettfbbox(ceil($textFont * 3), 0, "ttff.ttf", $waterText);
    

    这里使用了一个名为 "ttff.ttf" 的字体文件。确保以下几点:

    1. 字体文件 "ttff.ttf" 存在于相同的目录中,或者您可以使用绝对路径来指定字体文件的位置。

    2. 字体文件是一个可用的 TrueType 字体文件(.ttf 格式)。

    3. 字体文件路径的大小写与实际文件名的大小写一致。文件系统可能对大小写敏感。

    如果您没有指定字体文件的路径,您可以尝试使用绝对路径或相对于脚本文件的相对路径来指定字体文件。另外,请确保字体文件的可读性和可访问性。

    如果问题仍然存在,您可以尝试使用绝对路径,并检查字体文件是否可用。如果您不确定可用的字体文件,可以尝试使用操作系统默认的 TrueType 字体,例如 "arial.ttf"(如果在大多数操作系统上都可用)。

    示例:

    $temp = imagettfbbox(ceil($textFont * 3), 0, "/path/to/arial.ttf", $waterText);
    

    或者:

    $temp = imagettfbbox(ceil($textFont * 3), 0, "C:/Windows/Fonts/arial.ttf", $waterText);
    

    请根据您的实际情况更改字体文件路径。如果问题仍然存在,请提供更多关于字体文件的详细信息以供进一步帮助。

  • @Ta / 2023-10-06 / /
    @ChatGPT,<?php
    /*
    * author 张长伟
    * date   2009-3-27 16:30:12
    * 功能: PHP图片水印 (水印支持图片) 
    * 参数: 
    *      $groundImage   背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式; 
    *      $waterPos      水印位置,有10种状态,0为随机位置; 
    *                     1为顶端居左,2为顶端居中,3为顶端居右; 
    *                     4为中部居左,5为中部居中,6为中部居右; 
    *                     7为底端居左,8为底端居中,9为底端居右; 
    *      $waterImage    图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式; 
    *      $waterText     文字水印,即把文字作为为水印,支持ASCII码,不支持中文; 
    *      $textFont      文字大小,值为1、2、3、4或5,默认为5; 
    *      $textColor     文字颜色,值为十六进制颜色值,默认为#FF0000(红色); 

    * 注意:
    *      Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG 
    *      $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。 
    *      当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。 
    *      加水印后的图片的文件名和 $groundImage 一样。  
    */
    function imageWaterMark($groundImage, $waterPos = 9, $waterImage = "", $waterText =
        "", $textFont = 1, $textColor = "#FF0000")
    {
        $isWaterImage = false;
        $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
        //读取水印文件
        if (!empty($waterImage) && file_exists($waterImage)) {
            $isWaterImage = true;
            $water_info = getimagesize($waterImage);
            $water_w = $water_info[0]; //取得水印图片的宽
            $water_h = $water_info[1]; //取得水印图片的高
            switch ($water_info[2]) //取得水印图片的格式
                {
                case 1:
                    $water_im = imagecreatefromgif($waterImage);
                    break;
                case 2:
                    $water_im = imagecreatefromjpeg($waterImage);
                    break;
                case 3:
                    $water_im = imagecreatefrompng($waterImage);
                    break;
                default:
                    die($formatMsg);
            }
        }
        //读取背景图片
        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:
                    $ground_im = imagecreatefromgif($groundImage);
                    break;
                case 2:
                    $ground_im = imagecreatefromjpeg($groundImage);
                    break;
                case 3:
                    $ground_im = imagecreatefrompng($groundImage);
                    break;
                default:
                    die($formatMsg);
            }
        } else {
            die("需要加水印的图片不存在!");
        }
        $water_w = 0; // 初始化水印宽度
        $water_h = 0; // 初始化水印高度
        //水印位置
        if ($isWaterImage) //图片水印
            {
            $water_w = $water_info[0];
            $water_h = $water_info[1];
            $w = $water_w;
            $h = $water_h;
            $label = "图片的";
        } else //文字水印
        {
            $temp = imagettfbbox(ceil($textFont * 3), 0, "ttff.ttf", $waterText); //取得使用 TrueType 字体的文本的范围
            $w = $temp[2] - $temp[6];
            $h = $temp[3] - $temp[7];
            unset($temp);
            $label = "文字区域";
        }
        //判断图片大小与水印大小
        //if( ($ground_w<$w) || ($ground_h<$h) )
        // {
        //  echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
        //  return;
        // }
        switch ($waterPos) {
            case 0: //随机
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
            case 1: //1为顶端居左
                $posX = 0;
                $posY = 0;
                break;
            case 2: //2为顶端居中
                $posX = ($ground_w - $w) / 2;
                $posY = 0;
                break;
            case 3: //3为顶端居右
                $posX = $ground_w - $w;
                $posY = 0;
                break;
            case 4: //4为中部居左
                $posX = 0;
                $posY = ($ground_h - $h) / 2;
                break;
            case 5: //5为中部居中
                $posX = ($ground_w - $w) / 2;
                $posY = ($ground_h - $h) / 2;
                break;
            case 6: //6为中部居右
                $posX = $ground_w - $w;
                $posY = ($ground_h - $h) / 2;
                break;
            case 7: //7为底端居左
                $posX = 0;
                $posY = $ground_h - $h;
                break;
            case 8: //8为底端居中
                $posX = ($ground_w - $w) / 2;
                $posY = $ground_h - $h;
                break;
            case 9: //9为底端居右
                $posX = $ground_w - $w;
                $posY = $ground_h - $h;
                break;
            default: //随机
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
        }
        //设定图像的混色模式
        imagealphablending($ground_im, true);
        // 解决变黑色问题(仅在有图片水印时执行)
        if ($isWaterImage) {
            //解决变黑色问题
            $resize_im = imagecreatetruecolor($water_w, $water_h);
            // 下面三行是重点,解决png、gif透明背景变黑色的问题
            imagesavealpha($ground_im, true);
            imagealphablending($resize_im, false); //不合并颜色,直接用$im图像颜色替换,包括透明色;
            imagesavealpha($resize_im, true); //不要丢了$resize_im图像的透明色;
        }
        if ($isWaterImage) //图片水印
            {
            imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h); //拷贝水印到目标文件
        } else //文字水印
        {
            if (!empty($textColor) && (strlen($textColor) == 7)) {
                $R = hexdec(substr($textColor, 1, 2));
                $G = hexdec(substr($textColor, 3, 2));
                $B = hexdec(substr($textColor, 5));
            } else {
                die("水印文字颜色格式不正确!");
            }
            imagestring($ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate
                ($ground_im, $R, $G, $B));
        }
        //生成水印后的图片
        @unlink($groundImage);
        switch ($ground_info[2]) //取得背景图片的格式
            {
            case 1:
                imagegif($ground_im, $groundImage);
                break;
            case 2:
                imagejpeg($ground_im, $groundImage);
                break;
            case 3:
                imagepng($ground_im, $groundImage);
                break;
            default:
                die($errorMsg);
        }
        //释放内存
        if (isset($water_info))
            unset($water_info);
        if (isset($water_im))
            imagedestroy($water_im);
        unset($ground_info);
        imagedestroy($ground_im);
    }
    //---------------------------------------------------------------------------------------
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    // 在这之后执行您的代码
    $uploadfile = "images/651fa047ccf0c.jpg"; // 图片存放的路径
    $waterImage = ""; // 水印图片路径
    // 第一条文字水印
    imageWaterMark($uploadfile, 5, $waterImage, "水印1水印1水印1水印1水印1水印1水印1水印1", "5","#46FB04");
    echo "<img src=\"" . $uploadfile . "\" border=\"0\">";
    ?>还是不行,字体文件路径是正确的!
  • @Ta / 2023-10-06 / /

    @坏人,如果您的字体文件路径是正确的,但文字水印仍然无法显示在图片上,那么问题可能与字体文件或字体大小有关。以下是一些可能的解决方法:

    1. 字体文件问题: 确保字体文件不仅存在于指定的路径中,而且是有效的 TrueType 字体文件(.ttf 格式)。有时,字体文件可能损坏或不可用。您可以尝试使用其他 TrueType 字体文件来查看是否能够正常工作。

    2. 字体大小问题: 您在调用 imagettfbbox 函数时,使用了 ceil($textFont * 3) 来计算文字的大小。确保计算出的字体大小在合理范围内,并且不会使文字超出图像边界。您可以尝试不乘以 3,直接使用 $textFont

    $temp = imagettfbbox($textFont, 0, "ttff.ttf", $waterText);
    
    1. 文字颜色问题: 请确保 $textColor 参数的格式正确,应该是一个十六进制颜色值,例如 "#FF0000"(红色)。检查一下 $textColor 是否设置正确。

    2. 错误报告问题: 请确保您的 PHP 配置中没有禁用 TrueType 字体支持。在一些 PHP 配置中,可能需要启用或安装相关的库来支持 TrueType 字体。

    3. 字体文件路径问题: 确保 ttff.ttf 文件在脚本所在目录或指定路径中,并且文件名的大小写与实际文件名匹配。有时,文件名大小写不匹配可能导致文件无法找到。

    如果您按照上述建议仍然遇到问题,请尝试使用不同的 TrueType 字体文件,或者提供更多关于您的环境和字体文件的信息,以便更好地诊断问题。如果您使用的是特定的字体文件,您还可以尝试使用操作系统默认的 TrueType 字体文件,例如 "arial.ttf",以查看是否能够正常工作。

  • @Ta / 2023-10-06 / /
    @ChatGPT,把一个文字水印写入的imagestring测试代码给我测试!不要用外来函数
  • @Ta / 2023-10-06 / /

    @坏人,理解您的需求,以下是一个简单的示例代码,使用 imagestring 函数将文字水印添加到图像中:

    <?php
    // 创建一个背景图像
    $width = 400;
    $height = 200;
    $image = imagecreatetruecolor($width, $height);
    
    // 定义颜色
    $bgColor = imagecolorallocate($image, 255, 255, 255); // 背景颜色为白色
    $textColor = imagecolorallocate($image, 255, 0, 0); // 文字颜色为红色
    
    // 填充背景色
    imagefill($image, 0, 0, $bgColor);
    
    // 文字水印内容
    $watermarkText = "Watermark";
    
    // 字体文件路径
    $fontFile = "arial.ttf"; // 请确保此文件存在于脚本所在目录或指定路径中
    
    // 字体大小
    $fontSize = 20;
    
    // 水印位置
    $watermarkX = 20;
    $watermarkY = 50;
    
    // 将文字水印写入图像
    imagestring($image, $fontSize, $watermarkX, $watermarkY, $watermarkText, $textColor);
    
    // 输出图像
    header("Content-type: image/png");
    imagepng($image);
    
    // 释放图像资源
    imagedestroy($image);
    ?>
    

    这个示例创建了一个白色背景的图像,然后使用 imagestring 函数将红色文字水印添加到图像上。您需要提供正确的字体文件路径($fontFile 变量)以及其他所需的参数。在上述代码中,水印位置由 $watermarkX$watermarkY 变量控制。您可以根据需要调整这些参数以及其他图像属性。

  • @Ta / 2023-10-06 / /
    @ChatGPT,<?php
    /*
    * author 张长伟
    * date   2009-3-27 16:30:12
    * 功能: PHP图片水印 (水印支持图片) 
    * 参数: 
    *      $groundImage   背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式; 
    *      $waterPos      水印位置,有10种状态,0为随机位置; 
    *                     1为顶端居左,2为顶端居中,3为顶端居右; 
    *                     4为中部居左,5为中部居中,6为中部居右; 
    *                     7为底端居左,8为底端居中,9为底端居右; 
    *      $waterImage    图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式; 
    *      $waterText     文字水印,即把文字作为为水印,支持ASCII码,不支持中文; 
    *      $textFont      文字大小,值为1、2、3、4或5,默认为5; 
    *      $textColor     文字颜色,值为十六进制颜色值,默认为#FF0000(红色); 

    * 注意:
    *      Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG 
    *      $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。 
    *      当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。 
    *      加水印后的图片的文件名和 $groundImage 一样。  
    */
    function imageWaterMark($groundImage, $waterPos = 9, $waterImage = "", $waterText =
        "", $textFont = 1, $textColor = "#FF0000")
    {
        $isWaterImage = false;
        $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
        //读取水印文件
        if (!empty($waterImage) && file_exists($waterImage)) {
            $isWaterImage = true;
            $water_info = getimagesize($waterImage);
            $water_w = $water_info[0]; //取得水印图片的宽
            $water_h = $water_info[1]; //取得水印图片的高
            switch ($water_info[2]) //取得水印图片的格式
                {
                case 1:
                    $water_im = imagecreatefromgif($waterImage);
                    break;
                case 2:
                    $water_im = imagecreatefromjpeg($waterImage);
                    break;
                case 3:
                    $water_im = imagecreatefrompng($waterImage);
                    break;
                default:
                    die($formatMsg);
            }
        }
        //读取背景图片
        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:
                    $ground_im = imagecreatefromgif($groundImage);
                    break;
                case 2:
                    $ground_im = imagecreatefromjpeg($groundImage);
                    break;
                case 3:
                    $ground_im = imagecreatefrompng($groundImage);
                    break;
                default:
                    die($formatMsg);
            }
        } else {
            die("需要加水印的图片不存在!");
        }
        $water_w = 0; // 初始化水印宽度
        $water_h = 0; // 初始化水印高度
        //水印位置
        if ($isWaterImage) //图片水印
            {
            $water_w = $water_info[0];
            $water_h = $water_info[1];
            $w = $water_w;
            $h = $water_h;
            $label = "图片的";
        } else //文字水印
        {
            $temp = imagettfbbox(ceil($textFont * 3), 0, "ttff.ttf", $waterText); //取得使用 TrueType 字体的文本的范围
            $w = $temp[2] - $temp[6];
            $h = $temp[3] - $temp[7];
            unset($temp);
            $label = "文字区域";
        }
        //判断图片大小与水印大小
        //if( ($ground_w<$w) || ($ground_h<$h) )
        // {
        //  echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
        //  return;
        // }
        switch ($waterPos) {
            case 0: //随机
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
            case 1: //1为顶端居左
                $posX = 0;
                $posY = 0;
                break;
            case 2: //2为顶端居中
                $posX = ($ground_w - $w) / 2;
                $posY = 0;
                break;
            case 3: //3为顶端居右
                $posX = $ground_w - $w;
                $posY = 0;
                break;
            case 4: //4为中部居左
                $posX = 0;
                $posY = ($ground_h - $h) / 2;
                break;
            case 5: //5为中部居中
                $posX = ($ground_w - $w) / 2;
                $posY = ($ground_h - $h) / 2;
                break;
            case 6: //6为中部居右
                $posX = $ground_w - $w;
                $posY = ($ground_h - $h) / 2;
                break;
            case 7: //7为底端居左
                $posX = 0;
                $posY = $ground_h - $h;
                break;
            case 8: //8为底端居中
                $posX = ($ground_w - $w) / 2;
                $posY = $ground_h - $h;
                break;
            case 9: //9为底端居右
                $posX = $ground_w - $w;
                $posY = $ground_h - $h;
                break;
            default: //随机
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
        }
        //设定图像的混色模式
        imagealphablending($ground_im, true);
        // 解决变黑色问题(仅在有图片水印时执行)
        if ($isWaterImage) {
            //解决变黑色问题
            $resize_im = imagecreatetruecolor($water_w, $water_h);
            // 下面三行是重点,解决png、gif透明背景变黑色的问题
            imagesavealpha($ground_im, true);
            imagealphablending($resize_im, false); //不合并颜色,直接用$im图像颜色替换,包括透明色;
            imagesavealpha($resize_im, true); //不要丢了$resize_im图像的透明色;
        }
        if ($isWaterImage) //图片水印
            {
            imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h); //拷贝水印到目标文件
        } else //文字水印
        {
            if (!empty($textColor) && (strlen($textColor) == 7)) {
                $R = hexdec(substr($textColor, 1, 2));
                $G = hexdec(substr($textColor, 3, 2));
                $B = hexdec(substr($textColor, 5));
            } else {
                die("水印文字颜色格式不正确!");
            }
            imagestring($ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate
                ($ground_im, $R, $G, $B));

        }
        //生成水印后的图片
        @unlink($groundImage);
        switch ($ground_info[2]) //取得背景图片的格式
            {
            case 1:
                imagegif($ground_im, $groundImage);
                break;
            case 2:
                imagejpeg($ground_im, $groundImage);
                break;
            case 3:
                imagepng($ground_im, $groundImage);
                break;
            default:
                die($errorMsg);
        }
        //释放内存
        if (isset($water_info))
            unset($water_info);
        if (isset($water_im))
            imagedestroy($water_im);
        unset($ground_info);
        imagedestroy($ground_im);
    }
    //---------------------------------------------------------------------------------------
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    // 在这之后执行您的代码
    $uploadfile = "images/651fa047ccf0c.jpg"; // 图片存放的路径
    $waterImage = ""; // 水印图片路径
    // 第一条文字水印
    imageWaterMark($uploadfile, 5, $waterImage, "水印1水印1水印1水印1水印1水印1水印1水印1", "5",
        "#46FB04");
    echo "<img src=\"" . $uploadfile . "\" border=\"0\">";
    ?>还是不能打印出文字水印.
  • @Ta / 2023-10-06 / /

    @坏人,我注意到在您的代码中,您使用了 imagestring 函数来添加文字水印。但是,在此函数中,您指定的字体文件路径是 "ttff.ttf",这可能不是您系统中的正确字体文件路径。如果字体文件路径不正确,文字水印将无法显示。

    请确保以下几点:

    1. 您已经上传了正确的字体文件到服务器。
    2. 您已经提供了正确的字体文件路径。通常,字体文件的扩展名是 .ttf
    3. 如果字体文件不在与脚本相同的目录中,您需要提供正确的绝对路径。

    您可以使用以下代码测试文字水印是否正常工作,确保字体文件路径正确:

    <?php
    $groundImage = "images/651fa047ccf0c.jpg"; // 背景图片路径
    $fontFile = "arial.ttf"; // 字体文件路径,请确保该文件存在并正确配置
    $text = "水印文字"; // 水印文字内容
    
    $watermarkImage = imagecreatefromjpeg($groundImage);
    
    // 设置水印文字颜色
    $color = imagecolorallocate($watermarkImage, 255, 0, 0); // 红色
    
    // 设置字体大小
    $fontSize = 20;
    
    // 设置水印位置
    $x = 20; // 水印的 x 坐标
    $y = 50; // 水印的 y 坐标
    
    // 向图像添加文字水印
    imagettftext($watermarkImage, $fontSize, 0, $x, $y, $color, $fontFile, $text);
    
    // 输出带有水印的图像
    header("Content-type: image/jpeg");
    imagejpeg($watermarkImage);
    
    // 释放图像资源
    imagedestroy($watermarkImage);
    ?>
    

    在这个示例中,请确保提供了正确的字体文件路径 ($fontFile),字体大小 ($fontSize) 和水印位置 ($x$y)。这应该能够正确地将文字水印添加到图像上。如果字体文件路径仍然有问题,您可能需要提供正确的路径或使用字体文件的绝对路径。

  • @Ta / 2023-10-06 / /
    函数中字体文件路径是 "ttff.ttf"是正确的已经试过可以用
  • @Ta / 2023-10-06 / /
    @ChatGPT,函数中字体文件路径是 "ttff.ttf"是正确的已经试过可以用
  • @Ta / 2023-10-06 / /

    @坏人,如果您已确认字体文件路径 "ttff.ttf" 是正确的,并且在您的环境中正常工作,但仍然无法打印出文字水印,那么问题可能与其他因素有关。以下是一些可能导致无法打印文字水印的原因:

    1. GD 库支持:请确保您的 PHP 安装中启用了 GD 库,以支持图像处理功能。您可以通过 phpinfo() 函数来检查 GD 库是否启用。

    2. 字体存在性:检查 "ttff.ttf" 字体文件是否确实存在于与您的脚本相同的目录中。或者,您可以尝试使用字体文件的绝对路径,以确保脚本能够访问它。

    3. 错误报告:请检查是否存在 PHP 错误或警告消息,这可能会提供有关问题的更多信息。您可以通过查看 PHP 错误日志或启用错误显示来获取这些信息。

    4. 图像输出:确保您的脚本具有输出图像的权限,并且没有其他代码将图像输出清除或覆盖。

    5. GD 库版本:某些 GD 库版本可能会对字体支持有不同的要求。您可以尝试升级或降级 GD 库的版本,以查看是否会影响结果。

    如果您仍然无法解决问题,请提供更多关于您的 PHP 环境和运行代码的详细信息,以便我能够更好地帮助您找到问题的原因。

  • @Ta / 2023-10-06 / /
    @ChatGPT,<?php
    /*
    * author 张长伟
    * date   2009-3-27 16:30:12
    * 功能: PHP图片水印 (水印支持图片) 
    * 参数: 
    *      $groundImage   背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式; 
    *      $waterPos      水印位置,有10种状态,0为随机位置; 
    *                     1为顶端居左,2为顶端居中,3为顶端居右; 
    *                     4为中部居左,5为中部居中,6为中部居右; 
    *                     7为底端居左,8为底端居中,9为底端居右; 
    *      $waterImage    图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式; 
    *      $waterText     文字水印,即把文字作为为水印,支持ASCII码,不支持中文; 
    *      $textFont      文字大小,值为1、2、3、4或5,默认为5; 
    *      $textColor     文字颜色,值为十六进制颜色值,默认为#FF0000(红色); 

    * 注意:
    *      Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG 
    *      $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。 
    *      当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。 
    *      加水印后的图片的文件名和 $groundImage 一样。  
    */
    function imageWaterMark($groundImage, $waterPos = 9, $waterImage = "", $waterText =
        "", $textFont = 1, $textColor = "#FF0000")
    {
        $isWaterImage = false;
        $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
        //读取水印文件
        if (!empty($waterImage) && file_exists($waterImage)) {
            $isWaterImage = true;
            $water_info = getimagesize($waterImage);
            $water_w = $water_info[0]; //取得水印图片的宽
            $water_h = $water_info[1]; //取得水印图片的高
            switch ($water_info[2]) //取得水印图片的格式
                {
                case 1:
                    $water_im = imagecreatefromgif($waterImage);
                    break;
                case 2:
                    $water_im = imagecreatefromjpeg($waterImage);
                    break;
                case 3:
                    $water_im = imagecreatefrompng($waterImage);
                    break;
                default:
                    die($formatMsg);
            }
        }
        //读取背景图片
        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:
                    $ground_im = imagecreatefromgif($groundImage);
                    break;
                case 2:
                    $ground_im = imagecreatefromjpeg($groundImage);
                    break;
                case 3:
                    $ground_im = imagecreatefrompng($groundImage);
                    break;
                default:
                    die($formatMsg);
            }
        } else {
            die("需要加水印的图片不存在!");
        }
        $water_w = 0; // 初始化水印宽度
        $water_h = 0; // 初始化水印高度
        //水印位置
        if ($isWaterImage) //图片水印
            {
            $water_w = $water_info[0];
            $water_h = $water_info[1];
            $w = $water_w;
            $h = $water_h;
            $label = "图片的";
        } else //文字水印
        {
            $temp = imagettfbbox(ceil($textFont * 3), 0, "ttff.ttf", $waterText); //取得使用 TrueType 字体的文本的范围
            $w = $temp[2] - $temp[6];
            $h = $temp[3] - $temp[7];
            unset($temp);
            $label = "文字区域";
        }
        //判断图片大小与水印大小
        //if( ($ground_w<$w) || ($ground_h<$h) )
        // {
        //  echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
        //  return;
        // }
        switch ($waterPos) {
            case 0: //随机
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
            case 1: //1为顶端居左
                $posX = 0;
                $posY = 0;
                break;
            case 2: //2为顶端居中
                $posX = ($ground_w - $w) / 2;
                $posY = 0;
                break;
            case 3: //3为顶端居右
                $posX = $ground_w - $w;
                $posY = 0;
                break;
            case 4: //4为中部居左
                $posX = 0;
                $posY = ($ground_h - $h) / 2;
                break;
            case 5: //5为中部居中
                $posX = ($ground_w - $w) / 2;
                $posY = ($ground_h - $h) / 2;
                break;
            case 6: //6为中部居右
                $posX = $ground_w - $w;
                $posY = ($ground_h - $h) / 2;
                break;
            case 7: //7为底端居左
                $posX = 0;
                $posY = $ground_h - $h;
                break;
            case 8: //8为底端居中
                $posX = ($ground_w - $w) / 2;
                $posY = $ground_h - $h;
                break;
            case 9: //9为底端居右
                $posX = $ground_w - $w;
                $posY = $ground_h - $h;
                break;
            default: //随机
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
        }
        //设定图像的混色模式
        imagealphablending($ground_im, true);
        // 解决变黑色问题(仅在有图片水印时执行)
        if ($isWaterImage) {
            //解决变黑色问题
            $resize_im = imagecreatetruecolor($water_w, $water_h);
            // 下面三行是重点,解决png、gif透明背景变黑色的问题
            imagesavealpha($ground_im, true);
            imagealphablending($resize_im, false); //不合并颜色,直接用$im图像颜色替换,包括透明色;
            imagesavealpha($resize_im, true); //不要丢了$resize_im图像的透明色;
        }
        if ($isWaterImage) //图片水印
            {
            imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h); //拷贝水印到目标文件
        } else //文字水印
        {
            if (!empty($textColor) && (strlen($textColor) == 7)) {
                $R = hexdec(substr($textColor, 1, 2));
                $G = hexdec(substr($textColor, 3, 2));
                $B = hexdec(substr($textColor, 5));
            } else {
                die("水印文字颜色格式不正确!");
            }
            imagestring($ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate
                ($ground_im, $R, $G, $B));

        }
        //生成水印后的图片
        @unlink($groundImage);
        switch ($ground_info[2]) //取得背景图片的格式
            {
            case 1:
                imagegif($ground_im, $groundImage);
                break;
            case 2:
                imagejpeg($ground_im, $groundImage);
                break;
            case 3:
                imagepng($ground_im, $groundImage);
                break;
            default:
                die($errorMsg);
        }
        //释放内存
        if (isset($water_info))
            unset($water_info);
        if (isset($water_im))
            imagedestroy($water_im);
        unset($ground_info);
        imagedestroy($ground_im);
    }
    //---------------------------------------------------------------------------------------
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    // 在这之后执行您的代码
    $uploadfile = "images/651fa047ccf0c.jpg"; // 图片存放的路径
    $waterImage = ""; // 水印图片路径
    // 第一条文字水印
    imageWaterMark($uploadfile, 5, $waterImage, "水印1水印1水印1水印1水印1水印1水印1水印1", "5",
        "#46FB04");
    echo "<img src=\"" . $uploadfile . "\" border=\"0\">";
    ?>查验是那里有误
  • @Ta / 2023-10-06 / /

    @坏人,在您提供的代码中,有几个地方可能导致无法打印出文字水印:

    1. 字体路径:您在以下代码中指定了字体文件的路径:

      $temp = imagettfbbox(ceil($textFont * 3), 0, "ttff.ttf", $waterText);
      

      请确保 "ttff.ttf" 字体文件位于与您的脚本相同的目录中。如果字体文件不在同一目录中,您可以提供字体文件的绝对路径。

    2. GD 库中 FreeType 支持:确保您的 PHP GD 库已启用 FreeType 支持。FreeType 是用于处理 TrueType 字体的库,而文字水印需要使用 TrueType 字体。

    3. 字体大小:在以下代码中,您使用了 imagettfbbox 函数来计算文字水印的大小:

      $temp = imagettfbbox(ceil($textFont * 3), 0, "ttff.ttf", $waterText);
      

      请确保 $textFont 变量的值为有效的字体大小,例如 1、2、3、4 或 5。

    如果您已经确认上述问题都没有出现,但仍然无法打印文字水印,请尝试执行以下操作:

    1. 检查 PHP 错误日志:查看是否有任何 PHP 错误或警告消息。这些消息可能包含有关问题的更多详细信息。

    2. 以纯文本形式输出错误消息:在代码中添加一些 echo 语句,以便在执行脚本时输出调试信息,以查看是否存在问题。

    3. 使用默认字体:尝试在 imagettfbbox 函数中使用默认字体而不是 "ttff.ttf",并查看是否能够打印出文字水印。

    如果上述方法都没有解决问题,请提供任何出现的错误消息以及更多关于您的 PHP 环境的详细信息,以便我能够更好地帮助您找到问题的原因。

  • @Ta / 2023-10-06 / /
    @ChatGPT,把$waterText 转ASCII码    文字水印,即把文字作为为水印,支持ASCII码,不支持中文; 
  • @Ta / 2023-10-06 / /

    @坏人,要将文本转换为ASCII码,您可以使用PHP中的ord函数来获取文本中每个字符的ASCII码值。以下是将$waterText文本转换为ASCII码的示例代码:

    $waterText = "Hello"; // 你的水印文本
    $asciiText = '';
    
    for ($i = 0; $i < strlen($waterText); $i++) {
        $asciiText .= ord($waterText[$i]) . ' ';
    }
    
    echo $asciiText;
    

    在上面的示例中,我们遍历了$waterText中的每个字符,使用ord函数将每个字符转换为ASCII码,并将结果存储在$asciiText变量中。最后,我们使用echo语句打印ASCII码文本。

    请注意,这只是一个示例,您可以将$waterText替换为您实际要转换的文本。这将生成文本的ASCII码表示形式,您可以将其用作水印文本。

  • @Ta / 2023-10-06 / /
    @坏人,先把图片水印个代码全部先删了只保留文字水印写入的相关代码.<?php
    /*
    * author 张长伟
    * date   2009-3-27 16:30:12
    * 功能: PHP图片水印 (水印支持图片) 
    * 参数: 
    *      $groundImage   背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式; 
    *      $waterPos      水印位置,有10种状态,0为随机位置; 
    *                     1为顶端居左,2为顶端居中,3为顶端居右; 
    *                     4为中部居左,5为中部居中,6为中部居右; 
    *                     7为底端居左,8为底端居中,9为底端居右; 
    *      $waterImage    图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式; 
    *      $waterText     文字水印,即把文字作为为水印,支持ASCII码,不支持中文; 
    *      $textFont      文字大小,值为1、2、3、4或5,默认为5; 
    *      $textColor     文字颜色,值为十六进制颜色值,默认为#FF0000(红色); 

    * 注意:
    *      Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG 
    *      $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。 
    *      当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。 
    *      加水印后的图片的文件名和 $groundImage 一样。  
    */
    function imageWaterMark($groundImage, $waterPos = 9, $waterImage = "", $waterText =
        "", $textFont = 1, $textColor = "#FF0000")
    {
        $isWaterImage = false;
        $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
        //读取水印文件
        if (!empty($waterImage) && file_exists($waterImage)) {
            $isWaterImage = true;
            $water_info = getimagesize($waterImage);
            $water_w = $water_info[0]; //取得水印图片的宽
            $water_h = $water_info[1]; //取得水印图片的高
            switch ($water_info[2]) //取得水印图片的格式
                {
                case 1:
                    $water_im = imagecreatefromgif($waterImage);
                    break;
                case 2:
                    $water_im = imagecreatefromjpeg($waterImage);
                    break;
                case 3:
                    $water_im = imagecreatefrompng($waterImage);
                    break;
                default:
                    die($formatMsg);
            }
        }
        //读取背景图片
        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:
                    $ground_im = imagecreatefromgif($groundImage);
                    break;
                case 2:
                    $ground_im = imagecreatefromjpeg($groundImage);
                    break;
                case 3:
                    $ground_im = imagecreatefrompng($groundImage);
                    break;
                default:
                    die($formatMsg);
            }
        } else {
            die("需要加水印的图片不存在!");
        }
        $water_w = 0; // 初始化水印宽度
        $water_h = 0; // 初始化水印高度
        //水印位置
        if ($isWaterImage) //图片水印
            {
            $water_w = $water_info[0];
            $water_h = $water_info[1];
            $w = $water_w;
            $h = $water_h;
            $label = "图片的";
        } else //文字水印
        {
            $temp = imagettfbbox(ceil($textFont * 3), 0, "ttff.ttf", $waterText); //取得使用 TrueType 字体的文本的范围
            $w = $temp[2] - $temp[6];
            $h = $temp[3] - $temp[7];
            unset($temp);
            $label = "文字区域";
        }
        //判断图片大小与水印大小
        //if( ($ground_w<$w) || ($ground_h<$h) )
        // {
        //  echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
        //  return;
        // }
        switch ($waterPos) {
            case 0: //随机
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
            case 1: //1为顶端居左
                $posX = 0;
                $posY = 0;
                break;
            case 2: //2为顶端居中
                $posX = ($ground_w - $w) / 2;
                $posY = 0;
                break;
            case 3: //3为顶端居右
                $posX = $ground_w - $w;
                $posY = 0;
                break;
            case 4: //4为中部居左
                $posX = 0;
                $posY = ($ground_h - $h) / 2;
                break;
            case 5: //5为中部居中
                $posX = ($ground_w - $w) / 2;
                $posY = ($ground_h - $h) / 2;
                break;
            case 6: //6为中部居右
                $posX = $ground_w - $w;
                $posY = ($ground_h - $h) / 2;
                break;
            case 7: //7为底端居左
                $posX = 0;
                $posY = $ground_h - $h;
                break;
            case 8: //8为底端居中
                $posX = ($ground_w - $w) / 2;
                $posY = $ground_h - $h;
                break;
            case 9: //9为底端居右
                $posX = $ground_w - $w;
                $posY = $ground_h - $h;
                break;
            default: //随机
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
        }
        //设定图像的混色模式
        imagealphablending($ground_im, true);
        // 解决变黑色问题(仅在有图片水印时执行)
        if ($isWaterImage) {
            //解决变黑色问题
            $resize_im = imagecreatetruecolor($water_w, $water_h);
            // 下面三行是重点,解决png、gif透明背景变黑色的问题
            imagesavealpha($ground_im, true);
            imagealphablending($resize_im, false); //不合并颜色,直接用$im图像颜色替换,包括透明色;
            imagesavealpha($resize_im, true); //不要丢了$resize_im图像的透明色;
        }
        if ($isWaterImage) //图片水印
            {
            imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h); //拷贝水印到目标文件
        } else //文字水印
        {
            if (!empty($textColor) && (strlen($textColor) == 7)) {
                $R = hexdec(substr($textColor, 1, 2));
                $G = hexdec(substr($textColor, 3, 2));
                $B = hexdec(substr($textColor, 5));
            } else {
                die("水印文字颜色格式不正确!");
            }
            imagestring($ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate
                ($ground_im, $R, $G, $B));

            // 将文字水印写入图像
            imagestring($ground_im, 20, 20, 50, "eeeeeeeeeeeeeeee", imagecolorallocate($ground_im,255, 0, 0));

        }
        //生成水印后的图片
        @unlink($groundImage);
        switch ($ground_info[2]) //取得背景图片的格式
            {
            case 1:
                imagegif($ground_im, $groundImage);
                break;
            case 2:
                imagejpeg($ground_im, $groundImage);
                break;
            case 3:
                imagepng($ground_im, $groundImage);
                break;
            default:
                die($errorMsg);
        }
        //释放内存
        if (isset($water_info))
            unset($water_info);
        if (isset($water_im))
            imagedestroy($water_im);
        unset($ground_info);
        imagedestroy($ground_im);
    }
    //---------------------------------------------------------------------------------------
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    // 在这之后执行您的代码
    $uploadfile = "images/651fa047ccf0c.jpg"; // 图片存放的路径
    $waterImage = ""; // 水印图片路径
    // 第一条文字水印
    $waterText = "Hello你的水印文本"; // 你的水印文本
    $asciiText = '';

    for ($i = 0; $i < strlen($waterText); $i++) {
        $asciiText .= ord($waterText[$i]) . ' ';
    }

    echo $asciiText;
    imageWaterMark($uploadfile, 5, $waterImage, $asciiText, "5",
        "#46FB04");
    echo "<img src=\"" . $uploadfile . "\" border=\"0\">";
    ?>
    <?php
    /**
     * // 创建一个背景图像
     * $width = 400;
     * $height = 200;
     * $image = imagecreatetruecolor($width, $height);

     * // 定义颜色
     * $bgColor = imagecolorallocate($image, 255, 255, 255); // 背景颜色为白色
     * $textColor = imagecolorallocate($image, 255, 0, 0); // 文字颜色为红色

     * // 填充背景色
     * imagefill($image, 0, 0, $bgColor);

     * // 文字水印内容
     * $watermarkText = "Watermark";

     * // 字体文件路径
     * $fontFile = "ttff.ttf"; // 请确保此文件存在于脚本所在目录或指定路径中

     * // 字体大小
     * $fontSize = 20;

     * // 水印位置
     * $watermarkX = 20;
     * $watermarkY = 50;

     * // 将文字水印写入图像
     * imagestring($image, 20, 20, 50, "eeeeeeeeeeeeeeee", imagecolorallocate($image, 255, 0, 0));
    imagestring($image, $fontSize, $watermarkX, $watermarkY, $watermarkText, $textColor);

     * // 输出图像
     * header("Content-type: image/png");
     * imagepng($image);

     * // 释放图像资源
     * imagedestroy($image);
     */
    ?>
  • @Ta / 2023-10-06 / /
    @ChatGPT,先把图片水印个代码全部先删了只保留文字水印写入的相关代码.<?php
    /*
    * author 张长伟
    * date   2009-3-27 16:30:12
    * 功能: PHP图片水印 (水印支持图片) 
    * 参数: 
    *      $groundImage   背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式; 
    *      $waterPos      水印位置,有10种状态,0为随机位置; 
    *                     1为顶端居左,2为顶端居中,3为顶端居右; 
    *                     4为中部居左,5为中部居中,6为中部居右; 
    *                     7为底端居左,8为底端居中,9为底端居右; 
    *      $waterImage    图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式; 
    *      $waterText     文字水印,即把文字作为为水印,支持ASCII码,不支持中文; 
    *      $textFont      文字大小,值为1、2、3、4或5,默认为5; 
    *      $textColor     文字颜色,值为十六进制颜色值,默认为#FF0000(红色); 

    * 注意:
    *      Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG 
    *      $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。 
    *      当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。 
    *      加水印后的图片的文件名和 $groundImage 一样。  
    */
    function imageWaterMark($groundImage, $waterPos = 9, $waterImage = "", $waterText =
        "", $textFont = 1, $textColor = "#FF0000")
    {
        $isWaterImage = false;
        $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
        //读取水印文件
        if (!empty($waterImage) && file_exists($waterImage)) {
            $isWaterImage = true;
            $water_info = getimagesize($waterImage);
            $water_w = $water_info[0]; //取得水印图片的宽
            $water_h = $water_info[1]; //取得水印图片的高
            switch ($water_info[2]) //取得水印图片的格式
                {
                case 1:
                    $water_im = imagecreatefromgif($waterImage);
                    break;
                case 2:
                    $water_im = imagecreatefromjpeg($waterImage);
                    break;
                case 3:
                    $water_im = imagecreatefrompng($waterImage);
                    break;
                default:
                    die($formatMsg);
            }
        }
        //读取背景图片
        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:
                    $ground_im = imagecreatefromgif($groundImage);
                    break;
                case 2:
                    $ground_im = imagecreatefromjpeg($groundImage);
                    break;
                case 3:
                    $ground_im = imagecreatefrompng($groundImage);
                    break;
                default:
                    die($formatMsg);
            }
        } else {
            die("需要加水印的图片不存在!");
        }
        $water_w = 0; // 初始化水印宽度
        $water_h = 0; // 初始化水印高度
        //水印位置
        if ($isWaterImage) //图片水印
            {
            $water_w = $water_info[0];
            $water_h = $water_info[1];
            $w = $water_w;
            $h = $water_h;
            $label = "图片的";
        } else //文字水印
        {
            $temp = imagettfbbox(ceil($textFont * 3), 0, "ttff.ttf", $waterText); //取得使用 TrueType 字体的文本的范围
            $w = $temp[2] - $temp[6];
            $h = $temp[3] - $temp[7];
            unset($temp);
            $label = "文字区域";
        }
        //判断图片大小与水印大小
        //if( ($ground_w<$w) || ($ground_h<$h) )
        // {
        //  echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
        //  return;
        // }
        switch ($waterPos) {
            case 0: //随机
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
            case 1: //1为顶端居左
                $posX = 0;
                $posY = 0;
                break;
            case 2: //2为顶端居中
                $posX = ($ground_w - $w) / 2;
                $posY = 0;
                break;
            case 3: //3为顶端居右
                $posX = $ground_w - $w;
                $posY = 0;
                break;
            case 4: //4为中部居左
                $posX = 0;
                $posY = ($ground_h - $h) / 2;
                break;
            case 5: //5为中部居中
                $posX = ($ground_w - $w) / 2;
                $posY = ($ground_h - $h) / 2;
                break;
            case 6: //6为中部居右
                $posX = $ground_w - $w;
                $posY = ($ground_h - $h) / 2;
                break;
            case 7: //7为底端居左
                $posX = 0;
                $posY = $ground_h - $h;
                break;
            case 8: //8为底端居中
                $posX = ($ground_w - $w) / 2;
                $posY = $ground_h - $h;
                break;
            case 9: //9为底端居右
                $posX = $ground_w - $w;
                $posY = $ground_h - $h;
                break;
            default: //随机
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
        }
        //设定图像的混色模式
        imagealphablending($ground_im, true);
        // 解决变黑色问题(仅在有图片水印时执行)
        if ($isWaterImage) {
            //解决变黑色问题
            $resize_im = imagecreatetruecolor($water_w, $water_h);
            // 下面三行是重点,解决png、gif透明背景变黑色的问题
            imagesavealpha($ground_im, true);
            imagealphablending($resize_im, false); //不合并颜色,直接用$im图像颜色替换,包括透明色;
            imagesavealpha($resize_im, true); //不要丢了$resize_im图像的透明色;
        }
        if ($isWaterImage) //图片水印
            {
            imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h); //拷贝水印到目标文件
        } else //文字水印
        {
            if (!empty($textColor) && (strlen($textColor) == 7)) {
                $R = hexdec(substr($textColor, 1, 2));
                $G = hexdec(substr($textColor, 3, 2));
                $B = hexdec(substr($textColor, 5));
            } else {
                die("水印文字颜色格式不正确!");
            }
            imagestring($ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate
                ($ground_im, $R, $G, $B));

            // 将文字水印写入图像
            imagestring($ground_im, 20, 20, 50, "eeeeeeeeeeeeeeee", imagecolorallocate($ground_im,255, 0, 0));

        }
        //生成水印后的图片
        @unlink($groundImage);
        switch ($ground_info[2]) //取得背景图片的格式
            {
            case 1:
                imagegif($ground_im, $groundImage);
                break;
            case 2:
                imagejpeg($ground_im, $groundImage);
                break;
            case 3:
                imagepng($ground_im, $groundImage);
                break;
            default:
                die($errorMsg);
        }
        //释放内存
        if (isset($water_info))
            unset($water_info);
        if (isset($water_im))
            imagedestroy($water_im);
        unset($ground_info);
        imagedestroy($ground_im);
    }
    //---------------------------------------------------------------------------------------
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    // 在这之后执行您的代码
    $uploadfile = "images/651fa047ccf0c.jpg"; // 图片存放的路径
    $waterImage = ""; // 水印图片路径
    // 第一条文字水印
    $waterText = "Hello你的水印文本"; // 你的水印文本
    $asciiText = '';

    for ($i = 0; $i < strlen($waterText); $i++) {
        $asciiText .= ord($waterText[$i]) . ' ';
    }

    echo $asciiText;
    imageWaterMark($uploadfile, 5, $waterImage, $asciiText, "5",
        "#46FB04");
    echo "<img src=\"" . $uploadfile . "\" border=\"0\">";
    ?>
    <?php
    /**
     * // 创建一个背景图像
     * $width = 400;
     * $height = 200;
     * $image = imagecreatetruecolor($width, $height);

     * // 定义颜色
     * $bgColor = imagecolorallocate($image, 255, 255, 255); // 背景颜色为白色
     * $textColor = imagecolorallocate($image, 255, 0, 0); // 文字颜色为红色

     * // 填充背景色
     * imagefill($image, 0, 0, $bgColor);

     * // 文字水印内容
     * $watermarkText = "Watermark";

     * // 字体文件路径
     * $fontFile = "ttff.ttf"; // 请确保此文件存在于脚本所在目录或指定路径中

     * // 字体大小
     * $fontSize = 20;

     * // 水印位置
     * $watermarkX = 20;
     * $watermarkY = 50;

     * // 将文字水印写入图像
     * imagestring($image, 20, 20, 50, "eeeeeeeeeeeeeeee", imagecolorallocate($image, 255, 0, 0));
    imagestring($image, $fontSize, $watermarkX, $watermarkY, $watermarkText, $textColor);

     * // 输出图像
     * header("Content-type: image/png");
     * imagepng($image);

     * // 释放图像资源
     * imagedestroy($image);
     */
    ?>
  • @Ta / 2023-10-06 / /

    @坏人,下面是删除了图片水印相关代码并保留文字水印写入的相关代码:

    <?php
    function imageWaterMark($groundImage, $waterText, $textFont = 5, $textColor = "#FF0000")
    {
        // 读取背景图片
        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:
                    $ground_im = imagecreatefromgif($groundImage);
                    break;
                case 2:
                    $ground_im = imagecreatefromjpeg($groundImage);
                    break;
                case 3:
                    $ground_im = imagecreatefrompng($groundImage);
                    break;
                default:
                    die("暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。");
            }
        } else {
            die("需要加水印的图片不存在!");
        }
    
        $temp = imagettfbbox(ceil($textFont * 3), 0, "ttff.ttf", $waterText); // 取得使用 TrueType 字体的文本的范围
        $w = $temp[2] - $temp[6];
        $h = $temp[3] - $temp[7];
        unset($temp);
    
        // 文字水印位置
        $posX = ($ground_w - $w) / 2;
        $posY = ($ground_h - $h) / 2;
    
        // 设定图像的混色模式
        imagealphablending($ground_im, true);
    
        if (!empty($textColor) && (strlen($textColor) == 7)) {
            $R = hexdec(substr($textColor, 1, 2));
            $G = hexdec(substr($textColor, 3, 2));
            $B = hexdec(substr($textColor, 5));
        } else {
            die("水印文字颜色格式不正确!");
        }
    
        // 将文字水印写入图像
        imagestring($ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate
        ($ground_im, $R, $G, $B));
    
        // 生成水印后的图片
        @unlink($groundImage);
    
        switch ($ground_info[2]) // 取得背景图片的格式
        {
            case 1:
                imagegif($ground_im, $groundImage);
                break;
            case 2:
                imagejpeg($ground_im, $groundImage);
                break;
            case 3:
                imagepng($ground_im, $groundImage);
                break;
            default:
                die("暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。");
        }
    
        // 释放内存
        unset($ground_info);
        imagedestroy($ground_im);
    }
    
    // 在这之后执行您的代码
    $uploadfile = "images/651fa047ccf0c.jpg"; // 图片存放的路径
    $waterText = "Hello你的水印文本"; // 你的水印文本
    
    imageWaterMark($uploadfile, $waterText, 5, "#46FB04");
    echo "<img src=\"" . $uploadfile . "\" border=\"0\">";
    ?>
    

    上面的代码删除了图片水印相关的部分,保留了文字水印写入的部分。在执行代码时,它将在背景图片上添加指定的文字水印,并输出带有水印的图像。

  • @Ta / 2023-10-06 / /
    @ChatGPT,文字水印还是不能打印水印
  • @Ta / 2023-10-06 / /

    @坏人,看起来代码中有一些问题,导致文字水印无法正确打印。我注意到以下几个可能引发问题的地方:

    1. 字体文件路径问题:确保 "ttff.ttf" 字体文件存在并且位于与脚本相同的目录,或者你可以提供正确的路径。

    2. 图片水印的问题:在代码中,如果 $isWaterImage 为 true,则执行图片水印相关的操作。这可能导致文字水印无法正确打印。你需要确保 $isWaterImage 的值为 false,以执行文字水印的相关操作。

    3. 水印位置问题:确保选择的水印位置适用于你的图片。在代码中,水印位置默认是居中的,你可以根据需要将其更改为其他位置。

    4. 图片格式问题:代码中要求背景图片和水印图片必须是 GIF、JPG 或 PNG 格式。确保你的背景图片是这些格式之一。

    5. 颜色问题:代码中使用了 $textColor 变量来指定文字水印的颜色。确保颜色值的格式是正确的十六进制颜色值,例如 "#FF0000" 代表红色。

    如果你仍然遇到问题,请检查并修复上述可能引发问题的地方。如果问题仍然存在,请提供更多关于错误或问题的信息,以便我可以帮助你更好地解决问题。

  • @Ta / 2023-10-06 / /
     @ChatGPT,解释下!这代码有误吗 $temp = imagettfbbox(ceil($textFont * 3), 0, "ttff.ttf", $waterText); // 取得使用 TrueType 字体的文本的范围
        $w = $temp[2] - $temp[6];
        $h = $temp[3] - $temp[7];
        unset($temp);
添加新回复
回复需要登录