Image from storage folder is not showing in laravel - laravel

I have stored a image in my storage directory using spatie media package, the url to the image as returned by the getFullUrl() is http://127.0.0.1:8000/storage/34/conversions/download-thumb.jpg.
my APP_URL is APP_URL=http://127.0.0.1:8000
Then in in my blade file i'm using <img src="{{ $channel->image() }}" alt="">
And my function look like this
public function image() {
if($this->media->first())
{
return $this->media->first()->getFullUrl('thumb');
}
return null;
}
I'm not sure why my image is not showing on this url

Related

how to render to blade the image stored in storage/app/public directory of Laravel 7.14?

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') }}">

Laravel: Display image from public storage

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

NotFoundHttpException error in laravel

This is my view:
<img src="../public/{{ $axid->img }}">
and this is my route :
Route::get('/ax/{axid}', function($id) {
$axid = show::find($id);
return view('ax', compact('axid'));
Images are uploaded in public/uploads and show is model name. However, I have this error:
NotFoundHttpException
...what is the problem?
If we assume that your image resolving code is functioning properly and that you are returning the full url of the image you want to show, then change your view code to:
<img src="{{ asset($axid->img) }}">
Please see how you should be setting this out:
View before moving to this a href..: Get ID
Route: Route::get('/ax/{id}', 'AxidController#get');
Controller with me assuming Show is your model name:
public function get($id, Show $show)
{
$axid = $show->find($id);
return view('page', compact('axid'));
}
On the page of ax/{id}:
<img src="{{ $axid->image }}">

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">

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]
...

Resources