Laravel: Display image from public storage - laravel

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

Related

Image is not displaying after successfuly uploading it?

Image is not displaying in the nav bar after uploading it to the database. I think there is something wrong with the file storage path, but I don't know where I am making a mistake.
This is the image storage path
$image = $request->file('picture')
$path = $image->store('public/images');
$yourModel = new User();
$yourModel->name = $validateData['name'];
$yourModel->email = $validateData['email'];
$yourModel->password = $validateData['password'];
$yourModel->image = $path;
you store the image in the public iamges and in blade you say show the iamge form the storage/images that is incorrect you need to change the ima html to <img class="" src="{{ asset('images/' . Auth::user()->image) }}">

Image not displaying in show blade but displaying in index blade

Please I need your help. So am trying to display an image in my view, it quite working on the index blade but not working on the show with the same code.
Here is my code
<img src="{{asset('/blog_images/'.$post->photo )}}" style="width:300px; height:150px" alt="NO IMAGE">
I've even gone ahead to try other helpers like public_path, url.
Please note that i inspected on the browser and it returned the full part to the image yet didn't display.
I'm thinking it might have to do with a certain settings somewhere.
Cheers!
What is in your Controller show method?)
It must be somthing like this:
public function show($id)
{
$post = Post::find($id);
return view('post.show', compact('post'));
}

Show an image in a blade file after using Intervention Image (Laravel)

I want to show an image in a blade template that contains already some HTML code after doing some modifications (resize, insert...) to this image using the Intervention Image library.
Example :
public function image(){
return $img = Image::make('images/test.jpg')
->resize(800, 500)
->insert('some_url', 'bottom-right', 10, 10)
->text('foo', 100, 100, function($font) {
$font->file('fonts/font.ttf');
$font->size(100);
$font->color('#fdf6e3');})
->response();
}
This method is called using this route :
Route::get('/image', 'MyController#image');
The result is perfect :)
but I want to show this image in a blade template that contains already HTML code, and not just return the image using the route.
Thanks in advance.
Try just setting returned like this <img src="$img"> in your blade.

Show the result of a route in blade view (Laravel)

I have a route that return an image, and i want the show the result of this route in a blade view, is there a way to directly call controller action or result of this route directly in the blade view ?
Thanks in advance.
You rather don't want to return image in blade. You should create separate controller action, that will return this image and link image src to this route, for example in controller:
public function image()
{
return $img = Image::make('image.jpg')->resize(800, 500)
->insert('sub_image.jpg', 'bottom-right', 10, 10) ->response();
}
then you create route to this controller action:
Route::get('my-image', 'MyController#image')->name('get-my-image');
and then in your Blade view you should link image src to this action:
<img src="{{ route('get-my-image') }}">
You can do this by giving the URL in src attribute of <img> tag as:
<img src="{{ route('image.url') }}">
Well if it's an image, why don't you use an img tag?
<img src="{{ route('my.image') }}" alt="My Image">

Symfony2 - Checking if a file exists in Twig

Is there a way to check if the file exists in Twig for an image uploaded in a newly created post?
The way I have my post setup is if an image is not uploaded the Twig template will show an empty box with the proportions of where the image should be, but it's a blank box that's awkward to look at.
Question, is if there is a specific command to use with an if statement to check if a file exists if so, display the image code if not do not display the image code.
Image code: (this is the code I want to hide if no image exists to get rid of the empty box)
<div class="entry-thumbnail">
<img width="230" height="172" src="{{ asset(['images/', post.image]|join) }}" class="" alt=""/>
</div>
you could write a twig extension and use some code like this:
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Finder\Finder;
$this->fs=new Filesystem();
if (!$this->fs->exists($directory)) {
$this->logger->info("there is no ".$directory." ...");
} else {
$this->logger->info("Directory ".$directory." already exists.");
}
but i would rather use javascripz like
in your img tag :
<img ... onError="this.src='imagefound.gif';" />
update:
The image Tag has an onError-Handler that triggers when the image in the src attribute is not found. So instead of using this simple inline syntax you could do the same like :
pseudo jquery code:
$(document).on('error','img',function(){
$(this).attr("src","../path/to/placeholerImage.png");
});

Resources