使用PHP代码生成缩略图时,如何保持PNG图像的透明度

使用PHP代码生成缩略图时,如何保持PNG图像的透明度,php,thumbnails,Php,Thumbnails,我试图保持PNG或GIF图像的透明度,但通过生成缩略图的代码,我得到的是图像的黑色背景,而不是透明的 这是我的密码: function create_image($source_image_path, $thumbnail_image_path, $real_width, $real_height,$source_image_height,$source_image_width) { list($source_image_width, $source_image_height, $sour

我试图保持PNG或GIF图像的透明度,但通过生成缩略图的代码,我得到的是图像的黑色背景,而不是透明的

这是我的密码:

function create_image($source_image_path, $thumbnail_image_path, $real_width, $real_height,$source_image_height,$source_image_width)
{
   list($source_image_width, $source_image_height, $source_image_type) =     getimagesize($source_image_path);
$source_gd_image = false;
switch ($source_image_type) {
    case IMAGETYPE_GIF:
        $source_gd_image = imagecreatefromgif($source_image_path);
        break;
    case IMAGETYPE_JPEG:
        $source_gd_image = imagecreatefromjpeg($source_image_path);
        break;
    case IMAGETYPE_PNG:
        $source_gd_image = imagecreatefrompng($source_image_path);
        break;
}
if ($source_gd_image === false) {
    return false;
}


$thumbnail_gd_image = imagecreatetruecolor($real_width, $real_height);
if(($source_image_type == 1) || ($source_image_type==3)){
    imagealphablending($thumbnail_gd_image, false);
    imagesavealpha($thumbnail_gd_image, true);
    $transparent = imagecolorallocatealpha($thumbnail_gd_image, 255, 255, 255, 127);
    imagefilledrectangle($thumbnail_gd_image, 0, 0, $real_width, $real_height, $transparent);
}

imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $real_width, $real_height, $source_image_width, $source_image_height);
imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 100);
imagedestroy($source_gd_image);
imagedestroy($thumbnail_gd_image);
return true;
}
通过上面的代码,我得到了图像的黑色背景

我尝试使用imagecolorallocate$thumbnail\u gd\u image,255,255;。但是有了这个图像填充的反射角,给我一个白色的背景


有人能帮我获取PNG或GIF图像的透明背景吗?

我想你的问题在下面一行:

$thumbnail_gd_image = imagecreatetruecolor($real_width, $real_height);

ImageCreateTureColor返回表示指定大小的黑色图像的图像标识符


您能告诉我应该怎么做来修复它吗?我将首先查看我提供的PHP.net手册链接中的注释,创建流程测试,并评估错误发生的位置。也就是说,我对PHP图像处理的经验很小,但是基于您的代码,我可以考虑用$SooSuxGDIGIMAGE替换$TunnNeLayGDI图像。老实说,请阅读手册用户评论。第一个问题是如何进行真正透明的PNGOn比较,您是否尝试过更改$Transparent=imagecoloralallocatealpha$thumbnail\u gd\u image,255,255,127;到0,0,0,127?我找到了答案。。。我检查了一个条件,即图像是JPG、PNG或GIF,如果是PNG,那么imagepng$thumbnail\u gd\u image,$thumbnail\u image\u path,9;将工作,给我透明的图像背景。。我只想知道imagepng$thumbnail\u gd\u image,$thumbnail\u image\u path,9;将适用于GIF和PNG图像??感谢您的帮助…!!: