Kirby CMS file method to optimize images using ImageOptim PHP API within your template code.
Usage
In your site/config.php
activate the plugin and set the ImageOptim API key.
c::set('plugin.imageoptim', true); // default is false
c::set('plugin.imageoptim.apikey', 'YOUR_API_KEY_HERE');
The plugin adds a $myFile->imageoptim()
function to $file objects.
// get any image/file object
$myFile = $page->file('image.jpg');
// get url (on your webserver) for optimized thumb
$url = $myFile->imageoptim();
// echo the url as image
// https://getkirby.com/docs/toolkit/api#brick
$img = brick('img')
->attr('src', $url)
->attr('alt', $myFile->filename());
echo $img;
Changing width, height and/or fitting is also supported. Modifying dpr and quality setting as well.
// fit to 400px width
$url = $myFile->imageoptim(400);
// fit to 400px width and 300px height
$url = $myFile->imageoptim(400, 300);
// crop to 800x600px dimension
$url = $myFile->imageoptim(800, 600, 'crop');
// fit to 400px width and 300px height at 2x dpr
$url = $myFile->imageoptim(400, 300, 'fit', 2);
// fit to 400px width and 300px height at 2x dpr and 'high' quality
$url = $myFile->imageoptim(400, 300, 'fit', 2, 'high');