moderntribe/tribe-storage-statically-cdn 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

moderntribe/tribe-storage-statically-cdn

Composer 安装命令:

composer require moderntribe/tribe-storage-statically-cdn

包简介

Tribe Storage Plugin to add statically.io CDN support

README 文档

README

PHPCS + Unit Tests php 7.3+

Provides dynamic image sizing and optimization via statically.io and only creates WordPress thumbnails for images that require hard cropping.

Installation Composer v1

Add the following to the composer.json repositories object:

  "repositories": [
      {
        "type": "vcs",
        "url": "git@github.com:moderntribe/tribe-storage-statically-cdn.git"
      }
  ]

Then run:

composer require moderntribe/tribe-storage-statically-cdn

Configuration

There are two ways to configure this plugin, either directly using the cdn.statically.io directly, or proxying to that domain via Nginx.

Use statically.io URLs directly

Ensure you have defined the TRIBE_STORAGE_URL constant in wp-config.php to your cloud provider's publicly accessible URL and it will be replaced to use Statically's CDN for images only:

NOTE: Image URLs are cached, ensure your flush your object cache if you make any changes to the following defines.

// Azure example
define( 'TRIBE_STORAGE_URL', 'https://account.blob.core.windows.net/container' );

URL rewriting would look as follows:

  • Original: https://account.blob.core.windows.net/container/sites/4/2021/06/image.jpg
  • Rewritten: https://cdn.statically.io/img/account.blob.core.windows.net/f=auto,w=1024,h=1024/container/sites/4/2021/06/image.jpg

Use statically.io as a proxy via Nginx

Ensure you do not have TRIBE_STORAGE_URL defined, and define the following in wp-config.php:

define( 'TRIBE_STORAGE_STATICALLY_PROXY', true );

Sample Nginx Proxy

Replace account.blob.core.windows.net below with your cloud provider's hostname. Using the example from above it would be: 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 rewriting would look as follows, and proxied to Statically behind the scenes:

  • Original: https://example.com/wp-content/uploads/sites/4/2021/06/image.jpg
  • Rewritten: https://example.com/wp-content/uploads/f=auto,w=1024,h=1024/sites/4/2021/06/image.jpg
  • Proxied URL: https://cdn.statically.io/img/account.blob.core.windows.net/f=auto,w=1024,h=1024/container/sites/4/2021/06/image.jpg

Disable WordPress thumbnail creation

If you're not concerned with exact cropping, you can let statically.io resize your image based with keeping the same dimension ratio and disable thumbnail creation to see a large performance boost when uploading new images.

For this you have two options:

Option 1: Add this define to wp-config.php

define( 'TRIBE_STORAGE_STATICALLY_CREATE_THUMBNAILS', false );

Option 2: Make the tribe/storage/plugin/statically/create_thumbnails filter return false, e.g.

add_filter( 'tribe/storage/plugin/statically/create_thumbnails', '__return_false' );

NOTE: Don't forget to clear object caching and regenerate thumbnails each time this option is swapped.

Automated Testing

Testing provided via PHPUnit and the Brain Monkey testing suite.

Run Unit Tests

$ composer install
$ ./vendor/bin/phpunit

More Resources:

moderntribe/tribe-storage-statically-cdn 适用场景与选型建议

moderntribe/tribe-storage-statically-cdn 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 304 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wordpress」 「proxy」 「cdn」 「nginx」 「moderntribe」 「tribe-storage」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 moderntribe/tribe-storage-statically-cdn 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 moderntribe/tribe-storage-statically-cdn 我们能提供哪些服务?
定制开发 / 二次开发

基于 moderntribe/tribe-storage-statically-cdn 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 304
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 14
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2021-06-24