承接 moritzebeling/kirby-headless-image-transformations 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

moritzebeling/kirby-headless-image-transformations

最新稳定版本:1.0.1

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.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固