Trouble returning an image file via the router

I’m trying to return images using the router with the show method. From what I understand, given a file object, the show method returns the file contents with correct headers. My tests with this either returns a corrupt image or an empty image. Simplified example code is something like:

return array(
  'pattern' => 'return/some/image',
  'action' => function () {
    page('page')->image('image.jpg')->show();
  }
);

Which would allow access to image.jpg in the browser like this:

<img src="http://mysite.com/return/some/image">

I definitely could be missing something, so any ideas or thoughts would be awesome!

This code works for me:

c::set('routes', array(
  array(
    'pattern' => 'return/an/image',
    'action' => function () {
      page('mypage')->image('myimage.jpg')->show();
    }
  )
));
1 Like

Where are you returning your code to? Normally you would use something like this in your config.php:

c::set('routes', array(
  array(
    'pattern' => 'return/some/image',
    'action' => function() {
      page('page')->image('image.jpg')->show();
    }
  )
));

Edit: @texnixe was faster. :smiley:

Ah yep, forgot to take out that lingering return in my example. I have my routes in separate files and then glob them into the routes array in config.php which is why the array gets returned. But, even when using the same code as you guys have here I’m getting empty or corrupt image files (I’ve tried a variety of images/image types).

It might have something more to do with my server config since the code is working fine for you guys. I’ll do some digging. Thanks for the quick responses!

Late reply here, but in case anyone runs into this, it was tracked down to the MinifyHTML Kirby plugin which was actually minifying the file output causing the corrupt results.

Related bug documented in the MinifyHTML repository.

1 Like