【问题标题】:Watermarking a .png, .gif or .jpg with a .png, .gif or .jpg使用 .png、.gif 或 .jpg 为 .png、.gif 或 .jpg 加水印
【发布时间】:2012-03-31 23:41:03
【问题描述】:

我正在尝试编写一个函数,该函数将为任何图像(.png、.jpg 或 .gif)加上可能也是其中一种格式的另一个图像。我希望这两个图像都保持透明度。我不关心 gif 动画。不幸的是,水印在它不应该透明的地方变得透明,并且在它所在的地方并没有完全透明。我使用了一些可用的随机图像来测试这个脚本,我的结果如下所示:

虽然水印图像看起来像这样:

这是我的代码:

  function watermark_get_left()
  {
    return NCFG_TEMPLATE_DIR . 'images/watermark_left.png';
  }

  function watermark_get_right()
  {
    return NCFG_TEMPLATE_DIR . 'images/watermark_right.png';
  }

  function create_img($path, $type)
  {
    switch($type)
    {
      case NCFG_IMAGE_JPG:
        $img = imagecreatefromjpeg($path);
        return $img;
      case NCFG_IMAGE_GIF:
        $img = imagecreatefromgif($path);
        return $img;
      case NCFG_IMAGE_PNG:
        $img = imagecreatefrompng($path);
        imagealphablending($img, false);
        imagesavealpha($img, true);
        return $img;
      default:
        return FALSE;
    }
  }

  function img_transparency($new_img, $img, $type)
  {
    if($type == NCFG_IMAGE_GIF)
    {
      $transparent_index = imagecolortransparent($img);

      if($transparent_index != -1)
      {
        $transparent_color = imagecolorsforindex($img, $transparent_index);
        $transparent_new = imagecolorallocate($new_img, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
        $transparent_new_index = imagecolortransparent($new_img, $transparent_new);
        imagefill($new_img, 0, 0, $transparent_new_index);
      }
    }else if($type == NCFG_IMAGE_PNG){
      imagecolortransparent($new_img, imagecolorallocatealpha($new_img, 0, 0, 0, 127));
      imagealphablending($new_img, false);
      imagesavealpha($new_img, true);
    }
  }

  function image_watermark($path, $new_path, &$w_height)
  {
    $type = get_image_type($path);
    list($width, $height) = getimagesize($path);

    if(!($img = create_img($path, $type)))
    {
      return FALSE;
    }

    $l_path = watermark_get_left();
    $r_path = watermark_get_right();

    if($l_exists = file_exists($l_path))
    {
      $l_type = get_image_type($l_path);
      list($l_width, $l_height) = getimagesize($l_path);

      if(!($l_img = create_img($l_path, $l_type)))
      {
        return FALSE;
      }
    }

    if($r_exists = file_exists($r_path))
    {
      $r_type = get_image_type($r_path);
      list($r_width, $r_height) = getimagesize($r_path);

      if(!($r_img = create_img($r_path, $r_type)))
      {
        return FALSE;
      }
    }

    $w_height = max(($r_exists ? $r_height : 0), ($l_exists ? $l_height : 0));

    echo("w_height: $w_height<br/>");

    $new_width = $width;
    $new_height = $height + $w_height;

    $new_img = imagecreatetruecolor($new_width, $new_height);

    $bg_color = imagecolorallocate($new_img, 255,255,255);
    imagefilledrectangle($new_img, 0, $height, $new_width - 1, $new_height - 1, $bg_color);

    img_transparency($new_img, $img, $type);
    imagecopy($new_img, $img, 0, 0, 0, 0, $width, $height);

    if($l_exists)
    {
      imagecopy($new_img, $l_img, 0, $height, 0, 0, $l_width, $l_height);
    }

    if($r_exists)
    {
      imagecopy($new_img, $r_img, $new_width - $r_width, $height, 0, 0, $r_width, $r_height);
    }

    switch($type)
    {
      case NCFG_IMAGE_JPG:
        imagejpeg($new_img, $new_path);
        break;
      case NCFG_IMAGE_GIF:
        imagegif($new_img, $new_path);
        break;
      case NCFG_IMAGE_PNG:
        imagepng($new_img, $new_path, 9);
        break;
    }

    imagedestroy($new_img);
    imagedestroy($img);

    return TRUE;
  }

我也尝试了 imagecopymerge,但无济于事。我将不胜感激。

编辑:我的图像显示在这里,与我的预期不同。带水印的图像在白色背景上看起来正确,但在任何其他背景上都不正确。

【问题讨论】:

  • 我希望这两个图像都保持透明度。我不在乎透明度。
  • 据我所知,水印应该是半透明的。因此,您不能使用 jpg 作为水印,因为它没有透明度。这真的很令人困惑:“我希望这两个图像都保持透明度。我不在乎透明度。”你要还是不要?
  • 对不起,我想写“我不关心 gif 动画”……我已经写了太久了。
  • 我在底部嵌入了我的水印,在扩展图像的高度之后。
  • 刷子非常适合将透明图像复制到另一张图像上,请参阅php.net/manual/en/function.imagesetbrush.php - 根据我的经验,imagecopy 等通常无法按预期工作。

标签: php transparency gd watermark


【解决方案1】:

您可以为此使用 ImageMagick CLI。这是一个班轮:

composite -dissolve 50% -geometry +20+10 -gravity SouthWest watermark.png input.png output.png

这会将watermark.png 放置在input.png 之上,SouthWest 角的不透明度为50%,偏移为+20, +10 像素。您可以根据自己的喜好对其进行调整并在 PHP 中使用 shell_exec:

shell_exec('composite ...');

参考:http://www.imagemagick.org/script/command-line-options.php

示例图片:

编辑: 如果您还想扩展原始图像的高度(例如 20 像素),您需要先执行此操作:

convert original.png -bordercolor transparent -border 0x20 -background transparent -gravity NorthWest -extent +0+20 input.png

【讨论】:

  • 不幸的是,我的服务器上没有安装 ImageMagick。我也无权安装它。
猜你喜欢
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 2012-12-21
  • 2011-08-05
  • 2011-05-11
  • 2014-05-31
  • 2011-08-04
  • 1970-01-01
相关资源
最近更新 更多