Laravel display image from path which is in database - laravel

So use is uploading a logo and it's path is stored in a database like this:
C:\xampp\htdocs\laravel\public\logo\1496912432.jpg
I am displaying the image like this:
<img class="images" id="image" src="{{$business->image}}" />
However I get this error:
Not allowed to load local resource: file:///C:/xampp/htdocs/laravel/public/logo/1496912432.jpg
How can this problem be solved?
//edit
Controller:
public function image(Request $request) {
if($request->hasFile('img'))
{
$image = Input::file('img');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('logo/' . $filename);
Image::make($image->getRealPath())->fit(303, 200)->save($path);
$file = $request->file('img');
$session = session()->get('key');
$update_image = Business::find($session);
$update_image->image = $path;
$update_image->save();
return ['url' => url('logo/' . $filename)];
}

Use Laravel file() to store files https://laravel.com/docs/5.4/requests#files
Store the $path to your db
$path = $request->photo->store('logo');
the $request->photo is depending on your input file attribute name. In your case, it should be $request->img.
the above code will create a folder (if not exist), namely "logo" and store to that folder with random string file name.
Also check your configuration for file, located at /config/filesystem.php. Default is set to public
Use asset function to get the full path from public folder
<img class="images" id="image" src="{{ asset($business->image }}" />

You can do in two ways
Best way is update url path when image saving save url path to db
$path = $request->photo->store('logo'); // in 5.4
The other way if you can't changes db url you can do some hack like this
$file = explode('/public/', $business->image);
echo asset($file[1]);

You want to store all files inside the web root. Because of cross-domain security, you cannot access the file:// domain/protocol from a http protcol. By using Laravel to store and retrieve, it will come from the same host.

Related

How to store image as URL in Laravel 8.*

In my application I add photos that are then scaled, I call them to my views using {{asset()}}
Everything works fine, but for my mobile app I need to send to API URL of image instead of just image path called from db.
That's how I save images now:
$image = $request->photo_patch;
$filename = time() . '.' . $image->extension();
$event->photo_patch = $filename;
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1200, 630);
$image_resize->save('storage/images/' . ($filename));
$event->save();
Example of saved image name:
1630531230.jpg
That's how I get image on view:
<img src="{{ asset('storage/images/' . $eventView->photo_patch) }}">
What I tried:
$url = Storage::url($filename);
$event->photo_patch = $url;
After this file name looks like this
"/storage/1631043493.png"
But that isnt really URL
What can I do to save photo path like this:
"localhost/storage/images/1631043493.png"
Edit:
# Фарид Ахмедов suggested to call whole URL in API.
How can I do that then?
This is what I tried:
Route::get('/events/{id}', function($id){
return [
Event::findOrFail($id),
'image' => asset('storage/images/'.$this->photo_patch)
];
});
That's it.
url(Storage::url($filename));
But I think, you don't need to save the whole path. You can convert it to full URL before sending response.

The picture is not displaying in my page (403 error)

I put my website online, and the images are not showing. So I went to the site and went to the developer console.
The following error is displayed:
Failed to load resource: the server responded with a status of 403 ()
However, when I tried my site in localhost, everything was showing normally.
When I copy/past the img src in my code (using the developer tool) into the my internet URL, I've an
forbidden You don't have permission to access this resource.
So do I need to edit something somewhere to display the images? Do I have to modify any rights?
NB: I'm using OVH.
EDIT: There is the code I use to store the picture and the one I use to display the picture :
protected function saveFiles($idArticle, $file)
{
$article = Article::where('id_article',$idArticle)->firstOrFail();
$path = 'public/documents/articles/' . $idArticle . '/' . $article->titre;
$fileName = $idArticle.'-'.$article->titre.'.'.$file->getClientOriginalExtension();
$file->storeAs($path, $fileName);
}
In another function I'm doing that :
$data = array();
$path = 'public/documents/articles/';
$files = Storage::files($path.$editer_article->id_article . '/' . $editer_article->titre);
$data[$editer_article->id_article] = url(Storage::url($files[0]));
return view('test', ['data' => $data]);
This is inside my controller. Inside my view I'm doing :
<img src="{{ $data[$article_carousel->id_article] }}" class="d-block w-100" alt="{{$article_carousel->titre}}"/>
As I said, this is working in local, but not online.

Image src not get right pat in Laravel

