IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> PHP 开启 webp 支持 -> 正文阅读

[PHP知识库]PHP 开启 webp 支持

不同的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-dirmake 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格式。

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章           查看所有文章
加:2021-08-09 10:01:53  更:2021-08-09 10:03:27 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/13 20:43:32-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码