PHP重新编译GD扩展,以支持WEBP格式
问题描述
Laravel 用 Intervention Image 读取 webp 格式的图片时报错:
Intervention/Image/Exception/NotReadableException with message 'Unsupported image type. GD/PHP installation does not support WebP format.'

看这报错的意思,是GD库缺少对webp格式的支持(webp格式是google出品的一种高效图片压缩格式,跟jpeg类似是有损压缩,但体积会比jpeg平均减少40%以上)
用以下命令看下php-gd的配置详情,里边缺少“WebP Support”字样:
php --ri gd

解决方案
Intervention Image 配置里,默认图像库用的是GD,PHP 是在 CentOS7 上用 yum 装的,不想从源码重新编译完整的PHP,所以,只编译PHP源码中的ext/gd,当然,先去官网下载对应版本的 PHP 源码,然后:
cd php-7.x.x/ext/gd
phpize
./configure --with-webp-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-xpm-dir
make
如果有报错,应该是相应的图片格式库没装,安装相应的devel库:
yum install libwebp-devel libjpeg-turbo-devel libpng-devel libXpm-devel freetype-devel
编译完成后,将so文件放到 PHP 的 modules 目录下,修改 php-gd 的配置,注释掉旧的 gd.so,添加新的 my_gd.so:
cp modules/gd.so /usr/lib64/php/modules/my_gd.so
vim /etc/php.d/gd.ini
;extension=gd.so
extension=my_gd.so
然后,检查下 php-gd 的配置里有“WebP Support => enabled”字样即可(php-fpm需要重启生效)

