Laravel SnappyPDF image attachment - laravel

Im currently using barryvdh/laravel-snappy to convert my data to pdf format.
The code i'm using is :
public function generate_pdf($id){
$rfq = RFQ::find($id);
$data = array(
'rfq' => $rfq,
);
//return view('rfq.pdf')->with('rfq', $rfq);
$pdf = PDF::loadView('rfq.pdf',$data)->setPaper('a4')->setOption('margin-bottom', 0);
return $pdf->download($rfq->rfq_no.'-'.date_timestamp_get(date_create()).'.pdf');
}
And then, I have to add an image to the pdf file. So i have to add this line to the pdf.blade.php file.
<img src="{{ asset('img/logo.png') }}">
But then, the image isn't showing properly.
It's visible, but it's OPACITY is very low, around 0.2.
But when viewing the actual image file, and the one in the actual html template, everything's good.
I tried <img src="data:image/png;base64,{{ base64_encode(#file_get_contents(asset('img/logo.png'))) }}">
but still, it's the same.

Related

Image is not displaying after successfuly uploading it?

Image is not displaying in the nav bar after uploading it to the database. I think there is something wrong with the file storage path, but I don't know where I am making a mistake.
This is the image storage path
$image = $request->file('picture')
$path = $image->store('public/images');
$yourModel = new User();
$yourModel->name = $validateData['name'];
$yourModel->email = $validateData['email'];
$yourModel->password = $validateData['password'];
$yourModel->image = $path;
you store the image in the public iamges and in blade you say show the iamge form the storage/images that is incorrect you need to change the ima html to <img class="" src="{{ asset('images/' . Auth::user()->image) }}">

laravel picture gallery optimisation

I have photo gallery page, which shows all files from given directory.
I have following logic of handling pictures:
original pictures I keep inside storage/app/img
using console command and Intervention\Image library I create thumbs - in public/thumbs_md I keep small thumbs about 300x300 and in public/thumbs_lg I keep the same pictures, but 1024x768.
My idea about gallery page is that I find all files from given directory inside thumbs_md and show them as thumbs with the links to the files with the same names inside thumbs_lg.
And each picture should have description, which I take from $image->exif('ImageDescription') using Intervention\Image library.
And paginate results with 20 pictures on the page.
It works well with less than 200 pictures in the directory, but when it is more nginx gives me 504 Gateway Time-out.
I found that the reason in getting description from exif.
EXIF info stored only in original source pictures inside storage/app/img. During thumbs generating EXIF is deleted. I could't find a way to keep it. Maybe this is main reason why I get Gateway Time-out. I guess that I have 2 problems:
It takes longer time to get EXIF from big source file rather than from thumb, but I don't know how to copy exif data from source image to thumb.
I retrieve exif data from all files in directory at once, but I don't know how to split it into chunks, because I must to give all elements to paginator at once.
At the moment I just comment code where I read EXIF from all files and there is no problems, but I want to get those descriptions and I don't really want to keep image description in database. I like the fact that it is part of image.
Is there a way to optimise my code?
controller
public function item($slug)
{
$files = Storage::disk('storage')->allFiles('thumbs_md/'.$slug);
$links = Storage::disk('storage')->allFiles('thumbs_lg/'.$slug);
if ( empty($files) ){
abort(404);
}
// generate title of the page
try {
$image = Image::make( '/var/www/storage/app/img/categories/'. $slug.'.jpg');
}
catch(NotReadableException $e)
{
// If error
$title = 'Picture not found';
}
// If no error ...
$title = $image->exif('ImageDescription');
// generate description for picture
$ImageDescription = function($value) {
try { $image = Image::make(str_replace('thumbs_md', '/var/www/storage/app/img', $value)); }
catch(NotReadableException $e)
{
// If error
return 'Picture not found';
}
// If no error ...
return $image->exif('ImageDescription');
};
//$imgDesc = array_map($ImageDescription, $files);
for ($i=0; $i < count($files); $i++) {
$items[$i]['thumb'] = $files[$i];
$items[$i]['link'] = $links[$i];
//$items[$i]['description'] = $imgDesc[$i];
}
$items = Arr::arrayToObject($items);
$items = collect($items)->mypaginate($perPage = 20);
$items->title = $title;
$items->slug = $slug;
return view('pages.gallery.item', compact('items'));
}
view
<div class="gallery">
#foreach ($items as $item)
<a href="/storage/{{ $item->link }}" class="lightGallery">
<img {{-- alt="{{ $item->description }}" --}} src="/storage/{{ $item->thumb }}">
{{-- <p>{{ $item->description }}</p> --}}
</a>
#endforeach
</div>
I don't see why you would not put this stuff in a database, when you've clearly shown that retrieving EXIF data from a big file is slow. You don't want to be doing heavy work on each page load.
You are also limited to filtering data easily as long as it is not in a database table.
The code you have currently is badly written and not performant (you are retrieving the page title from an image???) so the way to go is to design a few database tables to handle this :). Once it is done, I bet you can shrink this controller to under 7 lines.
Intensive or time consuming processing should be avoided during request as far as possible for better user experience.
One way to optimise your workflow would be:
Create database table for categories
Create image_categories table with columns
name(string)
slug(string)(unique)
title(string): exif('ImageDescription') or any value
exif(text): whole exif data for the category image if required
Create database table for Images
Create the images table with columns
image_category(string): slug for the image category
src(string): path to the original image
src_thumb_md(string): path to the thumb_md (300 x 300) image
src_thumb_lg(string): path to the thumb_lg (1024 x 768) image
description(string): exif('ImageDescription')
exif(text): whole exif data for the image if required
Store the data when creating thumbnails for image(s)
Extend the console command which you are using to generate the thumbnails, to
store the paths and exif data to the images table for all images
store the title and exif data to the image_categories table when storing the image for category
Define Image and ImageCategory models with relations (optional)
class ImageCategory extends Model
{
public function images()
{
return $this->hasMany(ImageCategory::class, 'image_category');
}
}
class Image extends Model
{
public function imageCategory()
{
return $this->belongsTo(ImageCategory::class, 'image_category', 'slug');
}
}
Controller & View
If Models are defined
public function item($slug)
{
$category = ImageCategory::where('slug', $slug)->first();
$images = $category->images()->paginate(20);
return view('pages.gallery.item', compact('category', 'images'));
}
Or when models are not defined
public function item($slug)
{
$category = DB::tables('image_categories')->where('slug', $slug)->first();
$images = DB::table('images')->where('image_category', $slug)->paginate(20);
return view('pages.gallery.item', compact('category', 'images'));
}
<div class="gallery">
<h1>{{ $category->title }}</h1>
#foreach ($images as $image)
<a href="/storage/{{ $image->src_thumb_lg }}" class="lightGallery">
<img {{-- alt="{{ $image->description }}" --}} src="/storage/{{ $image->src_thumb_md }}">
{{-- <p>{{ $image->description }}</p> --}}
</a>
#endforeach
<!-- Pagination links -->
{{ $images->links() }}
</div>

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

