I want to display images in my views in laravel - laravel

I am trying to create a blogging application with Laravel and so I got this in my view
<img src="{{$post->image}}" />
the image is not showing up but the post title shows up.
I tried:
php artisan storage:link
and its return:
The "public/storage" directory already exists.
but yet still no image displays

Based on your comment, you upload the image to a private storage folder so it's will never show. You should upload to the public folder using code like this:
$image = $request->image->store('posts','public');
then if you want to show it just use <img src="{{url("storage/".$post->image)}}" />

Related

How do I display and image from database to laravel blade?

I have followed the correctly marked answer in this question to save images in my app. I am having trouble displaying the images to blade file. I am using <img src="{{asset('/ProfileImages'.$p->profile_image)}}" alt="..."> ProfileImages is the storage folder with structure storage/app/ProfileImages. How do I display the images as the above line doesnot display?
Laravel storage filesystem is so unique. When you doing any upload it will upload in the storage/app/public directory. To make it accessible in public directory, things need to do is create symlink by run command:
php artisan storage:link
Or
<img src="{{ Storage::url($p->profile_image) }} " alt=""/>
StorageDocumentation
use Storage::url()
<img src="{{ Storage::url($p->profile_image) }}" alt="...">
ref link https://laravel.com/docs/8.x/filesystem#file-urls
By default when using {{asset()}} this will look inside your Public directory you should either consider storing photos inside your Public directory
Or take a look at the documentation about Laravel File System, here they explain how you can use storage directory and link it with your public directory
Laravel File System
Aslo you can use {{Storage::url()}} instead

Can't load images in laravel blade files

I've downloaded a template from here.
I want to design pages like that in laravel blade pages, I put css and js files in public folder and link blade files to them, and it works.
But I can't handle images, there isn't a folder for images in this template and I don't know how to load them in my blade pages.
you can upload images on below path
storage/app/public/images
//this way you can use in your blade file
<img src = "{{ asset('/images/YOUR_IMAGE.png') }}" />
but you need to run one command
php artisan storage:link
it will create a symlink from public/storage to storage/app/public
please insert as below path and make link like that.
try this.
<div class="hidden-xs" style="width:200px; height:auto; margin-left:-10px;">
<img src="{{ asset('uploads/logos/myLogo.png') }}">
</div>
If not working... then check your App Url in /.env and restart your app server.
APP_URL=http://localhost
Happy coding~!:)

Error displaying images in subdomain using laravel 5.3

I have successfully deployed my laravel project to shared hosting (cPanel) and all seems to be working apart from the images. The images stored directly on the server e.g. logo.png don't show up and yet css files in the same public directory show up.
When I try to view the image directly via its URL, I still can't access it and gives me an error of "The image "http://..." cannot be displayed because it contains errors.
Could you please advise? Thanks in advance
Updated:
I have tried several alternatives which work locally but not when I upload.
< img src="/img/logo.png" width="100" />
< img src="{{ asset('/img/logo.png') }}" width="100" />
None of the above works and even when I try to access it via http://sub.domain.com/img/logo.png . It gives the error above
It turns out that the problem wasn't with the RewriteRule but rather the ftp client uploading the images with ASCII format. Laravel doesn't seem to pick those images so I re-ftped them with binary format and everything is working fine.
I hope this can help someone else.

Can not display the image which its path comes from database

I have a form which successfully upload a picture to a folder and save its full path to database.But when I want to display the image nothing is shown.
This is one of the uploaded image's path in database:
/home/webuser/public_html/traincms/upload/koala.jpg
this image saved in a folder of server.what should I do to display the images?
Thanks for your help.
You should use HTTP path of image, if you are using codeigniter then try this way
<?php echo img("traincms/upload/koala.jpg"); ?>
OR
<img src="<?php echo base_url("traincms/upload/koala.jpg")?>" />
Hope your code will store in public_html directory
Finally I found the solution.My upload path was
$path=dirname($_SERVER['SCRIPT_FILENAME']).'/upload/';
I saved this path to database.When I want to display the image on browser it did not display it with this path.I uploaded the image with above path but I saved this path to database:
$image_path=base_url().'/upload/'.$_FILES['pic']['name'];
now I can upload and display the images successfully.

Image display codeigniter fails

I can upload image to the directory but can not disply it on the page.
Tried using static html code and codeigniter img() function, both are not working.
Here is my code:
<image src="./applications/views/uploads/image.jpeg" />
and
$this->output->set_header("Content-Type: image/jpeg");
$data['image'] = $this->load->file('../uploads/taurus.jpeg');
the first code shows me broken image the second one shows Unable to load the requested file: taurus.jpeg
<image src="./applications/views/uploads/image.jpeg" />
By default, CodeIgniter denies access to the application folder which is why I suspect this isn't working.
$this->output->set_header("Content-Type: image/jpeg");
$data['image'] = $this->load->file('../uploads/taurus.jpeg');
I suspect this isn't working because your path is incorrect. The path is relative to the index.php file in the root of your application, not the file this code is in. Secondly, $this->load->file() sends to file contents to the browser so this wouldn't work anyway.
A solution would be to move your uploads folder to a web accessible location. For example, place the uploads folder into the root of your application - the same folder as your index.php file. Then you can display your image like this:
<image src="/uploads/image.jpeg" />
Just get rid of the dot (.) in the URL so your src url reads: "/applications/views/uploads/image.jpeg"

Resources