I tried to display an image in view page, but the src attribute is incorrect.
C:\xampp\htdocs\new\public\uplode/Screenshot_5.png
but src is:
http://localhost/new/C:\xampp\htdocs\new\public\uplode/Screenshot_5.png
function is:`public function bpl(){
$ob=banner::all();
return view('admin.page.bpl',['var'=>$ob]);
}
View file is:<img src="{{asset($ob->bannerimage)}}">.
How do I get the right path in view file.
Try this on your view file.Use this link i belive it will resolve your issue.
<img src="{{asset('uplode/Screenshot_5.png')}}"/>
$image = $request->file('image');
$filename = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads');
$image->move($destinationPath, $filename);
you should save only the file name and extension like (filename.png) on database. you have save desktop actual path which contains /\ both so image destination is not working properly.

Access images inside public folder in laravel

How can I access the images stored inside public/images folder without the use of Laravel's own functions like assets()?
I just want to get the direct URL of the images stored in images folder.
Eg:
localhost/webapp/images/stackoverflow.png
When requesting the link i should get the direct image.
If you are inside a blade template
{{ URL::to('/') }}/images/stackoverflow.png
Just put your Images in Public Directory (public/...folder or direct images).
Public directory is by default rendered by laravel application.
Let's suppose I stored images in public/images/myimage.jpg.
Then in your HTML view, page like: (image.blade.php)
<img src="{{url('/images/myimage.jpg')}}" alt="Image"/>
I have created an Asset helper of my own.
First I defined the asset types and path in app/config/assets.php:
return array(
/*
|--------------------------------------------------------------------------
| Assets paths
|--------------------------------------------------------------------------
|
| Location of all application assets, relative to the public folder,
| may be used together with absolute paths or with URLs.
|
*/
'images' => '/storage/images',
'css' => '/assets/css',
'img' => '/assets/img',
'js' => '/assets/js'
);
Then the actual Asset class:
class Asset
{
private static function getUrl($type, $file)
{
return URL::to(Config::get('assets.' . $type) . '/' . $file);
}
public static function css($file)
{
return self::getUrl('css', $file);
}
public static function img($file)
{
return self::getUrl('img', $file);
}
public static function js($file)
{
return self::getUrl('js', $file);
}
}
So in my view I can do this to show an image:
{{ HTML::image(Asset::img('logo.png'), "My logo")) }}
Or like this to implement a Java script:
{{ HTML::script(Asset::js('my_script.js')) }}
when you want to access images which are in public/images folder and if you want to access it without using laravel functions,
use as follows:
<img src={{url('/images/photo.type')}} width="" height="" alt=""/>
This works fine.
You simply need to use the asset helper function in Laravel. (The url helper can also be used in this manner)
<img src="{{ asset('images/arrow.gif') }}" />
For the absolute path, you use public_path instead.
<p>Absolute path: {{ public_path('images/arrow.gif') }}</p>
If you are providing the URL to a public image from a controller (backend) you can use asset as well, or secure_asset if you want HTTPS. eg:
$url = asset('images/arrow.gif'); # http://example.com/assets/images/arrow.gif
$secure_url = secure_asset('images/arrow.gif'); # https://example.com/assets/images/arrow.gif
return $secure_url;
Lastly, if you want to go directly to the image on a given route you can redirect to it:
return \Redirect::to($secure_url);
More Laravel helper functions can be found here
In my case it worked perfectly
<img style="border-radius: 50%;height: 50px;width: 80px;" src="<?php echo asset("storage/TeacherImages/{$teacher->profilePic}")?>">
this is used to display image from folder i hope this will help someone looking for this type of code
Crete file .htaccess in root && add
...
RewriteCond %{REQUEST_URI} !(.css|.js|.png|.jpg|.gif|robots.txt|.ttf)$ [NC]
...

Laravel file upload path and view rendering

I successfully upload a file and store its path with the following snippet:
/*Image Handling*/
$file = Input::file('profilePicture');
$destinationPath = public_path().'/images/';
$filename = $file->getClientOriginalName();
Input::file('profilePicture')->move($destinationPath, $filename);
//Profile image
$profileImg = $destinationPath.$filename;
then I store profileImg in database. This is what it looks like:
/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg
Now I want to show this picture in one of the views. So I did this:
<a class="th" href="{{URL::to('/')}}">{{ HTML::image($details->profileImg, "work", array('style' => 'width:100px;height:100px;')) }}</a>
and this is how it is rendered:
<a class="th" href="http://localhost:8888/devproject/index.php"><img src="http://localhost:8888/devproject/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg" style="width:100px;height:100px;" alt="work"></a>
This of course doesn't work because the path is incorrect but it is how it was stored. I need the path to the image to be :
/public/images/picture.jpeg
instead of this:
/Applications/MAMP/htdocs/devproject/public/images/picture.jpeg
as this would fit in the url and show the picture. Any advices on how to achieve this will be appreciated.
Don't save the whole path to the model, save just the filename:
$profileImg = $filename;
Then, instead of using $details->profileImg by itself, use:
asset('images/' . $details->profileImg)
i.e.:
{{ HTML::image('images/' . $details->profileImg, "work", array('style' => 'width:100px;height:100px;')) }}

Resources