Cannot get image from public folder on Server in Laravel - laravel

I'm saving my uploaded image in public folder like
public/user/user_img_1.jpg
and saving
/user/user_img_1.jpg
in my database column as image path. Now when i get it
<img src="{{ $userData->thumb_img }}" alt="Profile Image" /> , the image shows on localhost but on server the image is not found. $userData variables stores the path.

We need to give permissions in Laravel for uploading and fetching the images.
Suppose you have saved the image in the public folder under the user folder.
Directory Path: public/user/
And save the image in Database like below:
key: thumb_img
value: "public/user/imagename.jpg"
{{ asset($userData->thumb_img) }}
You need to use the asset function for fetching the image.

Use asset function to generate a URL for an asset using the current scheme of the request.
In your case that would be: <img src="{{ asset($userData->thumb_img) }} alt="Profile Image" />

You have to do any one step :
Put all your files (img) into the public folder.
<img src="/user/{{ $userData->thum_img }}" alt="Profile Image" />
Or, Use asset helper :
<img src="/user/{{ asset($userData->thum_img) }}" alt="Profile Image" />
where user_img_1.jpg is path of your content.

Related

Laravel: Display image from public storage

I am saving an uploaded image like this:
$profile_picture = $request->file('profile_picture');
$user->profile_picture = $profile_picture->getClientOriginalName();
Storage::disk('public')->put( $user->profile_picture, $request->file('profile_picture') );
and then I am trying to display that image in HTML with:
<img class="img-fluid" src="{{ asset('storage/'. Auth::user()->profile_picture) }}">
the image is being saved in storage/app/public, but the image is not showing in html, how do i go about this?
thank you
You may use response method on a Laravel filesystem
return Storage::disk('public')->response($user->profile_picture);
N.B: In order to do that, you need a Laravel route. The above code should be located in a controller method

Media Finder display Image on Front End

I created a Model and Database and can upload an image using the Media Finder.
I set Media Finder field as photo in fields.yamal.
But after uploading, no image path is saved to the database in the photo column.
Database Tables
id (integer)
title (string)
photo (string)
Twig
{% for product in builderListCatalog.records %}
{{ product.title }}
<img src="{{ product.photo }}" />
{% endfor %}
This successfully displays the Title but not the Photo path because it is empty in the database.
You need to remove all the relation added to that field from model.
You are using public $attachOn remove it that is the issue now its treated like file attachment relation not a finder image so it will not store path of image in that photo (string) field in table
If you use media finder you just need to add normal string (photo) field in table no need to add any relation in model and it will just store selected file path in that field for ex. /someimage.png
Now to show it in front-end you can use
<img src="{{ model.photo|media }}" />
In component partial you can use
<img src="<?php echo 'https://october-plaza.com/storage/app/media/' . $model->photo ?>" />
if any doubt please comment.

Why is the avatar on app.blade is broken whenever I open links that has id?

Whenever I open an item the link would be
http://localhost/mywebsite/public/item/{$item_id}
the avatar will break. Below is the code I used
<img src="uploads/avatars/{{ Auth::user()->avatar }}">
The location of the image is C:\xampp\htdocs\mywebsite\public\uploads\avatars
I presume that as you are using relative URLs the translated path in browser will be http://localhost/mywebsite/public/item/{$item_id}/uploads/avatars/{{ Auth::user()->avatar }}
And that's not what you want
Try
<img src="http://localhost/mywebsite/public/uploads/avatars/{{ Auth::user()->avatar }}">
In production in will be just
<img src="/uploads/avatars/{{ Auth::user()->avatar }}">
Am not sure it will work on xampp though
So to say, I recommend using Laravel Valet or Homestead instead of Xampp, it cuts some problems like yours off.
in File model i created a function to get file encoded base64
public function getFile()
{
$path = storage_path('app'). "/" . $this->path;
$type = File::mimeType($path);
$file = Storage::disk('local')->get("/". $this->path);
return "data:image/png;base64,".base64_encode($file);
}
in blade You can use like that
<img src = '$file->getFile()'>

Laravel 5.3 show images in the view

I am having trouble showing images. I have stored images in:
nameproject/storage/images/2017/sep/
and I have created a link from the public folder:
ln -s storage/images public/images
and in my view:
<td><img src="{{ asset($post->image )}}" height="42" width="42"/></td>
but I receive the error:
GET https://challenge.local/images/2017/Sep/1504974522.jpeg 404 ()
try with this..images folder in public folder..
{{ URL::to('/images/2017/Sep/'.$post->image) }}
http://localhost/laraveltest/public/images/2017/Sep/1504974522.jpeg

Display image in laravel

I have already uploaded the image in a folder and stored its path in the database field. I am trying to display image like this:
Controller:
public function showProfileImage($id)
{
$profileimage = UserProfileModel::where(['usrID' => $id])->first();
return view('userlogin::welcome')->with('profileimage', $profileimage->userPicpath);
}
my blade template has:
<img src="{{ route('showprofileimage', ['id' => Session::get('loggedinuserid')]) }}" height="150" />
But the path returned in the src is the route path, it doesn't contain the 'profileimage' path returned form the controller. How do i do this? Thanks
The route('<named route>') helper function returns the URL to a page/endpoint/resource that was defined as:
Route::get('some/uri', ['as' => 'showprofileimage', function() {
//
});
It may not be used for displaying images (although it could be used with a few tweaks), but I would go with either one of the following approaches:
If you have the fully qualified image URL then you can use the url() helper function. Do:
<img src="{{ url('showprofileimage') }}" ... />
If your image is in the /public directory, you can use the asset() helper function:
<img src="{{ asset('path/to/profile/image') }}" ... />
If your image is in the /storage directory, you can use the storage() helper function (but I believe this is not recommended):
<img src="{{ storage('path/to/profile/image') }}" ... />
Hope this helps. Cheers!

Resources