generoi/wp-image-resizer 问题修复 & 功能扩展

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

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

generoi/wp-image-resizer

Composer 安装命令:

composer require generoi/wp-image-resizer

包简介

A plugin which provides dynamic image sizes through a CDN

关键字:

README 文档

README

A plugin which provides dynamic image sizes through a CDN

Requirements

A resizer service, this plugin currently supports:

  • A cloudflare CDN zone with image resizing enabled.
  • KeyCDN with pull zone configured
  • Fastly with image optimzer enabled

Note that you'll need to manage the loading attribute yourself. Only tags with loading="lazy" will get rewritten to use lozad.js. You should ensure that the LCP blocks have loading="eager" or no loading attribute at all.

IMAGERESIZER_ZONE environment variable is required.

Environment variables

IMAGERESIZER_ZONE='https://myapp.com/cdn-cgi/image/'
IMAGERESIZER_DISABLED=false

Features

  • Use lozad.js to lazyload images, iframes and videos
  • Support for sizes="auto" through data-sizes="auto", which is resolved against the element's rendered box. For images using object-fit: cover or object-fit: contain the painted image can be larger than its box, so the intrinsic aspect ratio is taken into account to avoid requesting a too-small, blurry candidate.
  • Preload first blocks image
  • Rewrite image URLs to use Cloudflare CDN
  • Replace all WP srcset with our Cloudflare Image Resizing URLs

API

See Cloudflare docs, KeyCDN docs or Fastly docs for transformation options.

Pass custom options

You can pass custom options to the resizer when using wp_get_attachment_image by setting data-resizer-args which supportes wp_parse_args type strings.

wp_get_attachment_image($profileImage, 'full', false, [
  'data-resizer-args' => 'crop=1:1&quality=50',
  'sizes' => '(min-width: 40em) 48px, (min-width: 68.75em) 56px, (min-width: 87.5em) 64px, 40px'
])

Generate src and srcset from URL

use GeneroWP\ImageResizer\Image;

$image = new Image($url, [
    'blur' => 50,
]);

return sprintf(
    '<img loading="eager" class="wp-block-cover__image-background" src="%s" srcset="%s" sizes="%s">',
    $image->src(),
    $image->srcset(),
    '100vw'
);

Generate src and srcset from attachment

use GeneroWP\ImageResizer\Image;

$image = Image::fromAttachment($bannerImageId, 'full', [
    'fit' => 'cover',
    'height' => 350 * 2,
]);

return sprintf(
    '<img loading="eager" class="wp-block-cover__image-background" src="%s" srcset="%s" sizes="%s">',
    $image->src(),
    $image->srcset(),
    '100vw'
);

Add a preload link tag to head

use GeneroWP\ImageResizer\Rewriters\Preload;

add_action('wp_head', function () {
    if (is_singular('recipe')) {
        $composer = new ContentRecipe();
        $featured = $composer->featuredImageHtml(get_post());
        echo Preload::buildLink($featured);
    }
}, 2);

Filters

// Alter rewriters
add_filter('wp-image-resizer/rewriters', function (array $rewriters) {
    $rewriters[] = MyCustomRewriter::class;
    return $rewriters;
});

// Alter resizer URLs
add_filter('wp-image-resizer/image/url', function (string $url) {
    if (defined('WP_ENV') && WP_ENV === 'development') {
        $url = str_replace('development-domain.ddev.site', 'production-domain.com', $url);
    }
    return $url;
});

// Alter blocks that should be considered for preloading
add_filter('wp-image-resizer/preload/blocks', function (array $blockTypes) {
    $blockTypes[] = 'my-theme/hero-banner';
    return $blockTypes;
});

// Alter if preload should stop iterating blocks
add_filter('wp-image-resizer/preload/should_stop', function (bool $stop, array $block, array $blocks) {
    if ($block['blockType'] === 'my-theme/breadcrumb') {
        return false;
    }
    return $stop;
}, 10, 3);

// Alter srcset breakpoints
add_filter('wp-image-resizer/config/breakpoints', function (array $breakpoints) {
    return [
        ...range(50, 500, 50),
        ...range(600, 2000, 100),
    ];
});

// Alter default resizing settings
add_filter('wp-image-resizer/config/default', function (array $breakpoints) {
    return [
        'quality' => 90
    ];
});

Development

Install dependencies

composer install
npm install

Run the tests

npm run test

Build assets

# Minified assets which are to be committed to git
npm run build:production

# Watch for changes and re-compile while developing the plugin
npm run start

generoi/wp-image-resizer 适用场景与选型建议

generoi/wp-image-resizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.63k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 generoi/wp-image-resizer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 21.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-29