Would like to ask if anyone has ever tried to display an image from a Laravel Controller. Below is my code to a Laravel Controller. So basically I just want to hide the actual url of image and add additional validation so I decided to the image call my laravel URL.
<img src="/image/1">
Route::get('/image/{image_id}', ['as' => 'site.viewImage', 'uses' => 'ImageController@viewImage']);
public function viewImage($image_id)
{
return Storage::get($image_id . '.png');
}
But this return an error not-found. Am I doing something wrong here? Note: I'm passing it to the controller because I need to do additional valdiation and to obfuscate the actual url of the file
I tried this code and its working but I would like a laravel type of approach
header("Content-type: image/png");
echo Storage::get($image_id .'.png');exit;
I also tried this approach
$response = response()->make(Storage::get($image_id . '.png'), 200);
$response->header("Content-Type", 'image/png');
return $response;
The laravel approach throws a 404 error.
Have you tried
return response()->file($filePath);
See: https://laravel.com/docs/5.3/responses#file-responses
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With