Laravel doesn't show image but url is correct - php

I want to get an image that is stored like this: public/pics/products.
Code:
<img src="{{ $shopGSM->productshot->first()->image }}" id="webshoppic">
The result:
<img src="pics/products/d132da08c7c01659edad47142261301b.jpg" id="webshoppic">
But no picture :(, what do i do wrong?

You didn't include the site url in src attribute. use following code
src="{{ URL::asset($shopGSM->productshot->first()->image)}}"
This will return something like as follow
src="http://www.domainName.com/pics/products/d132da08c7c01659edad47142261301b.jpg"
Hope this will work for you

Related

How do I add images on my laravel project?

I am trying to add images so I can see it on my views. I put the folder img inside the public folder and this is the code I am trying to see.
<?php $image = public_path() . './img/logo.png'; // destination path ?>
<h1>Royal <img src="{{$image}}"> Dice</h1>
All I get is this error in the console: Not allowed to load local resource.
I am learning Laravel but I think that this should be easy to solve. Thank you everyone!
Following should work:
<h1>Royal <img src="{{ asset('img/logo.png' }}"> Dice</h1>
You can find more about the asset helper in the laravel documentation: https://laravel.com/docs/8.x/helpers#method-asset
wite like this <h1>Royal <img src="{{ URL::to($image) }}"> Dice</h1>
or
<h1>Royal <img src="{{ asset($image) }}"> Dice</h1>

image working in index.blade.php but not show.blade.php

I am using Laravel to build a Events management system. Users can upload images to be displayed with their event. I have the img tag working on my index.blade.php and assumed I could use the same code on my show.blade.php page, it seems not.
I have the image stored in public/images and I am aware I should use storage for this but I am still learning basics so found this easier for now.
<img style="width:100%" src="images/{{$event->image}}">
As #Frank Ayyaz said you can use a backslash to solve this another Laravel-way is using public_path method which returns the fully qualified path to the public directory.
so you can do something like:
<img style="width:100%" src="{{ public_path('images/'.$event->image) }}">
Please use asset() method. The asset() method is used to include CSS/JavaScript/images files, you can use it as:
{{ URL::asset('images/'.$event->image) }}
<img style="width:100%" src="{{ URL::asset('images/'.$event->image) }}">
You can also use this function for CSS and js.
<link href="{{ asset('css/style.css') }}" rel="stylesheet">
<script src="{{ asset('js/custom.js') }}"></script>
For detail please see this article:
https://medium.com/#zwacky/laravels-url-to-vs-url-asset-fd427ed6f7ef
I suggest Not to use URL::asset but only asset method because sometime URL may cause Problem while showing the image...and use dd() function and check whether u are getting the path i.e Public_path , storage_path() or the custom like the image are stored in public directory..
You may use asset() method of Laravel and use any Prefix whatever you like..
Example:
For static
<img src="{!! asset('assets/images/cams/cam-01.png') !!}" class="img-fluid"
alt="Card image cap">
For Dynamic
<img src="{!! $settings->aws_url.$performer->coverimage !!}" class="img-fluid" alt="Card image cap">
when u use js or css or images u should use {{asset('path')}} to get the resource, it has noting to do with the blade u use, but the resources path.

Laravel - Image does not appear in view

I am using Laravel, and I want to display an image. I have the public/img folder, and this is the code
<img src="{{ URL::asset("img/background1.jpg") }}" alt="Unsplashed background img 1">
The image does not appear. When I inspect the page however, it returns the correct link, http://localhost:8000/img/background1.jpg but it says 0x0 px.
Add a / in front as the public folder is the root...
<img src="{{ URL::asset("/img/background1.jpg") }}" alt="Unsplashed background img 1">
Are you getting correct image when you open this link in browser? If so, then it would be issue with img height-width. Try setting static height-width for img and check.
Hope this helps..

Laravel image paths from controller to view

My image's path is public/uploads/users/image.png then using src like this:
<img src="{{ asset('uploads/users/image.png') }}" />
But for every user the image has to be changed.
I pass the image name of the user from controller as something like this
foreach ($user as $key) {
$this->data['user_img'] = $key->avatar;
}
Here, I have to change the user image as per $user_img from controller
How should I give it in the src of image tag.
I tried like this way
src="{{ asset('uploads/users/"{{$user_img}}"')}}"
But it seems to be an error for me.How should I resole this.
Somebody help me.
You can use this method for image in laravel.
if this is your image path public/assets/uploads/users/image.png.
image in $user_img
{{asset('assets/uploads/users').'/'.$user_img}}
Or we can use
{{ asset('assets/uploads/users') }}/{{$user_img}}
It is as simple as
<img src="{{ asset('uploads/users/' . $user_img) }}"/>
If that doesn't work the problem is not with the asset helper function but presumably in the value $user_img gets (or doesn't get..)
If you have laravelcollective/html, try this:
{{ Html::image('public/uploads/users/'.$user_img) }}
By the way, did you pass the $user_img to the view from controller ?
Excellent answer as it is a multi-fault protection
src="{{ asset('uploads/users/'.$user_img")}}"
and can use
{{asset('uploads/users').'/'.$user_img}}
other answer.... is scsesfual
try
src="{{ asset(\'uploads/users/$user_img\')}}"
Try Below Code:
<img src="{{URL::asset('assets/images/'.$user_img)}}"/>
Use Laravel Collective HTML Package
After installed this package you can use like this
{!! Html::style('your/css/file.css') !!}
{!! Html::script('your/js/file.js') !!}
{!! Html::image('your/image/file.jpg') !!}
Thank you all,
I can't understand why its not working for me.
But the $user_img retrieve the exact image name.
Then finally I tried like this way.
In controller
foreach ($user as $key) {
$this->data['user_img'] = \URL::to('').'/uploads/users/'.$key->avatar;
}
And in my View I just call the $user_img in the src
src="{{$user_img}}"
Edit
By using
Shrikant Bhardwaj code I gave it as
{{asset('uploads/users').'/'.$user_img}}
Thank you. :)

Images in Symfony2

I am new in Symfony 2 and i need your help. I want to output pictures uploaded by user to web/uploads/photos. But I dont know how I can access to that files, always get 404 error. I read this pages:
sf 1,
sf 2, and some articles on stackoverflow, but I realy havent answer. Please can somebody explain how I can do that?
Here is some code(maybe its be useful).
Get path in controller:
$pathToImages = $this->get('kernel')->getRootDir() . '/../web';
Try to get image in view:
<img src="{{ pathToImages }}/uploads/car_photos/{{ carPhotos.path }}">
you can use
<img src="{{ asset('uploads/car_photos/' ~ carPhotos.path) }}" />
if image folder is located under web folder
You can use the asset helper to generate the path.
The path helper goes directly to your web folder for example.
<img src="{{ asset('uploads/car_photos/' ~ carPhotos.path) }}" />

Categories