【问题标题】:Convert PNG files with transparency to WebP using PHP GD使用 PHP GD 将具有透明度的 PNG 文件转换为 WebP
【发布时间】:2021-05-19 19:22:32
【问题描述】:

我有一个实用程序类,它可以加载图像文件,并在其他操作中将它们转换为其他格式。它使用 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);
        }
    }
    
    ...
}

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

我也在考虑改用 Imagick,但他们还没有 PHP 8 扩展。

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

有什么帮助吗?

谢谢!

【问题讨论】:

  • 以下单行将带有 alpha 的 PNG 转换为带有 alpha 的 WEBP,没有任何问题:imagewebp(imagecreatefrompng('https://maxcdn.icons8.com/office/PNG/512/Science/alpha-512.png'), 'out.webp'); 基于此,我的猜测是您的一些调整大小或缩放操作正在删除 alpha在你打电话给save之前的频道。

标签: php gd


【解决方案1】:

是的!谢谢@msbit,我打电话来调整我的测试大小,因为我认为代码没问题......但它不是? 以前的调整大小函数是:

    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;
    }

现在一切都完美无缺?

谢谢!!

【讨论】:

    猜你喜欢
    • 2021-10-03
    • 2023-03-15
    • 2013-09-14
    • 2018-06-05
    • 2015-01-26
    • 2018-10-19
    • 2016-07-27
    • 2020-04-24
    • 2014-06-30
    相关资源
    最近更新 更多