我试图保留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_gd_image, 255, 255, 255);
。但是这个imagefilledrectangle()
给了我白色背景。
有人可以帮助我获得PNG或GIF图像的透明背景吗?
答案 0 :(得分:0)
我认为你的问题在下面一行:
$thumbnail_gd_image = imagecreatetruecolor($real_width, $real_height);
根据http://www.php.net/manual/en/function.imagecreatetruecolor.php
imagecreatetruecolor()返回表示指定大小的黑色图像的图像标识符。