Home > Software design >  How to return an Image on this API Call using Laravel
How to return an Image on this API Call using Laravel

Time:10-05

Im trying to make a call on an endpoint where the user uploads an image. I would like to return an image from the call, not a json. This is a Post request and Im using the Laravel HTTP Client methods.

Here's my code :

class PostImageController extends Controller
{
    /**
     * @param Request $request
     */
    public function scan(Request $request)
    {
        $url = env('AZURE_FUNCTION_PYTHON_DOCUMENT_SCANNER_URL');
        $file = $request->file('document')->get();

        $response = Http::withHeaders([
            'Content-Type' => 'multipart/form-data',
        ])->attach(
            'document',
            $file,
            'image.jpeg',
        )->post($url, $request->all());

        // Change this line to return an image
        return $response->json();
    }
}

Any idea is welcome.

CodePudding user response:

You need to stream the file given from returned URL from response (if it's there)

return response()->file($fileURLGivenFromHttpResponse);

CodePudding user response:

In order to return an image, you will need something like this

return Response::make($image, 200)->header('Content-Type', $type);

where $image holds the image that you want to upload and $type holds image type (for example image/png)

  • Related