How to remove link when used to create PDF in laravel dompdf?

I just make a simple html page and write content to it. I created a link when I click on that link the content of the page will converted into PDF file, I have implemented this using laravel dompdf -> https://github.com/barryvdh/laravel-dompdf . Now, My problem is that all things doing good, but the PDF that is generated after click will shows that link in the PDF file. How to remove that button from PDF file.
My code :
My controller : ItemController.php
class ItemController extends Controller
{
public function pdfview(Request $request)
{
if($request->has('download')){
$pdf = \PDF::loadView('pdfview');
return $pdf->download('pdfview.pdf');
//return $pdf->stream();
}
return view('pdfview');
}
}
My route: web.php
Route::get('/pdfview',array('as'=>'pdfview','uses'=>'ItemController#pdfview'));
My view : pdfview.blade.php
<body>
<div class="container">
<div class="rows"><br/>
Click to PDF
<h2 align="Center"> What is PDF ? </h2><br/><br/>
<h4> Portable Document Format (PDF) is a file format used to present and exchange documents reliably, independent of software, hardware, or operating system.</h4>
</div>
</div>
</body>
Help me, If you understand my problem. Thanks
Pass a variable to the view, i.e:
$pdf = \PDF::loadView('pdfview', array('pdf' => true));
...
return view('pdfview', array('pdf' => false));
and check the variable in the template:
#if(! $pdf )
Click to PDF
#endif

Show an image in a blade file after using Intervention Image (Laravel)

I want to show an image in a blade template that contains already some HTML code after doing some modifications (resize, insert...) to this image using the Intervention Image library.
Example :
public function image(){
return $img = Image::make('images/test.jpg')
->resize(800, 500)
->insert('some_url', 'bottom-right', 10, 10)
->text('foo', 100, 100, function($font) {
$font->file('fonts/font.ttf');
$font->size(100);
$font->color('#fdf6e3');})
->response();
}
This method is called using this route :
Route::get('/image', 'MyController#image');
The result is perfect :)
but I want to show this image in a blade template that contains already HTML code, and not just return the image using the route.
Thanks in advance.
Try just setting returned like this <img src="$img"> in your blade.

Resources