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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

And for every image i should call this function?
Yah, you'd have a route set up: Route::get('/image/{file}', 'ImageController@image'); so when you make a GET request to /image it'd serve through this function.
btw, for similar use case as this, I have had really good results from the Intervention Image package. image.intervention.io
Oh whoops! I guess my example did use the Intervention image package...you could easily rewrite it to just deliver an image response... return response()->file($image, $headers);
You'd add a route (like I mentioned in my first comment) to access the image by URL...use that route in your img src attribute.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.