Skip to content

Kirby 6 support (0.6.0) - #19

Draft
timnarr wants to merge 41 commits into
mainfrom
feature/kirby-6
Draft

timnarr wants to merge 41 commits into
mainfrom
feature/kirby-6

Conversation

@timnarr

@timnarr timnarr commented Oct 30, 2026

Copy link
Copy Markdown
Owner

Kirby 6 support for Imagex, released as 0.6.0-beta.1 from this branch.

Don't merge yet: this stays a draft while Kirby 6 is in beta. main keeps the 0.5.x line for Kirby 5. The PR exists so CI runs on the branch.

Highlights

  • Requires Kirby 6 and PHP 8.3+ (breaking, see CHANGELOG for the migration note)
  • New stack option: styles and preload links go into a Kirby template stack in the <head>
  • New preload option: <link rel="preload"> for critical images, one format per source, with mutually exclusive media conditions for art direction
  • New per-image formats option
  • Fixed: <source> elements are grouped by source, so compareFormats can no longer hide art-directed images
  • Fixed: files Kirby can't resize (e.g. SVGs) render as a plain <img>

Full list in CHANGELOG.md.

Before merging (stable 0.6.0)

  • Kirby 6 stable is out
  • require-dev: getkirby/cms ^6.0@beta → ^6.0
  • Combine the beta sections in the CHANGELOG into 0.6.0

Imagex only uses Kirby APIs that are unchanged in Kirby 6 (checked
against the migration guides, the changelog and getkirby/rector, which
finds nothing to migrate). The test suite passes on 6.0.0-beta.2.

Kirby 6 needs PHP 8.3+, so PHP 8.2 support and its CI syntax check are
dropped. Sites on Kirby 5 stay on 0.5.x.
README, examples, tests and error messages now write media conditions
as `(width >= 800px)` instead of `(min-width: 800px)` (Media Queries
Level 4). User-supplied `media` strings are still output unchanged.
With compareFormats and an art-directed image, the art-directed image
could be ignored: sources were grouped by format, so when the
art-directed image skipped AVIF but the main image didn't, the default
AVIF <source> (no media, always matches) came first.

Sources are now grouped by source: each artDirection entry in all its
formats, then the main image. An entry with its own image also keeps
formats that were only skipped for the main image. getPictureSources()
is built once per instance.

The compareFormats tests now pin the comparison result per image, so
every outcome is covered instead of whatever the encoders produce.
The example for compareFormats with art direction also lacked the main
image's WebP fallback.
thumb() returns the original for files like SVGs, so every format got a
<source> listing the original under every width, with a wrong type like
image/avif. Kirby 6 now reads dimensions from an SVG's viewBox, which
made this reachable without an error.

Such files are now rendered as an <img> with the original as src, no
srcset and no compareFormats run. width/height still come from the
preset and ratio. Art-directed entries need a resizable image and throw
otherwise.
Overrides the global `formats` for a single image, e.g. ['webp'] for
animated GIFs, which Kirby converts to a still AVIF, or [] for no
modern formats. null keeps the global option.
New `stack` plugin option: the name of a Kirby 6 template stack in the
<head>. The imagex-picture snippet pushes its <style> element there
instead of rendering it next to the <picture>.

New `preload` snippet option: adds <link rel="preload" as="image">
elements for critical (eager) images. The links are derived from
getPictureSources(), so they always match what <picture> uses: one link
per source in its first format, with mutually exclusive media conditions
(negated in range syntax where possible). The JSON snippet returns them
under `preloadLinks`.
- getPreloadLinks() builds the links in a foreach instead of
  array_values(array_map()) over values and keys
- validateArtDirection() reuses $sourceImage for the ratio check
- isFormatSkipped() returns early when compareFormats is off, so it no
  longer calls isResizable() for every source and format

No behavior change.
$page->image() is Kirby's built-in page method (the page's first
image), not the content field, in Kirby 5 and 6 alike. toFile() on the
returned File gives an empty content field, so Imagex threw 'must be an
instance of File'. The example now uses a files field named cover.
Kirby doesn't check extra.kirby.compatible at runtime, so Composer would
install this version into a Kirby 5 project running PHP 8.3+. The
conflict keeps those projects on 0.5.x.
An artDirection entry using a file Kirby can't resize now throws instead
of rendering broken <source> elements. That breaks pages that rendered
before, so move it from Fixed to Changed as BREAKING and add a migration
note.
- buildPreloadLinks() takes the <source> and <img> attributes and
  replaces the bodies of getPreloadLinks() and buildPreloadLink(), so
  it can be tested without Kirby
- wrapMediaCondition(), negateRangeMediaFeature() and
  exclusiveMediaConditions() move unchanged from misc.php, and their
  tests move to preloadTest.php
- getPreloadLinks() only checks the preload option and delegates

Imagex.php shrinks from 933 to 872 lines. No output changes.
getPictureSources() leaves out the main image if Kirby can't resize it,
and validateArtDirection() rejects such files, so isFormatSkipped() is
only ever called for resizable images.
A <source> doesn't inherit the <img>'s sizes and falls back to 100vw
without its own. As most browsers pick an AVIF or WebP <source>, the
examples that only set sizes on the <img> led to larger images than
needed. They now set sizes via attributes.sources too, and the README
explains why.
The example thumbs config uses quality 80 for WebP since the new rule of
thumb, but the example HTML and JSON output still showed q75 in the
WebP URLs.
…validation

