moderntribe / tribe-storage-statically-cdn
Tribe Storage 插件,用于添加 statically.io CDN 支持
Requires
- php: >=7.4
- ext-json: *
- moderntribe/tribe-storage: ^3.0
- php-di/php-di: ^6.0
Requires (Dev)
- brain/monkey: 2.*
- moderntribe/coding-standards: ^2.0
- php-mock/php-mock-mockery: ^1.3
- phpunit/phpunit: ^9
README
通过 statically.io 提供动态图片尺寸和优化,只为需要硬裁剪的图片创建 WordPress 缩略图。
Composer v1 安装
将以下内容添加到 composer.json 的 repositories
对象中
"repositories": [ { "type": "vcs", "url": "git@github.com:moderntribe/tribe-storage-statically-cdn.git" } ]
然后运行
composer require moderntribe/tribe-storage-statically-cdn
配置
配置此插件有两种方式,要么直接使用 cdn.statically.io
,要么通过 Nginx 代理到该域名。
直接使用 statically.io URL
确保您在 wp-config.php
中定义了 TRIBE_STORAGE_URL
常量为您云提供商的公开可访问 URL,并且它将替换为仅使用 Statically 的 CDN 来使用图片
注意: 图片 URL 被缓存,如果您对以下定义进行了任何更改,请确保清空对象缓存。
// Azure example define( 'TRIBE_STORAGE_URL', 'https://account.blob.core.windows.net/container' );
URL 重写将如下所示
- 原始:
https://account.blob.core.windows.net/container/sites/4/2021/06/image.jpg
- 重写:
https://cdn.statically.io/img/account.blob.core.windows.net/f=auto,w=1024,h=1024/container/sites/4/2021/06/image.jpg
通过 Nginx 代理使用 statically.io
确保您没有定义 TRIBE_STORAGE_URL
,并在 wp-config.php
中定义以下内容
define( 'TRIBE_STORAGE_STATICALLY_PROXY', true );
示例 Nginx 代理
将下面的
account.blob.core.windows.net
替换为您的云提供商的主机名。使用上面的示例,它将是:account.blob.core.windows.net/container
# Root site and sub sites location ~* ^/(.+)?wp-content/uploads { try_files $uri $uri/ @statically; } # Check statically first location @statically { # adjust the /container below to your actual container name rewrite "^/(.+)?wp-content/uploads/(.*=.*?[\/])?(.+)$" /$2container/$3 break; proxy_http_version 1.1; resolver 1.1.1.1; proxy_set_header Connection ''; proxy_set_header Authorization ''; proxy_set_header Host cdn.statically.io; proxy_hide_header Set-Cookie; proxy_ignore_headers Set-Cookie; proxy_intercept_errors on; recursive_error_pages on; error_page 400 404 500 = @uploads; add_header X-Image-Path "$uri" always; proxy_pass https://cdn.statically.io/img/account.blob.core.windows.net$uri; } # Fallback to check Azure directly location @uploads { # remove any statically.io params, e.g f=auto,w=518,h=291/ rewrite ^/(.*=.*?[\/])?(.+)$ /$2 break; proxy_http_version 1.1; resolver 1.1.1.1; proxy_set_header Connection ''; proxy_set_header Authorization ''; proxy_set_header Host account.blob.core.windows.net; proxy_hide_header x-ms-blob-type; proxy_hide_header x-ms-lease-status; proxy_hide_header x-ms-request-id; proxy_hide_header x-ms-version; proxy_hide_header Set-Cookie; proxy_ignore_headers Set-Cookie; proxy_intercept_errors on; recursive_error_pages on; error_page 400 404 500 = @imageerror; add_header X-Image-Path "$uri" always; add_header Cache-Control max-age=31536000; proxy_pass https://account.blob.core.windows.net$uri; } # If both the above fail, show the default Nginx 404 error page location @imageerror { add_header X-Error-Uri "$uri" always; return 404; }
URL 重写将如下所示,并在幕后代理到 Statically
- 原始:
https://example.com/wp-content/uploads/sites/4/2021/06/image.jpg
- 重写:
https://example.com/wp-content/uploads/f=auto,w=1024,h=1024/sites/4/2021/06/image.jpg
- 代理 URL:
https://cdn.statically.io/img/account.blob.core.windows.net/f=auto,w=1024,h=1024/container/sites/4/2021/06/image.jpg
禁用 WordPress 缩略图创建
如果您不关心精确裁剪,可以让 statically.io 根据相同的长宽比调整图片尺寸并禁用缩略图创建,以在上传新图片时获得大幅性能提升。
为此,您有两个选项
选项 1:将此定义添加到 wp-config.php
define( 'TRIBE_STORAGE_STATICALLY_CREATE_THUMBNAILS', false );
选项 2:让 tribe/storage/plugin/statically/create_thumbnails
过滤器返回 false,例如
add_filter( 'tribe/storage/plugin/statically/create_thumbnails', '__return_false' );
注意: 不要忘记每次切换此选项时清空对象缓存并重新生成缩略图。
自动化测试
通过 PHPUnit 和 Brain Monkey 测试套件提供测试。
运行单元测试
$ composer install $ ./vendor/bin/phpunit