简体   繁体   中英

display image from storage laravel 5.3

I'm new to laravel framework, I want to display an image stored in storage folder. I tried this

Route

 Route::get('/userimage/{filename}', [
        'uses' =>'PostController@getUserImage',
        'as' => 'account.image',
    ]);

blade file to get the url and display the image

 </section>
    @if (Storage::disk('public')->has($user->first_name . '-' . $user->id . '.jpg'))
        <section class="row new-post">
            <div class="col-md-6 col-md-offset-3">
                <img src="{{ route('account.image', ['filename' => $user->first_name . '-' . $user->id . '.jpg']) }}" alt="" class="img-responsive">
            </div>
        </section>

controller

 public function getUserImage($filename)
{
    $file = Storage::disk('public')->get($filename);
    return new Response($file,200);
}

How can I display the image using the code above ?

Your controller should handle all the "behind the scenes" process, and your view only display html and php variables. That is the main point of the MVC pattern.

So your controller should return a view, and inject in its view the file path.

public function getUserImage($filename)
{
    $file = Storage::disk('public')->get($filename);

    return view('yourviewnamehere', ['myFile' => $file]);
}

Now, your view will be able to access the variable $myFile :

<!-- ... -->
    @if (Storage::disk('public')->has($user->first_name . '-' . $user->id . '.jpg'))
        <section class="row new-post">
            <div class="col-md-6 col-md-offset-3">
                <img src="{{ URL::to('img/' . $myFile) ]) }}" alt="" class="img-responsive">
            </div>
        </section>
    @endif
<!-- ... -->

I assumed the file path for the images in public/img/ , so feel free to change this part of the code. Also, whether your variable contains the suffix .jpg or not, you should add it or not.

I had the same problem. The thing is, you can not access stuff in storage so as it is suggested here you can simply make a symbolic link from public/storage to storage/app/public by using this command php artisan storage:link and then you can access whatever in storage directory through public directory like this asset('storage/path_to_file') , of course sometimes it is not possible for you to do so (for instance when you are deploying your project to a shared host). If this is the case, you can try uploading the files to public directory(which is accessible to everyone) and address the files using asset() method 1. you can simply define a new disk driver in disks array which exists in config/filesystems.php . in laravel 5.4, three drivers have been defined by default( 'local' , 'public' , 's3' ). you just need to add your own driver which here we call it 'MyDiskDriver' (but you can call it whatever you want). config/filesystems.php

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_REGION'),
            'bucket' => env('AWS_BUCKET'),
        ],
        'MyDiskDriver' => [
            'driver' => 'local',
            'root' => public_path('uploads'),
            'url' => env('APP_URL').'/public',
            'visibility' => 'public',
        ],

    ],
];

notice that for 'MyDiskDriver', 'root' has been set to public/uploads (which means when you use store method of Illuminate\\Http\\UploadedFile class and pass the name of the 'MyDiskDriver' as the second argument, laravel would upload the file in public/uploads directory.)

  1. when you are using store method of Illuminate\\Http\\UploadedFile class, you can pass the name of your newly created disk driver(here 'MyDiskDriver') as the second argument so that laravel can use it to store the file ,you are trying to store, in the directory that you specified in 'root'(here public/uploads).

  2. you can access the uploaded file using the asset() method.

Edit

Instead of doing step 2, you can change the value of FILESYSTEM_DRIVER environment variable in .env to the name of the disk driver that you have just created, In our case MyDiskDriver . This way you can easily change the disk driver without the need of changing your code, just by changing the FILESYSTEM_DRIVER environment variable's value

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

Check the first answer, you can replace all the File stuff with Storage .

Here is a function I made:

public function getUserImage($filename)
    {
        $storagepath = $filename;
        if (!Storage::disk('public')->exists($storagepath)) {
            $storagepath = 'defaultuserimage.jpeg';
        }
        $path = Storage::disk('public')->getDriver()->getAdapter()->applyPathPrefix($storagepath);

        return Image::make($path)->response();
    }

It requires the intervention image package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM