Thumbnails outside the Kirby structure

I want to generate a thumbnail outside the template/snippets. Then it will fall into the thumbs structure but I want to control the path where the thumbnail is stored as well.

Path to file:

my/custom/path/to/image.png

My thumbs path:

thumbs/another/path/that/i/choose/image.png

Is it possible?

I found this in another @lukasbestle but I’m not sure what it does:

$myimage = new Asset($filepath);
echo thumb($myimage, array('width' => 300));

Where is it stored? Can I set a custom path when I set it?

Yes, with the destination param:

$myimage = new Asset($filepath);
echo thumb($myimage, array('width' => 300, 'destination' => function($thumb) {
  return new Obj([
    'root' => 'your path',
    'url'  => 'corresponding URL'
  ]);
}));

Thanks! It looks nice but I can’t get it to work.

$filepath = 'a_working_path_to_a_png_file';

$asset = new Asset($filepath);

$object = new Obj([
  'root' => kirby()->roots()->thumbs() . DS . 'test.png',
  'url'  => 'https://example.com/test.png'
]);

$args = array('width' => 300, 'height' => 120);
$args['destination'] = $object;
echo thumb($asset, $args);

I expected test.php to be in my thumbs folder but it does not place an image there. Can you see where I’ve done it wrong?

  • I’ve tested the path to the file and it is working.
  • I don’t get an error message. I get nothing.
  • I prepare my arguments before I use them (which you have probably already figured out)

Update

I will solve it a bit different. Reply if you want, but it’s not very important anymore.

destination needs to be a callback that returns an Obj like in my example above.