PDF Image thumbnail preview in laravel 5.1? - php

I have file module, where I want to list all files,with thumbnail preview.
as like follows,
I am storing files in storage folder,which is not accessible via http.
So How can I provide thumbnail preview for docs especially image and PDF.
Is there any package availabe in laravel 5.1?

The preview image of a PDF file is usually the first page of the PDF file.
You can use ImageMagick to obtain that first page of the PDF file.
<?php
$imagick = new imagick('sample.pdf[0]'); // 0 specifies the first page of the pdf
$imagick->setImageFormat('jpg'); // set the format of the output image
header('Content-Type: image/jpeg'); // set the header for the browser to understand
echo $imagick; // display the image
?>
You can also output (save the contents of the image in a file) and store it under a thumbnail folder with the PDF name as the file name. like ( sample.jpg )
As for a solution based on laravel, I don't think laravel has any package that can do the same

Though the question was posted quite a long ago but I'm answering because it might helps others. There is a package for generating image from pdf. Pdf to Image. You should have Imagick and Ghostscript installed

Related

PHP: Imagick converting multi-page pdf into single image issue

I am trying to convert a multi-page pdf into a single image with all pages in the pdf stacked. I am trying to use the Imagick() class. I found similar questions but none have helped the issue. Here is the code I am using.
$img = new Imagick();
$img->setResolution(300, 300);
$img->readImage(example.pdf);
$img->resetIterator();
$imga = $img->appendImages(true);
$imga->setImageFormat('jpg');
$imga->writeImage("pdfs.jpg");
Only the last page of the pdf is saved in the jpg file the other pages are not.
I am using ImageMagick-7.0.10, PHP 7.3.12 and I am on Windows 10.
You,have to add the index of the pdf page you want.
For the first page :
$img->readImage(example.pdf[0]);
I'm looking to generate animated gif with multipage pdf... not done yet !

Add remote images to PhpSpreadseet cell

There is a simple two-column table on a website: product name and product image. It's very easy to render in HTML.
The task would be creating an Xlsx file with these columns.
The images are not stored locally but all of them are remote images with full URL.
The export contains ~100-200 rows.
I tried to create a resource with imagecreatefromjpeg and adding it with MemoryDrawing but it took a huge amount of resources.
I tried with Html helper's toRichTextObject and a simple tag but got empty result.
How is it possible to add a remote image to a PhpSpreadsheet cell? It doesn't need working offline, it's fine to load the remote images when the file is opened.
As #FabriceFabiyi mentionned in his comment, and after reading:
PhpSpreadseet documentation on drawing.
PHP documentation on file_get_contents.
PHP documentation on file_set_contents.
This worked for me:
$image = file_get_contents('https://website.com/path/to/image');
$imageName = 'a_nice_image_name';
//You can save the image wherever you want
//I would highly recommand using a temporary directory
$temp_image=tempnam(sys_get_temp_dir(), $imageName);
file_put_contents($temp_image, $image);
// And then PhpSpreadsheet acts just like it would do with a local image
$drawing->setPath($temp_image);
$drawing->setHeight(36);
$drawing->setWorksheet($sheet);
$drawing->setCoordinates('B15');
More on temporary directories in the PHP docs:
sys_get_temp_dir()
tempnam()
There is an example how add image to your excel export:
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Paid');
$drawing->setDescription('Paid');
$drawing->setPath('/images/image-1.png'); // put your path and image here
$drawing->setHeight(30);
$drawing->setCoordinates('A5');
$drawing->setOffsetX(110);
$drawing->setRotation(25);
$drawing->getShadow()->setVisible(true);
$drawing->getShadow()->setDirection(45);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
Specifying coordinates for the image might help, as per the examples and the documentation
$objDrawing->setCoordinates('A3');
Note that an image isn't in a cell/column/row, but overlaid over the main sheet at the same position as that cell/column/row

How to fill image in pdf file using pdftk

This is my sample code.
I'm stuck on how to insert images into this.
I would be grateful for any help:
include('autoload.php');
use mikehaertl\pdftk\Pdf;
$dat = "review.png";
$pdf = new Pdf('Josco.pdf');
$pdf->fillForm(['signature'=>$dat ]);
$pdf->saveAs('my.pdf');
I don't know how to fill image in image type field. Please find attached
the pdf file for reference :
[1]: https://drive.google.com/file/d/1wz_mhdCFbG5o_1CgF6VlIJ4Gh70Ne8-E/view?usp=sharing
It is not possible to add image in a pdf using pdftk alone. A possibility would be to use tcpdf or fpdf to generate a pdf file of a blank page with the signature located where you want. Then to use the pdftk stamp function to stamp this generated file on the source pdf

Change image size to Save on PHP server

I have a form in my Android app that send information to php server with an image pick button. I want to resize image before saving on server with php codes :
<?php
move_uploaded_file($_FILES['file']['tmp_name'],'uploads/'.$_FILES['file']
['name']);
$orgfile='uploads/'.$_FILES['file']['name'];
list($width,$height)=getimagesize($orgfile);
$newfile=imagecreatefromjpeg($orgfile);
$thumb='uploads/a/'.$_FILES['file']['name'];
$truecolor=imagecreatetruecolor(600,400);
imagecopyresampled($truecolor,$newfile,0,0,0,0,600,400,$width,$height);
imagejpeg($truecolor,$thumb,100);
unlink($orgfile);
?>
This code just resize jpeg images and another formats (png or gif and even jpg) saved a black image.
It is necessary to mention that name of image file changed to a random number like "32165465423" and I don't know the image format to use "imagecreatefrompng" or "imagecreatefromgif" in my php file.
I want a code like "imagecreatefromall" or another ...
Thanks guys(sorry for bad English)
You will have to detect the type of image, based on that you can run the function. See the one cool php library for reference
https://github.com/eventviva/php-image-resize/blob/master/lib/ImageResize.php#L77

Gmagick thumbnail for multi-page PDF

I'm attempting to create a thumbnail of a multi-page PDF document using Gmagick, however I only want the first page of the PDF, not all of them.
$thumb = new Gmagick();
$thumb->readImage("/path/to/file/document.pdf");
$thumb->setImageFormat('JPG');
$thumb->thumbnailimage(198, 255);
$thumb->writeImage("/path/to/file/document.jpg");
$thumb->destroy();
This code works, however instead of creating just 1 image 'document.jpg' it creates 'document.jpg.0', 'document.jpg.1', 'document.jpg.2', etc. for all of the PDF pages. I could go and delete all the additional pages and rename the first image just 'document.jpg' but that seems a bit hacky to me.
Is there a way to designate just the first page of the PDF? I can't seem to find anything, and Gmagick's documentation seems to be lacking.
Pekka is right. GraphicsMagick - just like ImageMagick - accepts the special pdf file name notation filename.pdf[0] which would render only the first page, filename.pdf[1] for the second page, and so on.

Categories