8ctopus / webp8
将图像转换为webp格式的命令行工具
1.0.3
2023-07-24 11:21 UTC
Requires (Dev)
- symfony/console: 6.1.3
- symfony/stopwatch: ^6
README
webp8是一个将图像转换为webp格式的命令行工具
如何安装
您可以选择以下方式之一
- composer install
composer require 8ctopus/webp8
- 下载phar包
- 或自行构建
# download webp8 curl -LO https://github.com/8ctopus/webp8/releases/download/1.0.3/webp8.phar # check hash against the one published under releases sha256sum webp8.phar # make phar executable chmod +x webp8.phar # rename phar (from here on optional) mv webp8.phar webp8 # move phar to /usr/local/bin/ (optional) mv webp8 /usr/local/bin/
仅限Windows
-
从libwebp下载并提取cwebp.exe
-
将cwebp.exe添加到PATH环境变量(sysdm.cpl -> 高级 -> 环境变量)
-
下载webp8,curl -LO https://github.com/8ctopus/webp8/releases/download/1.0.3/webp8.phar
-
与发布页面下发布的哈希值进行核对
将图像转换为webp
./webp8 convert [--cwebp_m] [-cwebp_q] [--cwebp_z] [--multithreading] directory
[▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 15843/15843 (100%) - 1 hr/1 hr - 6.0 MiB
[OK]
------- ----------- --------- ------------- ------- --------------- -----------
total converted skipped webp bigger time size original size webp
------- ----------- --------- ------------- ------- --------------- -----------
15843 15843 0 235 64:20 1.2 GB 150.3 MB
------- ----------- --------- ------------- ------- --------------- -----------
删除现有的webp图像
./webp8 cleanup [--dry-run] [-v] directory
htaccess代码,当浏览器支持时显示webp而不是png/jpg
代码改编自webp-express
RewriteEngine On
# redirect images to webp when possible
# check if browser accepts webp
RewriteCond %{HTTP_ACCEPT} image/webp
# check if requested file is jpg or png
RewriteCond %{REQUEST_FILENAME} \.(jpe?g|png)$
# check if webp for image exists
RewriteCond %{REQUEST_FILENAME}\.webp -f
# serve webp image instead
RewriteRule . %{REQUEST_FILENAME}\.webp [T=image/webp,E=EXISTING:1,E=ADDVARY:1,L]
# make sure that browsers which do not support webp also get the Vary:Accept header
# when requesting images that would be redirected to existing webp on browsers that does.
SetEnvIf Request_URI "\.(jpe?g|png)$" ADDVARY
# Apache appends "REDIRECT_" in front of the environment variables defined in mod_rewrite, but LiteSpeed does not.
# So, the next lines are for Apache, in order to set environment variables without "REDIRECT_"
SetEnvIf REDIRECT_EXISTING 1 EXISTING=1
SetEnvIf REDIRECT_ADDVARY 1 ADDVARY=1
# Set Vary:Accept header for the image types handled by WebP Express.
# The purpose is to make proxies and CDNs aware that the response varies with the Accept header.
Header append "Vary" "Accept" env=ADDVARY
开发
构建phar
./build.sh
调试代码
php src/EntryPoint.php convert -- images
# cleanup test dir
git clean -xfd images
改进想法
- 检查转换后的图像大小是否不为0
- 添加关于如何在Windows上安装的提示