PNG图像显示上传/调整大小的黑色背景

问题描述 投票:1回答:2

我正在上传图片及其大小调整,但在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;
php image-resizing
2个回答
1
投票

回答:

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

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

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


0
投票

我遇到了同样的问题,我使用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);
© www.soinside.com 2019 - 2024. All rights reserved.