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]
...
Related
I am stuck. how to display the image stored in /storage/app/public directory of laravel 7.14 ?
I already tried the ones I found in stack overflow, or even the ones i found by googling.
I tried this solution
//Controller function
public function renderPic($filename) {
$path = '/storage/app/public/uploads/'.$filename;
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file,200);
$response->header("Content-Type",$type);
return $response;
}
web route
Route::get('user/renderPic/{filename}', 'User#renderPic')->name('user.renderPic');
blade view file
<img id="displayPic" src="{{ route('user.renderPic',$user->filename) }}">
it doesn't show up
You don't need to define a route and controller method to render an image.
you simply open a terminal in the root directory for your laravel app and run
php artisan storage:link
this will make a symlink for the storage/app/public directory to public/storage
the in your blade file you simply render the image like so:
<img id="displayPic" src="{{ asset('storage/your-image-name.ext') }}">
I use laravel 5.2 and tried to display a carousel only in index page but doesn't work.
I chose that the codes are not "spreaded" on the index page, they are stored in: public/carousel/carousel.php, too(not ...blade.php).
route.php:
Route::get('/', 'HomeController#index')
HomeController.php:
{
$cats = Category::all();
$carousel = public_path('carousel/carousel.php');
//$carousel = storage_path('public/carousel/carousel.php');
return view('layouts.app', compact('cats', 'carousel'));
}
layouts/app.blade.php:
{{-- #include('carousel/carousel');--}}
#if($carousel)
{{ $carousel }}
#endif
#yield('content')
Finally it displays only: C:\wamp\www\app_name\public\carousel/carousel.php.
Can you help me or point to another better way?
In your controller, you are passing to the view a variable called $carousel, which is the path to your file, as you defined here:
$carousel = public_path('carousel/carousel.php');
This is the reason why it only displays the string. You need to get the actual content of the file:
$carousel = file_get_content(public_path('carousel/carousel.php'));
A better and more laravel-ish way to do it would be to rename the file to carousel.blade.php, store it into the resources/views folder and simply include it from your main blade file (without the need of doing anything in the controller):
#include('carousel')
If you need to display the carousel on certain pages only, you can simply pass a variable $carousel = true on the pages that needs to display it:
$carousel = true;
return view('layouts.app',compact('carousel'));
And in your blade view, include the carousel file only when this variables is present and is true:
#includeWhen(isset($carousel) && $carousel, 'carousel')
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()'>
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.
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!