LARAVEL - display images from storage folder

I'm new to Laravel and was wondering if anyone could help me with a simple image upload.

I have a form that allows a user to create a profile and upload and an avatar for their profile during the process. This all works great. Here is my controller code:

if (request()->hasFile('avatar')) {

        $file = request()->file('avatar');

        $file->storeAs('avatars/' . auth()->id(), 'avatar.jpg');
    }

      

The image is saved in memory / app / avatars / USER -ID / avatar.jpg

I'm not sure how to display the image from this folder. I have looked for solutions but I cannot use php artisan storage:link

as this is not my server. How do I get around this without using a repository reference? Can I create a link manually?

If someone can explain the solution to me that would be great!

Just ask if you need code snippets.

Thank!

+3


source to share


3 answers


You will need to access the protected files using a route that connects to a controller that can access the files and pass them to the view.

I would also recommend using a package intervention/image

that can be installed

composer require intervention/image

See below for image access:

// Your route to web.php

// Public route to show user avatar
Route::get('avatars/{id}/{image}', [
    'uses'      => 'TheNameOfYourController@userProfileAvatar'
]);

      

// In your controller file. In this example, according to the name of what we had above, this would beuserProfileAvatar()

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Image;

class TheNameOfYourController extends Controller
{

    /**
     * Show user avatar
     *
     * @param $id
     * @param $image
     * @return string
     */
    public function userProfileAvatar($id, $image)
    {
        return Image::make(storage_path() . '/avatars/' . $id . '/' . $image)->response();
    }

}

      

// View your blade to display the image



<img src="images/profile/{{ \Auth::user()->id }}/avatar.jpg" alt="{{ \Auth::user()->name }}">

      

Here are some links, but that means take it one step further and save the path to the database file on load:

// Example route

// Example controller

// Examples for viewing

+3


source


Ok, if you don't have access to the server to create a symbolic link, you need to create a route that returns an image as a response.

Something like that:



Route::get('storage/{filename}', function ($filename) 
{
   $path = storage_path('avatars/' . $filename);

   if (!File::exists($path)) {
      abort(404);
   }

   $file = File::get($path);
   $type = File::mimeType($path);

   $response = Response::make($file, 200);
   $response->header("Content-Type", $type);

   return $response;
});

      

+1


source


Create a symbolic link that will connect to directories storage/app

and public/img

for example:

ln -s /home/laravel/public/img /home/laravel/storage/app

      

Then just use asset()

helper to create the link:

<img src="{{ asset('img/avatars/'.auth()->id().'/avatar.jpg') }}">

      

0


source







All Articles