2013-12-10 33 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