Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
return an image directly from a controller
  • I'm wanting to automatically generate thumbnails based on a URI, so I can use the controller as an IMG SRC:
    [img src="/thumbs/my_photo/300/300"]where 'thumbs' is a controller followed by params for the image file and dimensions This way I can simply use images in any size without having to generate them prior. The controller will create the image at that size if it doesn't exist and return it as it would any old image with the appropriate http headers. The docs hint at this with an output method but I can't get this to work or I'm using it incorrectly - I get 500 errors; it appears to be returning a Fuel image object not an actual byte stream as I'd hoped. // Output as jpeg Image::load( 'filename.gif' )->output( 'jpeg' ) My fall back is to use file_get_contents but I was hoping to do something more, um, Fuel inspired, like Image::load( 'myimage.jpg' )->crop_resize(100,100)->save( 'newimage.jpg' )->output( 'jpeg' ) Can this even be done using a Request object? I'd appreciate any ideas or suggestions. Thanks.
  • The issue here is that you would want to return only the binary image data, and nothing else. Unfortunately, even a die() or exit() in your code won't stop FuelPHP's event handler from doing it's thing, in this case run shutdown events. These will send extra headers out to the client, corrupting your image. To deal with this you have to setup your own shutdown event handler and have that send the image binary back. You can find an example of how this works in the core method File::download() which does exactly the same for file downloads.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion