Laravel 5 - How to access image uploaded in storage within View?

前端 未结 12 2709
悲哀的现实
悲哀的现实 2020-11-22 14:05

I have got user\'s avatars uploaded in Laravel storage. How can I access them and render them in a view?

The server is pointing all requests to /public,

12条回答
  •  时光说笑
    2020-11-22 14:37

    If you want to load a small number of Private images You can encode the images to base64 and echo them into directly:

    $path = image.png
    $full_path = Storage::path($path);
    $base64 = base64_encode(Storage::get($path));
    $image_data = 'data:'.mime_content_type($full_path) . ';base64,' . $base64;
    

    I mentioned private because you should only use these methods if you do not want to store images publicly accessible through url ,instead you Must always use the standard way (link storage/public folder and serve images with HTTP server).

    Beware encoding to base64() have two important down sides:

    1. This will increase image size by ~30%.
    2. You combine all of the images sizes in one request, instead of loading them in parallel, this should not be a problem for some small thumbnails but for many images avoid using this method.

提交回复
热议问题