0

Is it possible to use Luracast Restler to return an image? Something like calling:

http://myserver.com/api/users/002/avatar

to download a png?

Jelle
  • 1,024
  • 10
  • 18

1 Answers1

2

It is possible to serve images with Restler.

You need to do the following in your API method

  • Set the content type header for the right image type (png, jpeg etc)

    header("Content-Type: image/png");
    
  • echo the image content

    Example

    $im = imagecreatefrompng("test.png");
    
    header('Content-Type: image/png');
    
    imagepng($im); //this sends the image as the response
    imagedestroy($im);
    
  • use exit or die to stop execution instead of the usual return result

Arul Kumaran
  • 983
  • 7
  • 23
  • That sounds a lot easier than what I was getting into: extending the Format class to serve pngs... Thanks – Jelle Feb 12 '13 at 18:49
  • An imageloader http client is setting the Accept header to image/*, and now Restler complains "Content negotiation failed. Try 'application/json'". Should I add an empty ImageFormat class with MIME="image/*" ? Or is there a better way? – Jelle Feb 28 '13 at 15:19
  • 1
    Yes, you need to add a dummy ImageFormat class – Arul Kumaran Mar 01 '13 at 03:34