Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中创建图像时,透明图像的黑色背景即将出现_Php_Image Processing - Fatal编程技术网

在php中创建图像时,透明图像的黑色背景即将出现

在php中创建图像时,透明图像的黑色背景即将出现,php,image-processing,Php,Image Processing,我正在使用以下代码在php中编写水印代码,但对于父图像,黑色背景即将出现: $font_path = $_SERVER['DOCUMENT_ROOT'] . "/fonts/arial.ttf"; // Font file $water_mark_text_2 = "IndustrialStores.com"; // Watermark Text list($owidth,$oheight) = getimagesize($oldimage_na

我正在使用以下代码在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);

但是所有这些都没有用

添加这些行会有帮助吗

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);

已经尝试过但没有使用,或者您可以让我知道在我的代码中我需要将此行放置在代码上下文中。我在此处尝试了这些行,但这将更改水印文本颜色,但对背景没有影响仍为黑色检查此问题我已经尝试过该答案,但它不起作用