mardi 30 août 2016

Show secured images with Laravel 5 and Blade

I don't want that users can access images from simply using a URL like http://ift.tt/2bPGKCQ. Therefore I store all images in the storage directory, instead of storing them in the public folder.

With a controller like this

public function show($id) {
    $img = storage_path('my_images/123.jpg');
    return response()->download($img, null, [], null);
}

the image is correctly displayed by Laravel 5, although it is stored outside from the public folder.

Great up to this point ... but I want to use Blade and surround the image with some more details. Therefore my controller looks like this

public function show($id) {
    $picture = Picture::find($id);
    $img = storage_path('my_images/123.jpg');
    return view('pictures.show', array('picture' => $picture, 'img' => $img));
}

and the view like this

<!DOCTYPE html>
<html>
    <head>
        <title>Picture </title>
    </head>
    <body>
        <h1>Picture </h1>
        <ul>
            <li>Name: </li>
            <li>Description: </li>
        </ul>
        <img src="">
    </body>
</html>

which obviously does not work, because the location of the image is not publicly accessible (which ist the intention).

What do I have to use instead of <img src=""> to make this work?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire