简体   繁体   English

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

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

I'm trying to place a logo into a white image and make it semi-transparent to be used as a watermark. 我正在尝试将徽标放入白色图像中,并使其半透明以用作水印。

Here is my code... 这是我的代码...

   // 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);
   }

It all looks good to me but when I run it I get this... 对我来说一切都很好,但是当我运行它时,我得到了...

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

...as you can see, the lower right quad of the image is getting colored for some reason. ...如您所见,由于某种原因,图像的右下四角变色。

if you take a look at imagecopymerge() docs, the 7th and 8th arguments represent the source image width and height amount. 如果您查看imagecopymerge()文档,则第7个和第8个参数表示源图像的宽度和高度量。 You appear to pass the target image height (612, 792), so basically you're trying to copy a 612x792 slice from your logo image, which looks much smaller. 您似乎通过了目标图像高度(612,792),所以基本上您是在尝试从徽标图像中复制一个612x792切片,该切片看起来要小得多。

I'll try to better describe the arguments: 我将尝试更好地描述参数:

$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 :)
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM