ElpsySec's avatar

Images and the Storage API

Greetings Laracasters, I have a general design question and I would appreciate your input.

I am trying to design an Image class that I can save to storage. This way, users can upload an image. On this image class I have an add function that stores the image in local storage with the storage api. For retrieving and displaying that image, i need some input.

Should I have a get function that simply places/moves the image into public/images and refer to the image with an tag. Wouldn't I have to worry about removing it from public?

The second approach Im not terribly sure about the advantages or possible problems. I would have a get function that returns either a response, similar to this function I found:

    public function get($filename){
    
        $entry = Fileentry::where('filename', '=', $filename)->firstOrFail();
        $file = Storage::disk('local')->get($entry->filename);
 
        return (new Response($file, 200))
              ->header('Content-Type', $entry->mime);
    }

Any advice, online tutorials, or documentation references would be appreciated.

EDIT: I sort of wrote this not understand how the Response facade works, but now I get that it returns the exact same thing (an http response) as any href to any image. I thought it was returning a view for a second. Anyways, again, tips and advice on images and the storage API would be great. Thanks

By the way for those trying to do something similar, this is the tutorial I've been working off of: https://www.codetutorial.io/laravel-5-file-upload-storage-download/

The Storage API documentation is great as well: http://laravel.com/docs/5.1/filesystem

0 likes
0 replies

Please or to participate in this conversation.