moritzebeling/kirby-headless-image-transformations 问题修复 & 功能扩展

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

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

moritzebeling/kirby-headless-image-transformations

Composer 安装命令:

composer require moritzebeling/kirby-headless-image-transformations

包简介

Kirby plugin for headless image transformations

README 文档

README

This plugin adds a new route to your Kirby CMS installation that allows you to transform images on the fly. This is useful when you want to let the frontend decide what image size to load:

Schema

https://your-kirby-website.com/thumbs/{image-id}.{ext}?{transformations}

Original image

https://your-kirby-website.com/thumbs/{image-id}.{ext}

Thumbnail with 640px width and auto height

https://your-kirby-website.com/thumbs/{image-id}.{ext}?width=640

Cropped thumbnail with 640px width and height

https://your-kirby-website.com/thumbs/{image-id}.{ext}?width=640&height=640&crop=true

Installation

composer require moritzebeling/kirby-headless-image-transformations

Or download/clone this repo into site/plugins of your Kirby project.

URL Query parameters

You can use all options offered by the Kirby thumb() method:

let options = {
    'autoOrient' => true,  // bool
    'crop'       => false, // bool
    'blur'       => false, // bool
    'grayscale'  => false, // bool
    'height'     => null,  // int
    'quality'    => 90,    // int 0-100
    'width'      => null,  // int
}

Usage

This plugin can be helpful if you use Kirby as a Headless CMS and want to decide on your frontend which image size to load or to include in your srcset. You would only need the image id:

$image_id = $file->id();

Load single size:

function serializeObjectToQueryString( options = {} ){
    const queryString = Object.keys(options).map(key => key + '=' + options[key]).join('&');
    return queryString ? '?' + queryString : '';
}

const options = {
    width: 640,
    height: 640,
    crop: true,
};

const host = 'https://your-kirby-website.com/thumbs';
const image_id = 'page-id/image-filename.jpg';

const thumb_url = `${host}/${image_id}${serializeObjectToQueryString( options )}`;
<img src={thumb_url} width={options.width} height={options.height} />

Load multiple widths for srcset:

// createQueryString, url_base, image_id same as in example above

const sizes = [240,480,960];

let src = `${host}/${image_id}${serializeObjectToQueryString({ width: sizes[0] })}`;
let srcset = sizes.map( size => {
    const options = {
        width: size
    };
    return `${host}/${image_id}${serializeObjectToQueryString( options )} ${size}w`;
}).join(', ');
<img {src} {srcset} />

Note that the html code above is just an example and wouln’t work in a vanilla setup, but require some type of templating engine, e.g. Svelte.

Options and security considerations

Whenever a thumbnail is requested for the first time, Kirby will generate it and store it in the media folder. So whenever a thumb is requested for the first time, it will take a little longer.

This also means that people with bad intentions could exploit this to overwhelm your server and fill up your disk space. In order to prevent this, you should restrict the allowed transformations to only the ones you actually need.

Default settings:

// site/config/config.php

return [
    'moritzebeling.headless-image-transformations' => [
        /*
        These values can either be:
        - false (not allowed, will be ignored and might fallback to Kirby's default settings)
        - true (any value is allowed, do not use in production)
        - array (list of allowed values)
        */
        'allowed' => [
            'autoOrient' => false,
            'crop'       => [true,false],
            'blur'       => false,
            'grayscale'  => false,
            'height'     => [40,80,160,240,360,480,640,720,960,1280,1440,1920,2560,3200],
            'quality'    => false,
            'width'      => [40,80,160,240,360,480,640,720,960,1280,1440,1920,2560,3200],
        ]
    ],
];

When sticking to the default options, it is only allowed to request thumbs with the specified widths and heights as well as cropping. If you need the other options, you should enable them via your site/config/config.php file.

You can also set some thumbnail option defaults for Kirby: https://getkirby.com/docs/reference/system/options/thumbs

Development

  1. Install a fresh Kirby StarterKit
  2. cd site/plugins
  3. git clone this repo

Roadmap:

  • Discuss if preset images sizes make sense
  • Check if browser caching works as expected

☕️ Support

If you like this plugin, I would be glad if you would invite me for a coffee via PayPal. If you have any ideas for further development or stumble upon any problems, please open an issue or PR. Thank you!

Warranty

This plugin is work in progress and comes without any warranty. Use at your own risk.

moritzebeling/kirby-headless-image-transformations 适用场景与选型建议

moritzebeling/kirby-headless-image-transformations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 10, 最近一次更新时间为 2023 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 moritzebeling/kirby-headless-image-transformations 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 10
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-08