arnoson/kirby-auto-srcset

Generate image srcset automatically

v2.0.0 2022-09-30 09:21 UTC

This package is auto-updated.

Last update: 2024-10-30 21:21:44 UTC


README

A plugin for Kirby 3 to generate an image srcset automatically. Based on Processwire CMS user DaveP's forum post.

Installation

composer require arnoson/kirby-auto-srcset

Usage

$srcset = $image->autoSrcset([
  'minWidth' => 300,
  'maxWidth' => 1024,
  'thumb' => [
    'format' => 'avif',
    'quality' => 80,
  ],
]);

This will create the min and max dimensions as well as the dimensions in between, trying to create the images in roughly 20kb file size steps.

Configuration

All options can be passed to $file->autoSrcset() directly or set in the config.

// your-template.php
$srcset = $image->autoSrcset([
  'minWidth' => 300,
  'maxWidth' => 1000,

  // in kb
  'fileSizeStep' => 20,

  // The maximum number of images to be created.
  'maxSteps' => 10,

  // An optional ratio that is used to crop the image.
  'ratio' => 16 / 9,

  // Options to pass to kirby's `$file->thumb()` method.
  'thumb' => [
    'quality' => 80,
    'format' => 'jpeg',
    'crop' => 'center',
    // ...
  ],
]);
// config.php
return [
  'arnoson/kirby-auto-srcset' => [
    'minWidth' => 300,
    'maxWidth' => 1000,
    // ...
  ],
];