API inline image from userspace

Hi,

I need to integrate images from the user filesystem in my plugin. From what I understand I can either generate a URL that requests the image directly (raw?) and use html:

<img src="https/nextcloud/generatedURL"/>

but I can’t find specific documentation on how to do this, or use the controller as per the API, read the image file, serialise the bitstream and override the Content-Type:

#[ApiRoute(verb: 'GET', url:'/image')]
public function image(string $id): DataResponse {
    $myimageresponse = new DataResponse($imagedata, Http::STATUS_OK, ['Content-Type','image/png']);
    return $myimageresponse;
}

but this doesn’t seem to override the HTTP headers and I get XML response.

Please can anyone advise on the best route to get an image to display from userspace?

Thanks,
Steve

Hey @stevemartinsuk!

Have you tried to use the DataDisplayResponse class (or the FileDisplayResponse for that matter) yet?

The DataResponse is intended for data (think XML, JSON and the like) to be packed in a generic way AFAIK.

Christian

Thanks for the suggestion, streamresponse worked. I may give filedisplayresponse a go as perhaps that is more appropriate.

Steve

1 Like