代码之家  ›  专栏  ›  技术社区  ›  SigGP

PHP gd-水印-如何保存图像?

  •  0
  • SigGP  · 技术社区  · 9 年前

    我在服务器上有图像。我想在图像上做一个水印(文本),并用其他名称将此图像保存为新图像。我的功能是:

    function watermark($path, $watermark){
        $imageURL = $path;
        list($width,$height) = getimagesize($imageURL);
        $imageProperties = imagecreatetruecolor($width, $height);
        $targetLayer = imagecreatefromjpeg($imageURL);
        imagecopyresampled($imageProperties, $targetLayer, 0, 0, 0, 0, $width,      $height, $width, $height);
        $WaterMarkText = $watermark;
        $watermarkColor = imagecolorallocate($imageProperties, 191,191,191);
        imagestring($imageProperties, 5, 130, 117, $WaterMarkText, $watermarkColor);
        imagejpeg ($imageProperties);
        imagedestroy($targetLayer);
        imagedestroy($imageProperties);
    }
    

    其中参数为:

    $watermark = $_POST['watermark'];
    $path = "/images/$file_name";
    

    当我启动scypt时,带有水印的图像正在创建并显示在屏幕上。我的目标是不显示新图像,而是将其保存在名为$file_name_watermark的同一文件夹中。我该怎么做?

    1 回复  |  直到 9 年前
        1
  •  2
  •   Mitya    9 年前

    docs 在…上 imagejpeg() :

    filename

    将文件保存到的路径。如果未设置或为NULL,则原始图像流 将直接输出。

    因此:

    imagejpeg ($imageProperties, 'some/path.jpg');