2014-03-19 63 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