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


PHP - creating a PNG file with semi-transparent logo / watermark

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

这是我的密码。。。

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

正如您所看到的,由于某种原因,图像的右下象限正在变色。

如果您查看imagecopymerge()文档,第7个和第8个参数表示源图像的宽度和高度。你似乎超过了目标图像的高度(612792),所以基本上你试图从你的标志图像中复制一个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 :)
        );