uploading/resizing 上显示黑色背景的 PNG 图像

PNG image showing black background on uploading/resizing

我正在上传一张图片并调整其大小,但在 PNG 上,它显示黑色背景。

能否请您检查代码并告诉我问题出在哪里?

$newImageWidth = ceil($width * $scale);
        $newImageHeight = ceil($height * $scale);
        $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
        if ($ext == "jpg" || $ext == "jpeg")
        {
            $source = imagecreatefromjpeg($image);
        }
        else
        if ($ext == "png")
        {
            $source = imagecreatefrompng($image);
        }
        else
        {
            $source = imagecreatefromgif($image);
        }

        imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
        imagejpeg($newImage,$image,90);
        chmod($image, 0777);
        return $image;

答案:

在 imagecopyresampled() 函数之前添加了这段代码

$tmp = imagecreatetruecolor($new_width,$new_height);
imagefilledrectangle($tmp, 0, 0, $new_width, $new_height, imagecolorallocate($tmp, 255, 255, 255));

它开始按照我的意愿工作....

我遇到了同样的问题,我使用来自 http://php.net/manual/en/function.imagecolorallocatealpha.php

的 alpha 着色添加了以下代码
//setting transparent color
  $color = imagecolorallocatealpha($this->imageResized, 0, 0, 0, 127);
//seting the image fill to the transparent color
  imagefill($this->imageResized, 0, 0, $color);
//saving the image with transparency before resizing
  imagesavealpha($this->imageResized, TRUE);