moeli
April 2, 2019, 12:17pm
1
Hi
I am trying to implement a custom thumb method .
$file->thumb(['width' => 400])
should trigger the native method while
$file->thumb(['width' => 400, 'own' => true])
should use my own method.
'components' => [
'thumb' => function (App $kirby, string $src, string $dst, array $options) {
if (isset($options['own'])) {
$rootToThumb = myThumbGenerator($src, $dst, $options);
return $rootToThumb;
}
else {
$darkroom = Darkroom::factory(option('thumbs.driver', 'gd'), option('thumbs', []));
$options = $darkroom->preprocess($src, $options);
$root = (new Filename($src, $dst, $options))->toString();
F::copy($src, $root);
$darkroom->process($root, $options);
return $root;
}
}
],
My problem is that I already fail at creating a thumb with the native method.
When I call $file->thumb(['width' => 400])->html()
everything works (urls, objects returned, …) but the thumb is not created.
It appears that the .json file in the .jobs directory is created and waiting to be called…but how? What am I missing?
It appears that the .json file in the .jobs directory is created and waiting to be called…but how? What am I missing?
I think you should request the the thumb via the browser. When it is requested, it should be created automatically afaik.
moeli
April 2, 2019, 7:08pm
3
Thank you for your answer @bvdputte .
I agree, this is the expected behaviour. Unfortunately as soon as I use the custom thumb method it does not work anymore.
The method is the one in my first post and in a template I am calling $file->thumb(['width' => 400])->html()
to display the picture. But it ends in a 404 request, because the picture is not (yet?) generated.
In other words, this does not work, even if it should be the same as if I was not changing the thumb method:
'components' => [
'thumb' => function (App $kirby, string $src, string $dst, array $options) {
$darkroom = Darkroom::factory(option('thumbs.driver', 'gd'), option('thumbs', []));
$options = $darkroom->preprocess($src, $options);
$root = (new Filename($src, $dst, $options))->toString();
F::copy($src, $root);
$darkroom->process($root, $options);
return $root;
},
],
@moeli Hm, I put your above code into a plugin file, and calling $file->thumb(['width' => 400])
works as expected.
I think the problem is that you are calling it with html()
, while echoing the thumb already calls the html()
method in the magic__toString()
method.
moeli
April 2, 2019, 7:21pm
5
@texnixe thanks for your answer.
I tried $file->thumb(['width' => 400])
which shows the same result for me. Everything seems to be working but whatever triggers the lazy image creation is not triggered.
Are thumbnails properly created if you call the native thumb()
method without your plugin?
moeli
April 2, 2019, 7:28pm
7
Yes that works perfectly.
I my eyes both thumb methods should be the same since I just copied code from the core…?
Does the code from above actually create a thumbnail for you?
Yes, the thumb was actually created, I deleted the media folder first. But the strange thing is that it doesn’t work anymore, now. So maybe I’ve gone crazy.
moeli
April 2, 2019, 7:40pm
9
Already two crazy people then
My browser shows a http 302 response on the not created image. Should it be a 404? Or is the request somewhere stopped on the way and therefore no lazy loading triggered?
My browser shows a 404, but I’m currently not trusting myself anymore…
Guess it was a false positive first, maybe due to some sort of caching.
moeli
April 2, 2019, 8:48pm
11
I got it working. I had to call the classes with namespace (or add use statements).
'components' => [
'thumb' => function (Kirby\Cms\App $kirby, string $src, string $dst, array $options) {
$darkroom = Kirby\Image\Darkroom::factory(option('thumbs.driver', 'gd'), option('thumbs', []));
$options = $darkroom->preprocess($src, $options);
$root = (new Kirby\Cms\Filename($src, $dst, $options))->toString();
Kirby\Toolkit\F::copy($src, $root);
$darkroom->process($root, $options);
return $root;
},
]
moeli
April 3, 2019, 6:45am
12
Just had another idea so I can use core components code instead of copying it.
$core_components = require kirby()->roots()->kirby() . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'components.php';
Kirby::plugin('moeli/own-thumb', [
'components' => [
'thumb' => function (Kirby\Cms\App $kirby, string $src, string $dst, array $options) use ($core_components) {
if (isset($options['own'])) {
$rootToThumb = myThumbGenerator($src, $dst, $options);
return $rootToThumb;
}
else {
return $core_components['thumb']($kirby, $src, $dst, $options);
}
}
],
]);
A bit more fail-safe:
Kirby::plugin('moeli/own-thumb', [
'components' => [
'thumb' => function (Kirby\Cms\App $kirby, string $src, string $dst, array $options) {
if (isset($options['own']) === true) {
return myThumbGenerator($src, $dst, $options);
}
$core = require $kirby->root('kirby') . '/config/components.php';
return $core ['thumb']($kirby, $src, $dst, $options);
}
],
]);
2 Likes
moeli
April 3, 2019, 9:15am
14
Kirby::plugin('moeli/own-thumb', [
'components' => [
'thumb' => function (Kirby\Cms\App $kirby, string $src, string $dst, array $options) {
if (isset($options['own']) === true) {
return myThumbGenerator($src, $dst, $options);
}
$core = require $kirby->root('kirby') . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'components.php';
return $core['thumb']($kirby, $src, $dst, $options);
}
],
]);
Thx @distantnative . Good idea. I just changed the root folder because ‘config’ seems to point to ‘site/config’
My bad, I fixed it in my answer above.