Second beta for Kirby 6. Install it with composer require timnarr/kirby-imagex:^0.6@beta. For Kirby 5, stay on 0.5.x. If you use the imagex-picture-json output, see the migration guide below: its structure changed.
Added
- New
$file->imagex($options)file method: creates anImagexinstance for the file with the same options as the snippets. Echo it to render the<picture>(via theimagex-picturesnippet, including your own override), or usetoArray()/json_encode()for the JSON data. Unlike the snippets, it only gets the options you pass: unknown options throw, and controller variables named like an option (e.g.$ratio,$image) are never picked up, which Kirby otherwise passes to every snippet. - New public
Imagex::toArray(): everything needed to render the<picture>, in the structure of theimagex-picture-jsonsnippet, e.g. to return it from a Kirby route.Imagexalso implementsJsonSerializable, sojson_encode($imagex)returns the snippet's JSON. - Ratios may have spaces around the slash, like in CSS:
'16 / 9'. - Every global option except
cacheandstackcan now be overridden per image with the snippet option of the same name:addOriginalFormatAsSource,compareFormatsWeights,customLazyLoading,relativeUrlsandsrcsetInImg, likeformatsalready could.null(default) uses the global option. 'originalformat'informatsis now documented: it adds a<source>in the image's own format (e.g. JPEG), using the base srcset preset, at that position in the order. It already worked, but was undocumented.
Changed
- BREAKING: New structure for the
imagex-picture-jsonoutput.sourcesmoved frompicture.sourcesto the top level, sopicture,imgand every source are plain attribute objects that can be spread onto their element (previouslypicturemixed its attributes with thesourceslist).artDirectionStylesandpreloadLinksare always present: an empty string and an empty list when there's nothing to output (previously both were left out). The structure is now defined byImagex::toArray(). Migration: read the sources fromdata.sourcesinstead ofdata.picture.sources, and checkartDirectionStyles/preloadLinksfor emptiness instead of existence. See "Migration Guide" below. - BREAKING:
thumbRatio()throws anInvalidArgumentExceptionwhenheightis passed. The height is derived from the ratio, so it was overwritten silently. Migration: removeheightand pass awidth. - Renamed the global options
customLazyloadingtocustomLazyLoading(camelCase) andnoSrcsetInImgtosrcsetInImg(defaulttrue, the opposite value: no double negation likenoSrcsetInImg: false). The old names still work, with a deprecation warning in debug mode, and will be removed in a future release. Migration:'customLazyloading' => truebecomes'customLazyLoading' => true,'noSrcsetInImg' => truebecomes'srcsetInImg' => false. Imagex::getSmallestFormatForImage()is nowprotected. It was@internaland only public so tests could override it.- The internal helper functions moved from the
TimNarrnamespace toTimNarr\Imagex, so they can't collide with functions of othertimnarr/*packages. They are@internal; if you called them directly anyway, update the namespace.
Fixed
- Bug: The
imagex-picture-jsonsnippet output Kirby fields as objects:'alt' => $image->alt(), as used in the README, became"alt": {"value": "…"}. Attribute values that areStringable(e.g. Kirby fields) are now converted to strings when theImagexinstance is created, so the HTML and JSON output get the same value. - Bug: The
imagex-picture-jsonsnippet left out empty strings, e.g.alt=""for decorative images, while the HTML kept them. Attributes in the JSON output are now left out exactly like in the HTML output:nullandfalseremove an attribute (previouslyfalsewas output asfalse), empty strings are kept. classandstyleset tonull,falseor a Kirby field threw a type mismatch error.nullandfalsenow add nothing, e.g.'class' => $isActive ? 'active' : null, and fields are converted to strings.classstrings are split at any whitespace (tabs, line breaks), not only at spaces.- Bug:
styledeclarations without a trailing semicolon ran together when combined, e.g.'color: red'insharedand'margin: 0'inlazybecamestyle="color: red margin: 0". Each declaration now ends with a semicolon. - Bug:
thumbRatio()with'crop' => falsereturned a thumb in the image's own ratio instead of the requested one, e.g. 600×450 forthumbRatio('16/9', ['width' => 800, 'crop' => false]). Like in srcset presets,falseis now replaced withtrue. Crop positions like'top left'still work. Migration: for an uncropped thumb, use Kirby'sthumb()directly.
Migration Guide
JSON output
// Before
const { sources, ...pictureAttributes } = data.picture;
if (data.artDirectionStyles) { /* inject */ }
if (data.preloadLinks) { /* add links */ }
// After
const pictureAttributes = data.picture;
const sources = data.sources;
if (data.artDirectionStyles !== '') { /* inject */ }
if (data.preloadLinks.length) { /* add links */ }