首页 > 解决方案 > 上传PNG图像是黑色背景

问题描述

我正在从 excel 文件中的链接中获取 png 图像,并使用文件夹路径将其上传到数据库中。问题是它以黑色背景出现,尽管原始图像是透明的。

这是代码

$imageSource  = file_get_contents($obj->ProductImageLink);

$uploadedFile = file_put_contents($imageAbsolutePath, $imageSource);

if (filesize($imageAbsolutePath) != 0) {
try{
    $image = new ImageResize($imageAbsolutePath);
    $image->resizeToHeight(600);
    $image->save($imageAbsolutePath, $obj->ImageType);
    $image->resizeToHeight(250);
    $image->save($imageAbsolutePathThumb, $obj->ImageType);
    unset($image);
}

我正在使用图像调整器,对于 PNG 我有以下

case IMAGETYPE_PNG:
    if (!$this->quality_truecolor && !imageistruecolor($this->source_image)) {
        $dest_image = imagecreate($this->getDestWidth(), $this->getDestHeight());

        $background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1);
        imagecolortransparent($dest_image, $background);
        imagefill($dest_image, 0, 0, $background);
    } else {
        $dest_image = imagecreatetruecolor($this->getDestWidth(), $this->getDestHeight());
    }

    imagealphablending($dest_image, false);
    imagesavealpha($dest_image, true);
    break;
}

我试图改变$background = imagecolorallocatealpha($dest_image, 255, 255, 255, 1); to imagecolorallocatealpha($dest_image, 255, 255, 255);或不同的组合,但没有任何效果。

标签: phppngimage-uploading

解决方案


推荐阅读