2013-12-10 35 views
0

我试图在白色图像中放置一个徽标,并使其成为半透明以用作水印。PHP - 创建一个带有半透明徽标/水印的PNG文件

这里是我的代码...

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

...,你可以请参阅图像的右下四角因某种原因变色。

+0

我认为你需要做一些字母。 – putvande

+0

yeah..maybe ..但是logo * *正在被imagecopymerge正确透明化......它只是在$图像上标出了所有“图像之后”的东西......非常奇怪 – menriquez

回答

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 :) 
     ); 
+0

真棒......正确的感谢非常! – menriquez

+0

...还有... 15最后是透明度混合比... 0完全透明100不透明:) – menriquez