Laravel 5.1 Snappy pdf image not rendering in pdf file - laravel

I am using barryvdh/laravel-snappy to generate pdf file. I have two image files 1. yourlogohere.png is in public/image/ folder and 2. logo2.png is in folder other than public i.e. storage/app/logo and to get this file I defined a route (www.example.org/logo/logo.png) and use following code to access it.
public function logo($filename)
{
$file = Storage::disk('local_logo')->get($filename);
$mime = 'image/png';
return (new Response($file, 200))->header('Content-Type', $mime);
}
Problem:
When I use following code to generate pdf from the html containing the first file, pdf contains the yourlogohere.png image
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/images/yourlogohere.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
But when I do exact same thing for the second file, pdf does not render the image.(When I open the link http://www.example.org/logo/logo2.png in browser I get the image). What am I missing?
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/logo/logo2.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
Thanks,
K

You can also do:
<img src="data:image/jpeg;base64,
{{ base64_encode(#file_get_contents(url('your.image.url'))) }}">

I think I got the what the problem is, the route to access the image is via auth, even when user is logged in while accessing the snappy, the wkhtmltopdf exe runs in a shell that is totally different session. Now the right fix would be to be embed the image in the html that is sent to snappy instead of the link, Which I am not sure how I will do? Any suggestions welcome there.
Update:
I as able to convert the image to data:image/png;base64, and embed it in html.
$html = view('mytemplate.default', compact('variable1', 'variable2'))->render();
/*Convert logo image to base64 before pdf conversion*/
//search for <img src="http://example.org/mytemplate/logo/logo1.png">" and replace the src with data:image/png;base64,
$search = '/(<img\s+src=["\'])([^"\']+)(\/mytemplate\/logo\/)(.*)(\.)(.*?)(["\']\s+[^>]+>)/';
$html = preg_replace_callback($search, function ($matches) use ($invoicedetail) {
$filename = $matches[4] . $matches[5] . $matches[6];
$file = Storage::disk('local_logo')->get('yourlogohere.png');
$mime = "image/png";
$mytemplate = MyTemplate::where('logo_filename', '=', $filename)->first();
if (!empty($mytemplate)) {
$file = Storage::disk('local_logo')->get($mytemplate->logo_filename);
$mime = $mytemplate->logo_mime;
}
$base64 = 'data:' . $mime . ';base64,' . base64_encode($file);
return $matches[1] . $base64 . $matches[7];
}, $html);
$pdf_filename = 'template' . $mytemlpate->id . '.pdf';
$path = storage_path('app' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $filename);
$snappy = App::make('snappy.pdf');

Related

Saving Images in two size in Laravel Livewire

I have a form where I upload images using Laravel Livewire.
In my class I can save the files in original size using:
$filenames = collect($this->photos)->map->store('posts');
The issue that I faced is that I don't know how to save the same images (with same name as above) in another folder (thumbnail folder) in another size. For instance, width 200px.
I have used this with Laravel 8 and it works. I can't see what you have before this code but you can iterate through your photos and save 2 images, one in the parent folder and one resized in a "thumbnail" folder;
$ext = $image->getClientOriginalExtension();
$date = new Carbon;
$folder = "photos";
// Save original file
$orig_filename = $date->format('YmdHisv') . '.' . $ext;
$img_url = $image->storeAs('public/' . $folder, $orig_filename);
// Create and store thumbnail
$thumbnail = $image->storeAs('public/' . $folder . '/thumbnails', $orig_filename);
$thumb_path = Storage::path('public/' . $folder . '/thumbnails/' . $orig_filename);
$thumb_img = Image::make($thumb_path)->resize(150, 93, function ($constraint) {
$constraint->aspectRatio();
});
$thumb_img->save($path);
This is the package Intervention\Image and here is a great tutorial on that.

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.

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.

Laravel Nova: how to display image from binary string?

I save PNG's binary content in database.
I want display this PNG's on page without temporary save file on disk.
I think need generate img tag like <img src="data:image/png;base64,......
But I do not understand how it is better to implement it and what type of field to take as a basis.
Image::make('Image')->displayUsing(function($item) {
$mime_type = 'image/png';
return 'data: ' . $mime_type . ';base64,' . base64_encode($item);
}),
But Laravel Nova generated:
<img src="http://172.18.0.3/storage/data: image/png;base64,......" class="rounded-full w-8 h-8" style="object-fit: cover;">
Added unnecessary http://172.18.0.3/storage/and rounded class.
How to prevent it adding?
Work code for Laravel Nova 2.0.1:
Image::make('QRCode', 'qrcode')->thumbnail(function($value, $disk) {
return 'data: image/png;base64,' . $value;
})->preview(function($value, $disk) {
return 'data: image/png;base64,' . $value;
})->displayUsing(function($value) {
return base64_encode($value);})
Also need remove rounded-full from field.thumbnailUrl?t("img",{staticClass:"rounded-full w-8 h-8", in file public\vendor\nova\app.js
Override thumbnail & preview for image url
Try below code snippet
Image::make('Image')->thumbnail(function($value, $disk) {
return 'data: image/png;base64,' . base64_encode($value);
})->preview(function($value, $disk) {
return 'data: image/png;base64,' . base64_encode($value);
}),

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

Categories

Resources