2012-01-25 121 views
0

我想爲使用PHP和GD圖像庫的圖像添加水印。我可以在指定正確的不透明度設置的情況下應用水印。PHP GD - 不透明水印圖像

問題是我的水印本身有一個透明背景。當我嘗試將這種水印應用於圖像時,我會得到黑色背景。

水印被應用到的圖像是一個jpeg。這可能是問題嗎?如果是這樣,我將如何將jpeg轉換爲支持透明度的格式,應用水印,然後將其轉換回來?

這是我現在的代碼的關鍵位。

// Determine image size and type 
$size = getimagesize($this->image_path); 
$size_x = $size[0]; 
$size_y = $size[1]; 
$image_type = $size[2]; // This is always a JPEG 

// load source image 
$image = $this->ImageCreateFromType($image_type, $this->image_path); 

// Determine watermark size and type 
$wsize = getimagesize($watermark_path); 
$watermark_x = $wsize[0]; 
$watermark_y = $wsize[1]; 
$watermark_type = $wsize[2]; // This is typically a PNG 

// load watermark 
$watermark = $this->ImageCreateFromType($watermark_type, $watermark_path); 

$dest_x = $this->setX($size_x, $watermark_x); 
$dest_y = $this->setY($size_y, $watermark_y); 

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_x, $watermark_y, $this->opacity); 

雖然不是真正相關的,這裏是爲ImageCreateFromType功能

function ImageCreateFromType($type,$filename) { 
$im = null; 
switch ($type) { 
    case 1: 
    $im = ImageCreateFromGif($filename); 
    break; 
    case 2: 
    $im = ImageCreateFromJpeg($filename); 
    break; 
    case 3: 
    $im = ImageCreateFromPNG($filename); 
    imagealphablending($im, true); 
    imagesavealpha($im, true); 
    break; 
    } 
    return $im; 
} 

回答