quinninteractive/silverstripe-webp-substitution

自动将图片转换为WebP格式

安装: 1,584

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:silverstripe-vendormodule

2.1.1 2024-09-09 17:15 UTC

This package is auto-updated.

Last update: 2024-09-09 17:15:28 UTC


README

此模块提供了一个任务 ConvertImagesToWebpTask,该任务需要定期运行,为所有现有的公共图片提供WebP替代品。

要求

  • silverstripe/framework ^4.4
  • silverstripe/s3 冲突,因为此模块仅与默认的基于文件系统的资产管理器一起工作。

安装

composer require quinninteractive/silverstripe-webp-substitution ^2.0.0

许可证

许可证

配置示例

如果您需要更改用于WebP图片的相对目录或文件后缀,可以在配置中进行更改。

默认情况下,转换限制为32兆像素,超过此限制不进行转换。这是为了避免过度使用服务器的虚拟内存。此设置可以被以下方式覆盖。如果您更改了 webp_file_suffix,您还必须在您的nginx配置中更改后缀(见下面的示例)。

QuinnInteractive\WebPSub\Task\ConvertImagesToWebpTask:
  webp_directory_path: '.misc/.WebP'
  webp_file_suffix: '.wp'
  size_limit_megapixels: 16

命令行示例

sudo -Eu www ./vendor/bin/sake dev/tasks/webpconvert

crontab示例

DOCROOT=/var/silverstripe/live/current/docroot
SAKE=./vendor/bin/sake
SUDO=/usr/local/bin/sudo

# WebP graphics conversion task every hour
50 * * * *    (cd $DOCROOT && $SUDO -Eu www $SAKE dev/tasks/webpconvert) > /dev/null 2>&1

nginx配置示例

为了支持此模块,请将这些项添加到现有的Silverstripe配置中。如果您已更改YAML配置,则需要相应地调整这些项。

http部分

# webp dir to try, if we accept webp (or none if not)
map $http_accept $webp_dir {
    default   "";
    "~*image/webp"  "/assets/.webp/";
}

map $arg_nowebp $accept_webp {
    default   $webp_dir;
    '1'    "/fail-on-purpose/";
}

server部分中,在主要资产location指令之前

# first try to return allowed webp
location ~ ^/assets/.*\.(?i:gif|jpeg|jpg|png)$ {
    try_files $accept_webp$uri.webp $uri /index.php?$query_string;
}

# Never serve .protected, nor .webp not served above
location ~ ^/assets/\.(webp|protected)/ {
    return 403;
}

Apache配置示例

待定

文件系统准备

如果可能,任务将创建所需的目录。如果不行,您必须在assets目录下创建.webp目录(或webp_directory_path配置中命名的目录),并使其可由Web服务器写入。

版本

2.1.1

发行说明

2.1.1

Silverstripe 5支持。

2.1.0

此版本引入了一个向后兼容的特性,以便转换的WebP文件不在CMS中使用。为了利用此新特性,您需要更改您的nginx配置(见上面的示例)。如果您不进行更改,您的站点将像早期版本一样工作,所以这是一个非破坏性更改。