Both are rules in CLAUDE.md that no test pinned down: removing
fetchpriority="high" from eager images, or the file's modification time
from the compareFormats cache key, left the suite green.
The function only returns the key, not the value. Also fix the @return
of the thumbRatio file method, which can return a FileVersion or an
Asset as well.
imagexTest.php had grown past 1100 lines. The shared setup (fresh Kirby
app, fixture images, Imagex factories) moves into the abstract
ImagexTestCase, loaded in the bootstrap. Also point phpunit.xml at the
schema of the installed PHPUnit 13.4.
addRatioBasedHeightToSrcsetPreset() receives all presets keyed by preset
name, but its loop variable was called $format. The variables and the
docblock now describe the actual structure. The height calculation is
unchanged, so rounding stays the same.
Flat attributes are wrapped in 'shared' first, so both forms go through
the same validation and normalization instead of two branches that
built the same array.
…mat()

- compareFormats, focus and preload share one check with the same
  error message as before
- drop the comments in resolveOptions() that only restated the code
- getImageFormat() had a single caller that always passed an image,
  so it's replaced by normalizeFormat($image->extension())
…L helpers

resolveCompareFormatsWeights() builds the list of presets for its error
messages once and handles strings before the type check. The URL
helpers use ??= like the rest of the code base.
All three branches built the same '(feature operator value)' string; a
closure now does that, so each branch only shows what differs.
getAspectRatioFromRatioString() takes x and y from the regex captures
instead of splitting the string again, and greatestCommonDivisor()
swaps via destructuring instead of a temporary variable.
getPreloadLinks() rebuilt getImgAttributes() for every call, so an image
with preload ran 5 extra thumb() calls (Kirby filename building and
file_exists() checks). The attributes are now built once, like the
<picture> sources.

Benchmark (median of 5 × 300 renders, alternating old/new, 3 rounds):
eager + preload: 1306/1302/1291 µs -> 1115/1101/1122 µs (-14.5 %),
26 -> 21 thumb() calls per render. Other scenarios unchanged (within ±3 %).
thumbRatio('16/9', ['width' => 800, 'crop' => false]) returned a
600x450 thumb: without cropping, Kirby fits the image into the box and
keeps its own ratio. A missing or false crop now becomes true, like
srcset presets already did. Crop positions like 'top left' are kept.

The srcset preset comment called false "invalid" although it is
replaced silently; it now says what happens, and tests cover both
paths, including that true anchors the crop at the focus point.
Kirby fields like $image->alt(), as used in the README, were encoded as
{"value": "…"} in the JSON, and transformForJson() dropped empty
strings, so alt="" for decorative images was lost in the JSON while the
HTML kept it.

normalizeAttributeValues() (replacing coerceClassStyleToArrays())
converts Stringable values to strings, treats null/false class and
style as empty instead of throwing, splits classes at any whitespace
and ends every style declaration with a semicolon, so 'color: red' in
shared and 'margin: 0' in lazy no longer run together.
transformForJson() leaves out attributes exactly like Kirby's attr():
null and false, but not ''.
The getters are public API, but only their names were documented. The
class docblock, the @return types and the README now spell out what an
attribute array contains: null/false mean "left out", class and style
are lists, width and height are integers.
The height is derived from the ratio, so a passed height was
overwritten silently. It now throws with a hint to pass a width.
Imagex outputs ratios as '16 / 9' in CSS, but rejected that format as
input. Spaces around the slash are now accepted, like in CSS
aspect-ratio.
It was public and @internal only so the test subclass could pin its
result. protected is enough for that and keeps it out of the public
surface.
picture mixed its attributes with the sources list, so
<picture {...data.picture}> rendered a sources attribute, and
artDirectionStyles/preloadLinks were left out when empty. sources is
now a top-level key, picture, img and every source are plain attribute
objects, and all keys are always present.

The structure lives in the new Imagex::toArray(). Imagex implements
JsonSerializable with every attribute array as an object, so an empty
picture is {} rather than [], and the JSON snippet only encodes the
instance.
Snippets read their options from get_defined_vars(), and Kirby merges
controller data into every snippet's scope. So a misspelled option was
ignored silently, and a controller variable named like an option
(ratio, loading, image …) was used whenever the option wasn't passed.

$file->imagex($options) passes the options straight to the
constructor: unknown options throw, controller variables can't leak in.
Echoing the instance renders the imagex-picture snippet, including
overrides in site/snippets. The snippet only takes over the instance
__toString() is rendering, matched by identity, so a controller
variable named imagex is ignored as well.
'originalformat' already worked in formats (it uses the base preset),
but was an undocumented magic string. It is now documented as the way
to place the image's own format anywhere in the order, and covered by
a test.
Only formats could be set per image. addOriginalFormatAsSource,
compareFormatsWeights, customLazyLoading, relativeUrls and srcsetInImg
are now snippet options too; null uses the global option. Error
messages name the option that was actually set.

customLazyloading becomes customLazyLoading (camelCase), and
noSrcsetInImg becomes srcsetInImg, avoiding the double negation
noSrcsetInImg: false. The old global names keep working, with a
deprecation warning in debug mode.
The helpers lived directly in TimNarr\, which the other timnarr/*
packages share, so a function of the same name in one of them would be
a fatal "Cannot redeclare". They are @internal, so the move is not
covered by semver. Imagex imports them via use function.
- Allow PHP ~8.6.0 in composer.json and add 8.6 to the CI matrix
- Require stable getkirby/cms ^6.0 for development
- Update README requirements and add the 0.6.0-beta.3 changelog entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant