使用PHP GD库将webp转换为jpeg

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

我在使用PHP的GD库创建从webp到jpeg的清晰图像转码时遇到了一些麻烦。作为参考,这里是使用Google演示的原始jpeg版本:https://developers.google.com/speed/webp/gallery(由于我似乎无法将webp上传到stackoverflow,因此我从jpeg中包括了此文件)

enter image description here

首先我尝试了这个基本例程:

$image = imagecreatefromwebp('4.webp');
imagejpeg($image, 'converted.jpg', 100);

这有点奏效,但结果有一个黄色背景,如下所示:enter image description here

我还尝试使用这段代码来调整图像大小并首先绘制白色背景:

$image = imagecreatefromwebp('4.webp');
$width = imagesx($image);
$height = imagesy($image);
$new = imagecreatetruecolor($width, $height);
$background = imagecolorallocate($new, 255, 255, 255);
imagefilledrectangle($new, 0, 0, $width, $height, $background);
imagecopyresampled($new, $image, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($new, 'converted.jpg', 100);

这看起来更糟,并导致许多奇怪的伪像:

enter image description here

在我完全放弃GD之前,谁能提出建议以使其正常工作?首先,我正在寻找使用GD函数的解决方案。如果不可能的话,我可以使用ImageMagick或其他库-但我想对这里的问题有更多的了解,以及如果不更改工具就可以正确地完成它。

php image-processing gd image-manipulation php-gd
1个回答
2
投票

这可能是由于PHP中LibGD的较旧实现的一个众所周知的错误引起的:http://git.php.net/?p=php-src.git;a=commit;h=a31fe58d8005ff47f8f6ad095dcd4fb3a2f0aae9

https://bugs.php.net/bug.php?id=70102

据我所知,它在PHP 7.0.0中已修复:https://www.php.net/ChangeLog-7.php(错误70102)

第二个潜在问题是libgd itselt。在版本2.2.0之前,他们已经使用libvpx,在2.2.0中切换为libwebp

我已经用PHP 5.6.40和LibGD 2.1.0复制了该错误。然后将LibGD升级到2.2.4,一切正常。

我建议升级到较新的ubuntu,但是您也可以从https://packages.ubuntu.com/search?keywords=libgd手动获取所有所需的库,或者只需在/etc/apt/sources.list中增加系统的版本,然后执行apt-get updateapt-get upgrade libgd-dev libgd(或[C0 ]或libgd2取决于您系统中的软件包名称-我是Debian用户,我没有积极关注Ubuntu软件包)。它很可能会删除您的libgd3库,因此请确保也重新安装它。

© www.soinside.com 2019 - 2024. All rights reserved.