Returning response with the image contents NOT WORKING

38 Views Asked by At

I want to load an image as avatar of user like this at the Blade:

<img src="{{ route('adm.profile.details.show.avatar') }}" alt="user-img" title="Avatar" class="img-circle img-thumbnail img-responsive">

So it loads the image via this route:

Route::get('/avatar/display', [AdminController::class, 'showAvatar'])->name('adm.profile.details.show.avatar');

And then in the Controller:

public function showAvatar()
    {
        $avatarPath = "adm/avatars/" . auth()->user()->usr_name. ".jpg";
        $defaultAvatarPath = asset("assets/img/adm.png"); // Path to default avatar image in the public directory

        if (Storage::exists($avatarPath)) {
            $contents = Storage::get($avatarPath);
        } else {
            $contents = file_get_contents($defaultAvatarPath);
        }

        $headers = ['content-type' => 'image/jpeg'];
        return response($contents, 200, $headers);
    }

But now when the image with user name of user does not exist, it should load the default picture of user which is placed at public/assets/img/adm.png but somehow it does not work out!

So if you know, what's going wrong here and how can I load the default image properly please let me know...

0

There are 0 best solutions below