不同的PHP版本有不同编译参数和不同的依赖,这里是 CentOS 7 编译安装的PHP 5.6,通过GD库增加libvpx实现对webp格式图片支持,然后修改fileinfo扩展增加webp的mime文件类型检测
查看GD模块参数
运行php --ri gd
GD Support => enabled
GD Version => bundled (2.1.0 compatible)
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.7.0
GIF Read Support => enabled
GIF Create Support => enabled
JPEG Support => enabled
libJPEG Version => 6b
PNG Support => enabled
libPNG Version => 1.5.13
WBMP Support => enabled
XBM Support => enabled
可以看到并不支持webp格式图片
查看PHP编译参数
通过/usr/local/php/bin/php-config 可以看到PHP编译参数
--prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir \
--with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem \
--enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd \
--enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip \
--enable-soap --with-gettext --enable-fileinfo --enable-opcache --enable-intl --with-xsl
fileinfo扩展也是开启的
安装依赖libvpx、libwebp库
vpx是vp8、vp9视频格式的编解码器,webp就是vp8编解码的图片 安装libvpx库、libwebp库 yum -y install libvpx-devel libwebp-devel
fileinfo模块调整
需要对magic数据库做一个小小的修改,其实PHP 5.6已经支持webp了,只是返回的MIME类型不正确
file 37249391640154040.webp
Web/P image data, 19104 bytes\012- RIFF (little-endian) data
magic数据文件重新编译测试
PHP 5.6到7.1使用的是file-5.17版本的libmagic 库,数据文件是经过转换的,PHP解码然后与源码对比
以便保持以前的配置不变,仅增加webp格式识别
到php-5.6.40/ext/fileinfo 目录解码数据库文件:
# decode_magic_data.php
<?php
$dta = file_get_contents( $argv[1] );
preg_match('/php_magic_database\[(\d*)\]/', $dta, $matches);
$dta_l = $matches[1];
$offset = strpos($dta, '0x');
$j = 0;
for ($i = 0; $i < $dta_l; $i++) {
$hex = $dta[$offset+2] . $dta[$offset+3];
echo chr(hexdec($hex));
$offset += 6;
if ($j % 16 == 15) {
$offset += 1;
}
$j++;
}
?>
执行php decode_magic_data.php data_file.c > decode_magic.mgc 得到原版decode_magic.mgc
file-5.17源码编译
wget ftp://ftp.astron.com/pub/file/file-5.17.tar.gz
tar xf file-5.17.tar.gz
cd file-5.17
wget https://raw.githubusercontent.com/php/php-src/PHP-5.6.40/ext/fileinfo/magicdata.patch
[root@localhost file-5.17]# patch -p1 < magicdata.patch
patching file magic/Magdir/commands
patching file magic/Magdir/commands
patching file magic/Magdir/msooxml
patching file magic/Magdir/filesystems
Reversed (or previously applied) patch detected! Assume -R? [n]
Apply anyway? [n] y
Hunk #1 FAILED at 1.
1 out of 3 hunks FAILED -- saving rejects to file magic/Magdir/filesystems.rej
编译file
./configure --prefix=/usr/local/file
make && make install
可以看到PHP源码解码出中的decode_magic.mgc 与file源码中的magic/magic.mgc 大小一样,内容完全一样
增加webp的MIME
修改file源码中magic/Magdir/images 文件的893行后加上mime类型即可,就是这么简单
>8 string WEBP Web/P image data
!:mime image/webp
make && make install
/usr/local/file/bin/file -i 37249391640154040.webp
37249391640154040.webp: image/webp; charset=binary
可以看到已经成功识别webp。
然后将magic/magic.mgc复制到PHP源码php-5.6.40/ext/fileinfo/ 中,并执行:
php create_data_file.php magic.mgc > data_file.c
重新编译PHP
重新编译php,configure 增加--with-vpx-dir ,make ZEND_EXTRA_LIBS='-liconv' ,make install 再次查看GD模块,WebP支持已经有了
GD Support => enabled
GD Version => bundled (2.1.0 compatible)
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.7.0
GIF Read Support => enabled
GIF Create Support => enabled
JPEG Support => enabled
libJPEG Version => 6b
PNG Support => enabled
libPNG Version => 1.5.13
WBMP Support => enabled
XBM Support => enabled
WebP Support => enabled
测试
var_dump(finfo_file(finfo_open(FILEINFO_MIME_TYPE), '37249391640154040.webp'));
# string(10) "image/webp"
fileinfo模块已经识别了webp图片。
Intervention/Image测试
composer require intervention/image
require_once 'vendor/autoload.php';
use Intervention\Image\ImageManagerStatic as Image;
$image = Image::make('37249391640154040.webp');
var_dump($image->width());
# int(551)
总结
首先让gd库支持webp,然后让fileinfo支持webp格式。
|