首页 > 解决方案 > 如何在 php 7.4.10 版本上启用 Webp 支持为 true?

问题描述

我在 Red Hat Linux 86_64 服务器上使用 php 7.4.10 版本,我想在用户上传图像后将所有图像自动转换为 webp 扩展。我有 php 代码可以正常上传图片,但是当我尝试上传图片时,出现以下错误:

Fatal error: Uncaught Error: Call to undefined function imagewebp() in....

当我收到上面的错误时,我在页面上输入了var_dump (gd_info()),之后我得到了以下信息:

array(14) { 

["GD Version"]=> string(26) "bundled (2.1.0 compatible)" 
["FreeType Support"]=> bool(true) 
["FreeType Linkage"]=> string(13) "with freetype" 
["GIF Read Support"]=> bool(true) 
["GIF Create Support"]=> bool(true) 
["JPEG Support"]=> bool(true) 
["PNG Support"]=> bool(true) 
["WBMP Support"]=> bool(true) 
["XPM Support"]=> bool(true) 
["XBM Support"]=> bool(true) 
["WebP Support"]=> bool(false) 
["BMP Support"]=> bool(true) 
["TGA Read Support"]=> bool(true) 
["JIS-mapped Japanese Font Support"]=> bool(false) 

}

根据上面的数组结果,Webp 支持被检查为 false:

["WebP Support"]=> bool(false)

在这种情况下,我如何将 WebP 支持启用为["WebP Support"]=> bool(true)?我发现了一些与我的疑问相关的类似问题:

  1. 更新 GD 版本以支持 PHP 7.3 的 Webp
  2. 如何编译 php 以启用 webp 支持?
  3. 在 LiteSpeed 上运行的 Ubuntu 18.04 上的 GD 库中启用 WebP 支持

但不幸的是,我没有找到在我的服务器上启用 webp 支持的正确方法。

标签: phpwebp

解决方案


在全新的 php 安装中,添加 gd withsudo apt install php7.4-gd会自动启用 webp。在 phpinfo() 中输出;是:

gd
GD Support  enabled
GD headers Version  2.3.0
GD library Version  2.3.0
FreeType Support    enabled
FreeType Linkage    with freetype
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support    enabled
PNG Support     enabled
WBMP Support    enabled
XPM Support     enabled
XBM Support     enabled
WebP Support    enabled
BMP Support     enabled
TGA Read Support    enabled 

推荐阅读