2014-03-19 61 views
1

我使用下面的代碼,但對於transaparent圖像編寫的代碼在PHP中的水印製作圖像,黑色背景來了:黑色背景快到了透明的圖像,同時在PHP

$font_path    = $_SERVER['DOCUMENT_ROOT'] . "/fonts/arial.ttf"; // Font file 
$water_mark_text_2  = "IndustrialStores.com"; // Watermark Text 
list($owidth,$oheight) = getimagesize($oldimage_name); 

$width = $owidth; 
$height = $oheight; 
$image = imagecreatetruecolor($width, $height); 

$extension = pathinfo($oldimage_name, PATHINFO_EXTENSION); 
$extension = strtolower($extension); 

if($extension=="jpg" || $extension=="jpeg"){ 
    $image_src = imagecreatefromjpeg($oldimage_name); 
} 
else if($extension=="png"){ 
    $image_src = imagecreatefrompng($oldimage_name); 
} 
else if($extension=="gif"){ 
    $image_src = imagecreatefromgif($oldimage_name); 
} 
else if($extension=="bmp"){ 
    $image_src = imagecreatefrombmp($oldimage_name); 
} 
else{ 
    copy($oldimage_name, $new_image_name);  
    unlink($oldimage_name); 
    return true; 
} 

imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight); 
$blue = imagecolorallocate ($image, 179, 179, 179); 
$bbox = imageftbbox($width/15, 0, $font_path, 'IndustrialStores.com'); 

$x = $bbox[0] + (imagesx($image)/2) - ($bbox[4]/2); 
$y = $bbox[1] + (imagesy($image)/2) - ($bbox[5]/2) - 5; 

imagettftext($image, $width/15, 0, $x, $y, $blue, $font_path, $water_mark_text_2); 
imagejpeg($image, $new_image_name, 100); 
imagedestroy($image); 
unlink($oldimage_name); 

我已經嘗試過許多其他答案StackOverflow的來回就像使用:

$im = imagecreatetruecolor(55, 30); 
$red = imagecolorallocate($im, 255, 0, 0); 
$black = imagecolorallocate($im, 0, 0, 0); 

// Make the background transparent 
imagecolortransparent($im, $black); 

但沒有使用這一切

+0

我已經嘗試過,答案,但它不工作 – user3438880

回答

0

將這些行添加幫助嗎?

else{ 
    copy($oldimage_name, $new_image_name);  
    unlink($oldimage_name); 
    return true; 
} 

// add these lines here! 
imagealphablending($image, false); 
imagesavealpha($image, true); 

imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight); 
$blue = imagecolorallocate ($image, 179, 179, 179); 
+0

已經嘗試過這一點,但不是使用 的或者你可以讓我知道在我的代碼,我需要把這個線 – user3438880

+0

編輯的代碼上下文。 – rockerest

+0

我在這裏嘗試了這些行,但這會改變水印文本顏色,但對背景仍然沒有影響黑色 檢查此http://screencast.com/t/2XcvX0Gvjfbq – user3438880