We had issues with thumb generation and we’d like to provide our client with a way to evaluate generated thumbs for a given image. The idea is to have a files section on the image view to visually check thumbs for errors (half-generated jpegs and such). A few questions:
- Given an image file object, is is possible to get a collection of all related thumbs that have been generated in the
media
folder so far?
- Is there a way to feed a files section with such a collection of the generated thumbs from the
media
folder?
- If so, is there a way to make the files section use the thumbs provided with the collection directly without generating smaller thumbs of these thumbs?
Since thumbnails (or the jobs to create thumbnails) are only generated when the template code is executed it is not an easy task to preview all generated thumbs.
I would also be careful with such a panel plugin because it can make your panel really slow depending on the number of images and thumbs generated.
Here is a simple solution that will show you all generated Thumbs for a specific page when you add /thumbs
to the url in the frontend. This script will make a get request to the page ensuring that thumbs for this page are getting generated.
If you use images from one page on another page (e.g. overview) you need to call those pages manually to generate those thumbs.
<?php
// config.php
return [
'routes' => [
[
'pattern' => '(:all)/thumbs',
'action' => function ($path) {
$root = kirby()->root('media');
$page = page($path);
try {
Remote::get($page->url());
} catch (Exception $e) {
//
}
$thumbs = [];
foreach ($page->images() as $image) {
$filename = $image->filename();
$dir = dirname($image->mediaRoot());
$url = dirname($image->mediaUrl());
// Thumb jobs
foreach (Dir::files($dir . '/.jobs') as $file) {
$thumbs[] = $url . '/' . rtrim($file, '.json');
}
// Already generated thumbs
foreach (Dir::files($dir) as $file) {
$thumbs[] = $url . '/' . $file;
}
}
// Remove any duplicates
$thumbs = array_unique($thumbs);
// Sort thumbs
asort($thumbs);
foreach ($thumbs as $key => $value) {
$thumbs[$key] = Html::img($value);
}
return new Response(implode('<br>', $thumbs));
}
]
]
];
Thanks, that seems to be a good direction.
This script will make a get request to the page ensuring that thumbs for this page are getting generated.
In our case, it’s not required to generate missing thumbs. It’s just about reviewing finished jobs.
I would also be careful with such a panel plugin because it can make your panel really slow depending on the number of images and thumbs generated.
Sure, that’s why our idea was to show a files section on the image view. This would result in a maximum of six images in our case.