PHP图像透明

PHP图像透明,php,image,resize,thumbnails,transparent,Php,Image,Resize,Thumbnails,Transparent,我正在尝试使用以下代码调整PNG透明图像的大小: $src=ImageCreateFrompng($uploadedfile); $background=imagecolorallocate($src,0,0,0); imagecolortransparent($src,$background); imagealphablending($src,false); imagesavealpha($src,true); $dst=ImageCreateTrueColor($tn_width,$tn_h

我正在尝试使用以下代码调整PNG透明图像的大小:

$src=ImageCreateFrompng($uploadedfile);
$background=imagecolorallocate($src,0,0,0);
imagecolortransparent($src,$background);
imagealphablending($src,false);
imagesavealpha($src,true);
$dst=ImageCreateTrueColor($tn_width,$tn_height);
imagecopyresampled($dst,$src,0,0,0,0,$tn_width,$tn_height,$width,$height);
Imagepng($dst,$dstfile);
我使用了
imagealphabling($src,false)
imagesavealpha($src,true)
,但它仍然上传一个黑色背景的图像,而不是透明背景的图像


问题出在哪里?

这是因为您将透明图像复制到了黑色图像上。您的错误设置为APLLABLIND only applyes to ORIGINE image,因此当您复制新图像时,aplha混合将启用。
您的代码只需稍作改进:

$transparent = imagecolorallocatealpha($dst, 0,0,0,127);  //Transparent background color
imagealphablending($dst,false);            //Not to compound transparent colors with opaque
imagesavealpha($dst,true);                 //Allow transparency
imagefilledrectangle($dst, 0, 0, imagesx($dst), imagesy($dst), $transparent);   //Give the destination image transparent background
//Now you can copy
或者只是 imagecolorallocate($src,0,0,0)


该行看起来像是从源代码中获取图像,然后使用000背景。那是黑色的。只是一个猜测,不熟悉这个函数

我应该怎么做才能修复它?我已经稍微改进了我的帖子。抱歉第一次发布的时间太短:)只是一个小错误:imagecolorallocatealpha而不是imagecolorallocatealpha。。。它起作用了!!哦,很抱歉有打字错误。请随意对包含错误的帖子进行建议编辑。我不确定您要点击哪些代码,但如果您是在回复我的“不建议编辑”,请确保阅读页面
imagealphablending($dst,false);   //I'm not 100% sure this will make it, but its worth a try