【问题标题】:PHP - creating a PNG file with semi-transparent logo / watermarkPHP - 创建一个带有半透明徽标/水印的 PNG 文件
【发布时间】:2013-12-10 17:22:22
【问题描述】:

我正在尝试将徽标放入白色图像中并使其半透明以用作水印。

这是我的代码...

   // load the stamp and the photo to apply the watermark to
   if (file_exists($logoPath)) {

      $im = imagecreatefrompng($logoPath);
      $size = getimagesize($logoPath);

      $stamp = imagecreatetruecolor(612, 792);
      imagefilledrectangle($stamp, 0, 0, 612-1, 792-1, 0xFFFFFF);
      $sx = imagesx($stamp);
      $sy = imagesy($stamp);

      // center width and height
      $centerX=$sx/2-$size[0]/2;
      $centerY=$sy/2-$size[1]/2;

      $res=imagecopymerge($stamp, $im, $centerX,$centerY, 0, 0, $sx, $sy, 15);

      $waterPath = $watermark_path.$broker_id."_watermark.png";

      // Save the image to file and free memory
      imagepng($stamp, $waterPath);
      imagedestroy($stamp);
      imagedestroy($im);
   }

在我看来一切都很好,但是当我运行它时,我得到了这个......

http://i43.tinypic.com/2cyft06.jpg

...如您所见,由于某种原因,图像的右下角四边形被着色了。

【问题讨论】:

  • 我认为你需要对 alpha 做一些事情。
  • yeah..maybe..but the logo is is made transparent with the imagecopymerge ...它只是为图像“之后”出现的 $stamp 上的所有内容着色...很奇怪

标签: php gd


【解决方案1】:

如果您查看imagecopymerge() 文档,第 7 和第 8 个参数表示源图像的宽度和高度量。您似乎通过了目标图像高度 (612, 792),所以基本上您正在尝试从您的徽标图像中复制一个 612x792 切片,它看起来要小得多。

我会尝试更好地描述这些论点:

$res = imagecopymerge(
          $stamp,           // <- target image
          $im,              // <- source image, from where to copy (logo)
          $centerX,         // <- target x-position (where to place your logo), 
          $centerY,         // <- target y-position 
          0,                // <- source x-position (x-offset from where to start copy)
          0,                // <- source y-position
          imagesx($im),     // <- amount to copy from source (width)
          imagesy($im),     // <- amount... (height)
          15                // <- i have no idea what this is :)
        );

【讨论】:

  • ...还有...最后的15是透明度混合比...0是完全透明100是不透明:)
猜你喜欢
  • 2013-06-20
  • 2011-08-13
  • 2011-10-19
  • 2017-01-28
  • 2023-04-08
  • 2012-11-02
  • 2012-09-15
  • 1970-01-01
  • 2022-01-06
相关资源
最近更新 更多