PHP - 创建一个带有半透明徽标/水印的PNG文件

时间:2013-12-10 17:22:23

标签: php gd

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

这是我的代码......

   // 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

...正如您所看到的,图像的右下方因某种原因而变色。

1 个答案:

答案 0 :(得分: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 :)
        );