lkssmnt
November 28, 2023, 12:08pm
1
Hi!
I have some images that are being loaded in JS using fetch. I’ve set up a content representation json that generates the urls for me. It looks like this:
[...]
foreach ($projects as $project) {
if ($project->featuredimage()->isNotEmpty()) {
$featuredImageUrl = $project->featuredimage()->toFile()->thumb(["width" => "800", "height" => "800", "quality" => "80"])->url();
} else {
$featuredImageUrl = "";
}
[...]
But it turns out this does not trigger the thumb creation process. The urls have the correct filename but they load the full images.
Is there a way to force Kirby to generate those specific thumbs for me, other than having a page where all of those images are displayed once?
Thanks!
texnixe
November 28, 2023, 12:38pm
2
One options would be the thumbs job in this plugin: GitHub - bnomei/kirby3-janitor: Kirby 3 Plugin for running commands like cleaning the cache from within the Panel, PHP code, CLI or a cronjob
Or you generate the thumbs needed on file create/replace using the respective hooks:
I haven’t tested k4 yet.
But maybe You could use a hook to trigger the thumb generation after file upload/ replace.
Something I’d do in a k3 plugin (not tested, just to get the idea):
use Kirby\Cms\App;
use Kirby\Cms\Exception;
Kirby::plugin('k-community/auto-thumb-or-whatever', [
'hooks' => [
'file.create:after' => fn($file) => $file->autoThumb(),
'file.replace:after' => fn($newFile, $oldFile) => $newFile->autoThumb()
],
'fileMethods' => [
'autoThumb' => function() {
$kirby =…
lkssmnt
November 28, 2023, 12:51pm
3
Hi texnixe,
thanks for the heads up! Good to know!
The problem with my code was on me or on the server rather. Something went wrong with imageMagick and the convert just never ran. It actually runs the conversion job from reading the image urls from home.json!