Ivan Draganinski Ivan Draganinski - 10 days ago 9
PHP Question

How to show image from storage folder in Laravel 5.3?

I have a folder in Laravel 5.3 project located in storage and it contains image files.

How can i show image from that folder?

I have all the names with

Storage::allFiles('images');


I can't access them in the usual way due to a folder permissions and i don't want to change them.
I also don't want to put the files in Public directory.

Any suggestions?
Thanks in advance.

Answer

You could write a controller function to return the image...something like:

public function image($file){
    $image = storage_path('images/' . $file);

    if(!File::exists( $image ))
        App::abort(404);

    // using Intervention Image package
    return Image::make($image)->response('jpg');

    // using Laravel file response
    $headers = ['Content-Type' => 'image/jpeg'];
    return response()->file($image, $headers);
}

Of course this is very bare bones...you could spruce it up to resize on demand, examine mime types and return the correct response type based on that, etc.