I am making a pdf, for which I use the library: barryvdh/laravel-dompdf, in the pdf I try to print an image, with which when I open the recently downloaded file, all the data is printed except the image, I clarify this image if it is displayed correctly in my show.blade view, I cannot find a solution for this problem.
Below I detail how I am proceeding:
Controller: (fragment of store method and exportPdf method)
public function store(SaveDocumentRequest $request)
{
if ($request->hasFile('photo')) {
$file = $request->file('photo');
$name = time().$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
}else{
$name = null;
}
$document->photo = $name;
public function exportPdf()
{
$movie = Movies::with('document.creator', 'actors', 'generate_movie', 'document.adequacy', 'document.lenguage')->first();
$pdf = PDF::loadView('admin.movies.show', compact('movie'));
return $pdf->download('cine.pdf');
}
My show.blade file:
<div class="col-md-6">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Imagen de Portada </h3>
</div>
<div class="box-body box-profile">
<img class="img-responsive" src="{{ asset('/images/'.$movie->document->photo) }}" alt="{{ $movie->document->title }}" >
</div>
</div>
</div>
This way I am accessing the image that if it is displayed correctly in my show.blade file, but it is not displayed in the downloaded pdf.
I clarify I do not receive any type of error and the image is stored in the public folder of the project.
Could you guide me to find the solution?
Its simple just use this I have used same package and added image on pdf like this.
<img src="{{asset('foldername/path_to_image')}}">
Related
i have a project that it has some posts on it
i have made admin panel to create new posts
since i uploaded my project on cpanel host i can create a post but image wont shown in my post but before that on my localhost i can craete a post and store image in my path
i checked my public path in my host and saw that there isn,t any new image related to my post that i have created with my admin panel on my host just my image name has saved in my database
this is my blog blade file
<section class="w-80 mx-auto my-5">
<div class="container">
<div class="row">
#foreach($posts as $post)
<div class="col-md-4 translate-box relative mb-4">
<div class="blog-box-height">
<img src="{{asset('image/'.$post->header_image)}}" alt="{{$post->title}}" class="img-fluid">
<div class="py-3 px-4">
<a class="text-xmedium fw-bold text-decoration-none text-black" href="{{route('blog.single',['title'=>$post->title])}}">
{{str_replace('_',' ',$post->title)}}
</a>
<p class="text-muted-1 mt-2 text-small">
{!! strip_tags(mb_substr($post->content,0,270,'utf-8')) !!} ...
</p>
<div class="box-time absolute">
<p class="text-muted-1"><i class="far fa-clock"></i> {{$post->time}} دقیقه</p>
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
</section>
this is my postcontroller codes
public function storeImage(Request $request)
{
$image_name = null;
if ($request->file('image')) {
$image = $request->file('image')->getClientOriginalName();
$image_name = time() . $image;
$folder = public_path('/image');
$path = $folder . '/' . $image_name;
if (is_dir($folder)) {
$img = Image::make($request->file('image'))->resize(500, 438);
$img->save($path);
} else {
File::makeDirectory($folder, 0755, true);
$img = Image::make($request->file('image'))->resize(500, 438);
$img->save($path);
}
}
return $image_name;
}
this code works great in my localhost server but when i uploaded it on host i think image intervation wont work
You have to set the symlink to connect the stroge folder with the public folder.
Try running php artisan storage:link on the webserver.
Also in some hostings this command is banned. In that case you have to create the symlink manually using forexample midnight commander.
Try with 777 permission for make directory.
File::makeDirectory($folder, 0777, true);
I'm trying to upload images to my website but it does not show up
this is the HTML
<div class="col-md-10">
<div class="post-image">
<canvas style="display: none" id="canvas-preview" style="overflow:hidden;"></canvas>
<img id="img-preview" src="{{ url('backend/'). ($posts->preview_image?'/'. $posts->preview_image:'/post_preview/post-image.png')}}"> </div>
<div class="m-b-10">
<a id="upload-submit" class="btn btn-warning btn-block btn-sm" onclick="file.click();"><i class="fa fa-upload"></i> Upload Picture </a>
{{ Form::file('preview_image', ['class'=> 'btn btn-warning btn-block btn-sm','id'=>'file','style'=>'visibility:hidden']) }}
</div>
</div>
the controller:
public function create()
{ $this->authorize('create', Post::class);
return \View::make('posts.edit')
->with('posts', new \App\Models\Post);
}
and the model:
class Post extends Model
{
const DEFAULT_PREVIEW_IMAGE = "post_preview/post-image.png";
/**
* get preview image
* #return mixed|string
*/
public function getPreviewImage()
{
return $this->show_preview_image?$this->preview_image:static::DEFAULT_PREVIEW_IMAGE;
//return $this->preview_image;
}
}
I tried to add multiple extensions like jpg, jpeg or gif in the model but the problem persists
For laravel images should be stored in storage folder or you can create a folder inside storage folder. You also need to create symbolic link
php artisan storage:link
Here is how to upload the file to the storage folder
if ($request->hasFile('file_name')) {
$file = $request->file('file_name');
$fileName = str_replace(' ', '-', $file->getClientOriginalName());
$uploadPath = storage_path('app/public/folder_name');
$path = $request->file('file_name')->move($uploadPath, $fileName);
$uploadPathFile = $fileName;
}
Then you save the path to your DB.
For showing the image in the blade
<img src="/storage/folder_name/{{$data->image}}" alt="No image available" class="img-thumbnail">
Here $data->image is only the path
is your image stored in a public directory - try accessing the URL in your browser to see if the response is the expected image.
If not please provide more details how are you trying to retrieve the image (via binary response or stored as a public asset)?
I want to download a blade. I am using dompdf.
public function download($id)
{
$booking=Booking::where('id',$id)->first();
$pdf = PDF::loadView('pages.booking.bookingdetails',$booking);
return $pdf->download('bookingdetails.pdf');
}
bookingdetails.blade.php
<div class="container">
<div class="card">
<div class="card-header">
Invoice # {{$booking->id}}
<strong>01/01/01/2018</strong>
<span class="float-right"> <strong>Status:</strong> Pending</span>
</div>
</div>
</div>
So what I want to do is passing the $booking variable from the method and send to the view. Then download it as a pdf file.
But the error is:
"Undefined variable: booking
I'm shooting in the dark here, shouldn't it be:
$pdf = PDF::loadView('pages.booking.bookingdetails', ['booking' => $booking]);
You need to pass an array as the second argument to the view with the index name of booking so try this:
$pdf = PDF::loadView('pages.booking.bookingdetails',['booking' => $booking]);
I'm using the Image Intervention package in Laravel and I can't display my profile image. The problem is that the browser fails to load the resource
localhost:8000/uploads etc.
All files are saved in public folder :
public/uploads
My view:
<div class="row">
<div class="col-md-2 ">
<img src="uploads/{{ $user->avatar }}">
</div>
</div>
Controller:
class UserController extends Controller
{
public function profile()
{
return view ('user.profile', array('user'=>Auth::user()));
}
public function update(Request $request)
{
if($request->hasFile('avatar'))
{
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save(public_path('/uploads/avatar' . $filename));
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('user.profile',array('user'=>Auth::user()));
}
}
I'm using windows and i see these files saved in my folder public/uploads but i cant display it
Regards
When you save the image, add a / after "uploads/avatar":
save(public_path('/uploads/avatar/' . $filename));
That should solve the naming problem. Then in your HTML add "avatar/" after "uploads/", as you are saving to the "upload/avatar" folder.
Then your view would look like this:
<div class="row">
<div class="col-md-2 ">
<img src="uploads/avatar/{{ $user->avatar }}">
</div>
</div>
In your controller class change this line:
$user->avatar = $filename;
to this:
$user->avatar = 'avatar' . $filename;
I am working on the laravel 5.0. I am getting an error: "trying to get property of non-object".I want to get an image on the desired page by clicking on that particular image. There are so many images on the page, i just want after clicking on any image, it will be appear on the desired page.
here is my code:
My Routes:
Route::get('/image/big/{id}' ,[
'uses'=>'GalleryController#bigimage',
'as'=>'bigimage'
]);
My controller:
public function bigimage($id){
$images=image::findorFail($id);
return view('bigimage' , ['images'=>$images]);
}
My view from where i am calling the required Route:
#foreach($gallery->images as $image)
<li>
<a href="{{ URL('/image/big/'.$image->id) }}">
<img id="jumboimage2" src="{{ url($image->file_path) }}"></a>
</li>
<li>
<a href="{{ URL('/image/delete/'.$image->id) }}" id="margin">
<span id="margin" class="glyphicon glyphicon-remove-sign"></span></a>
</li>
#endforeach
My view where i want to get the required image:
<div class="row">
<section class="col-md-3">
#foreach($images as $image)
{{$image->id}}
#endforeach
</section>
</div>
try this one out
$image = image::findOrFail($id);
return view('bigimage' , ['image' => $image])
$images=image::findorFail($id); should be $images=image::findOrFail($id);.
findOrFail() returns a single model, i.e. one image, so I would have:
$image = image::findOrFail($id);
return view('bigimage' , ['image' => $image]);
You don't show where image:: comes from, however usually this is from a facade so is Image:: with a capital.
The view would then not have the #foreach loop as you'd only have a single image.
<div class="row">
<section class="col-md-3">
{{$image->id}}
</section>
</div>
You also need to elaborate on this to get from the id to an image but I'm assuming that's just not shown.
If you were able to give some detail about which line is causing the error this would make it easier to diagnose, as indicated by Maraboc there could be an issue with $gallery as well.