繁体   English   中英

使用 PHP GD 将具有透明度的 PNG 文件转换为 WebP

[英]Convert PNG files with transparency to WebP using PHP GD

我有一个实用程序 class 可以加载图像文件,并在其他操作中将它们转换为其他格式。 它使用 PHP GD。

一切正常,除了具有透明度的 PNG 文件在转换为 WebP 时出错。 结果图像有一个黑色背景,透明度应该是。

这是我的代码:

class OImage {
    private ?GdImage $image      = null;
    private ?int     $image_type = null;
    
    public function load(string $filename): void {
        $image_info       = getimagesize($filename);
        $this->image_type = $image_info[2];

        switch ($this->image_type) {
            case IMAGETYPE_JPEG: { $this->image = imagecreatefromjpeg($filename); }
            break;
            case IMAGETYPE_GIF: {  $this->image = imagecreatefromgif($filename);  }
            break;
            case IMAGETYPE_PNG: {  $this->image = imagecreatefrompng($filename);  }
            break;
            case IMAGETYPE_WEBP: { $this->image = imagecreatefromwebp($filename); }
            break;
        }
    }
    
    public function save(string $filename, int $image_type=IMAGETYPE_JPEG, int $compression=75, int $permissions=null): void {
        switch ($image_type) {
            case IMAGETYPE_JPEG: { imagejpeg($this->image, $filename, $compression); }
            break;
            case IMAGETYPE_GIF: {  imagegif($this->image,  $filename); }
            break;
            case IMAGETYPE_PNG: {  imagepng($this->image,  $filename); }
            break;
            case IMAGETYPE_WEBP: {
                imagepalettetotruecolor($this->image);
                imagealphablending($this->image, true);
                imagesavealpha($this->image, true);
                imagewebp($this->image, $filename);
            }
            break;
        }
        if (!is_null($permissions)) {
            chmod($filename, $permissions);
        }
    }
    
    ...
}

class 具有许多其他调整大小或缩放的功能,但与我的问题无关。 我尝试将imagealphablendingimagesavealpha设置为 true 或 false,结果完全相同。

我也在考虑切换到 Imagick,但他们还没有 PHP 8 扩展。

我在 Debian 9 和 GD 2.2.4 上使用 PHP 8

有什么帮助吗?

谢谢!

是的,谢谢@msbit,我打电话来调整我的测试大小。 因为我认为代码没问题..:它不是调整大小 function 曾经是:

    public function resize(int $width, int $height): void {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }

现在我添加了一个 if 来检查它是 PNG 还是 WebP,所以结果如下:

    public function resize(int $width, int $height): void {
        $new_image = imagecreatetruecolor($width, $height);
        if ($this->image_type === IMAGETYPE_PNG || $this->image_type === IMAGETYPE_WEBP) {
            imagealphablending($new_image, false);
            imagesavealpha($new_image, true);
            $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
            imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
        }
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }

现在一切都完美无缺

谢谢!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM