Why image is not loading in Twig Template? - php

I want to load every user image in my twig with the following line:
<img src="/web/images/users/{{ topic.user.image }}" class="w3-circle" width="100" height="100">
And am sure the path is correct. The user avatar is in web/images/users/ and selected image. Where can be the problem then ?
That's displayed:
And That's how i try to upload image file in registration controller:
/** #var UploadedFile $file */
$file = $user->getImageForm();
$filename = md5($user);
$file->move(
$this->get('kernel')->getRootDir() . '/../web/images/users/',
$filename
);
$user->setImage($filename);

your web-server probably already point to the web dir, so try simple:
<img src="images/users/{{ topic.user.image }}" class="w3-circle" width="100" height="100">
Hope this help
NB: You can try debugging dump the value of topic.user.image and check in which path the image exists. Then try to locate with your browser and see what happen.

Related

File download not working in Laravel 5?

I have stored a file in /public/storage/attachment and the file has a hashname which is stored in my database as well in the attachments table filehashname column
public/attachment/N52lbwmzubAcgUfHYhCC4A6NFBniIVmK.pdf
In the view I'm displaying the file as a link so that when the user clicks the link the file gets downloaded.
<a href="{{ URL::to( '/download/'.$post->attachments[0]->filehashname) }}">
{{ $post->attachments[0]->filename }}
</a>
web.php
Route::get('/download/{filehashname}', 'PostController#downloadAttachment');
PostController
public function downloadAttachment($filehashname)
{
$file= public_path('/storage/attachment/'.$filehashname);
return response()->download($file, 'demo.pdf');
}
But this does not download the file it opens the URL http://localhost:8000/download/public/attachment/N52lbwmzubAcgUfHYhCC4A6NFBniIVmKfctqMNOq.pdf with Page Not Found
If I hardcode the filename and click the link then it downloads but dynamically passing the name of the file in url gives Page Not Found error.
How to solve this?
If your file is stored in public directory then try public_path() and if your file is saved in storage directory then try storage_path()
$destination = storage_path('storage/attachment/'); // if your storage folder contains only attachement folder then like this :- storage_path('attachment/');
or
$destination = public_path('storage/attachment/');
$pathToFile = $destination.$filehashname;
return response()->download($pathToFile,'demo.pdf');
Hope it helps :)
You just need to add downlaod option in anchor tag
<a href="{{ URL::to( '/download/'.$post->attachments[0]->filehashname) }}" download >
{{ $post->attachments[0]->filename }}
</a>
This is being answered here
Try this
$myFile = public_path($filehashname); # place file in public/ and check this;
# If works add this public_path("storage/attachment/".$filehashname);
$headers = ['Content-Type: application/pdf'];
$newName = "demo.pdf";
return response()->download($myFile, $newName, $headers);
public_path()

The blade only load the default picture?

I have seen this answer [
[Determining If a File Exists in Laravel 5 and tried to implement the same method in my project.
I'm using Imageintervention and Laravel 5.4. I have a PropertyController with the following validation code:
//? file upload
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time() .'.' .$image->getClientOriginalExtension();
$location = public_path('/imgs/properties/' . $filename);
Image::make($image)->resize(400, 300)->save($location);
$property->image = $filename;
}
//store in the DB
In my show blade i have the following syntax:
<div class="thumbnail">
#if(file_exists(public_path().'../imgs/properties/. $filename'))
<img src="../imgs/properties/{{ $property->image }})" alt="">
#else
<img src="../dist/img/villa.jpg" alt="">
#endif
I put the following code in my Property Model:
public function image()
{
if(file_exists( public_path() . '/imgs/properties/' . $filename)) {
return '/imgs/properties/' . $filename;
} else {
return '../dist/img/villa.jpg';
}
If I register a property with an image I only get the default image and not the newly registered image. I have checked in my properties folder and the image newly registered image is there waiting i fix the problem. Do you have any idea about what went wrong?
Thank you in advance!

Laravel 5.3 storage cant find uploaded file

I am using laravel Filesystem to store images, the image will be uploaded inside the default storage, example:
/home/user/Projects/mywebapp/storage/app/images/posts/1/e392e17926559e7cc4e7934f.png
I didnt change the config/filesystem.php and here is my controller method to upload an image,
public function storeImage( Request $request ) {
$image = $request->file( 'image' )->store( 'images/posts/'.Auth::user()->id );
echo Storage::url( $image ); // <- print the url of the uploaded image
}
This would print the image url like this,
http://localhost:8000/storage/images/posts/1/e392e17c6a8d7926559e8e7cc4e7934f.png
But the image file is not accessable from the browser!
I dont want to use php to dynamically fetch the images, i just wanted to make them publicly accessable over http. How to display the image?
you just have to make a change in config\filesystems.php form
'default' => local,
to 'default' => public,
then in console run php artisan storage:link
then try the above code and it will work
For me it worked when I added "../" to the url of the file:
#foreach($arquivos as $i)
<li class="list-group-item"> <img src="{{ asset('img/pdf.png') }}" /> {{ $i -> nome }} </li>
#endforeach
And I am saving the file like this:
$ext = $request->arquivo->extension();
$path = $request->arquivo->storeAs('/relatorios', $nameoffile. "." . $ext);

How to display image from aws s3 in blade laravel 5.2

I have created a method for getting the data(image file) from aws S3 like this :
public static function getImage ($imagePath)
{
if(Storage::exists($imagePath))
{
return Storage::disk('s3')->get($imagePath);
}else
{
return 'No Image';
}
}
I'm sure that those image is exist on the aws, so there is no problem with that.
And then I use above method as the src in my blade view like this :
{!!Html::image(getImage($myPath),'logo',['width'=>60,'height'=>55])!!}
$myPath here already point to the specific file on aws for example : bucket/file.jgp. But when I run my webpage, this Html::Image gives a view like this :
What's going on here ? Why Html::image can't render my image file ? :(
Works on Laravel 6.x
<img src="{{Storage::disk('s3')->url($imagePath)}}">
You can use Storage::url($imagePath). Please see Filesystem / Cloud storage on laravel docs
{!!Html::image(<Your S3 bucket URL>.$myPath),'logo',['width'=>60,'height'=>55])!!}
You can save the S3 URL in a config file and use the config value instead of writing actual value everytime.
I had the same issue. This code snippet solved it
<img src="{!! url(<path to your image>) !!}" />
First, you need to give it public promotion in the controller
$path = Storage::disk('s3')->put('profileImages/', $request->file('profile_image'), 'public');
//save image name in database
$user->profile_image = basename($path);
In blade file
<img src="{{Storage::disk('s3')->url('profileImages/' . $user->profile_image)}}" />
You can add an accessor like follow to get Image URL
It returns URL to the image and you can access it by $profile->image
public function getImageAttribute($value)
{
if ($value) {
return Storage::disk('s3')->url($value);
}
return "https://source.unsplash.com/96x96/daily";
}
I assume you save image as follow
$path = $request->file('image')->store('images', 's3');
Storage::disk('s3')->setVisibility($path, 'public');
$profile->update([
'image' => $path
]);
You can check this commit for more detail
https://github.com/itxshakil/ediary/commit/23cb4b1cb7b8a4eac68f534e8c5b6897abc6421a
I have read every solution and no one has pointed it directly. So I thought to write my answers. For this problem I just required to add this simple line and boom, it starts working. But I got access denied alert you should check that problem too.
<img class="img-responsive" src="{!! Storage::disk('s3')->url('thumbs/' . $business->image->path) !!}" alt="thumb">
This works
Storage::disk('s3')->url($imagePath)
and also from your Minio Browser.
You have to Add a policy to Read and Write
I think that private visibility of images is an important security feature.
I found a solution :-)
Method in Controller:
/**
* #param string $name : image name on bucket
* ( stored if you want into database )
*/
public function show( $name ) {
$path = config('filesystems.disks.s3.base_directory') . '/' . $name;
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = Storage::get( $path );
$base64 = 'data:image/' . $type . ';base64,' . base64_encode( $data );
return view( 'show', [ 'src' => $base64 ]);
}
The view:
...
<div class="panel panel-primary">
<div class="panel-heading"><h2>Laravel File Retrive with Amazon S3 </h2></div>
<div class="panel-body">
**<img src="{{ $src }}"/>**
</div>
</div>
...

how to display an image that is outside of public_html in yii2?

My problem is that I try to show an image located outside the public_html folder of my project, for this, in the view I call a controller method in Yii, running the following command:
public function actionImagenreadfile2($file) {
if(file_exists($file)){
header('Content-Type: '. FileHelper::getMimeType($file));
ob_clean();
readfile($file);
}
}
The way I call this controller from the view is the following:
<img src="<?php Yii::$app->runAction('ficha/imagenreadfile2', ['file' => $imagen]); ?>">
Where $imagen is the image path, but at the time when the display image is displayed as text/plan.
You should fix your view :
<img src="<?= Url::to(['ficha/imagenreadfile2', 'file' => $imagen]); ?>">
You could also use yii\web\Response::sendFile() in your controller :
return \Yii::$app->response->sendFile($file);
Read more about sending file with Yii2.
And the most important, you should check $file since your action is completely unsecured...

Categories