Html to PDF to JPEG - php

I am basically all the way there. I am unsure how to push the temporary pdf object to imagick to generate and return the jpeg image. Using mPDF's standard output(), I get the pdf rendered to the screen:
$mpdf=new mPDF();
$mpdf->WriteHTML($output);
$img = new Imagick($mpdf->output());
$img->setResolution(300,300);
$img->resampleImage(150,150,imagick::FILTER_UNDEFINED,1);
$img->resizeImage(512,700,Imagick::FILTER_LANCZOS,0);
$img->setImageFormat('jpeg');
$img->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
$img->writeImage ("test.jpeg");
Under this scenario I get the pdf outputed to the screen instead of a jpg image returned/created. What is the proper way to pass the pdf into imagick?

I think you need: $mpdf->Output('temp/'.$filename.'.pdf','F');
$mpdf->Output('temp/'.$filename.'.pdf','F');
$pdf_file = 'temp/'.$filename.'.pdf';
$savepath = 'temp/'.$filename.'.jpg';
$img = new imagick($pdf_file);
$img->setImageFormat('jpg');
$img->writeImage($savepath);
echo "<img src='temp/$filename.jpg' />";
http://phpform.net/pdf-to-image.php

Related

PHP PDF to image crops some files when generating a thumbnail. setIteratorIndex fails as well

I have written a function in PHP 7.4 that generates a thumbnail of the first page of a PDF document. The function works as intended for most PDF files.
However, some PDFs get cropped when I generate the thumbnail. Additionally, sometimes, I don't get the first page.
This PDF triggers both problems:
http://www.teavigoinfo.com/pdf/study-27.pdf
function PDF2Thumbnail($url) {
$image = #file_get_contents($url);
if($image) {
$im = new Imagick();
$im->readImageBlob($image);
$im->setIteratorIndex(0);
$im->thumbnailImage(300, 0);
$im->setImageFormat('png');
$im = $im->flattenImages();
header('Content-Type: image/png');
echo $im;
}
}
Thank you.

PHP dataURL to png image

I have captured an image from the canvas and sent it via a ajax post method to my php file.
Here I need to put it back into a png with a filetype and extension. As I will be adding this new image to a pdf using fpdf.
Here is my code. I am just left with a broken image link icon.
<?php
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
$imgEmbed = $_POST['embed'];
//convert base64 string into an image file by wrapping
//it in the png container
$parts = explode(',',$imgEmbed);
$data = $parts[1];
$data = base64_decode($data);
header('Content-Type:image/png');
$pdf =& new FPDI();
$pdf->AddPage();
//Set the source PDF file
$pdf->setSourceFile("test2.pdf");
//Import the first page of the file
$tppl = $pdf->importPage(1);
//Use this page as template
// use the imported page and place it at point 20,30 with a width of 170 mm
$pdf->useTemplate($tppl, null, null, 215.9, 279.4, TRUE);
//Select Arial italic 8
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(100, 100);
$pdf->Write(0, 'This is just a simple text');
$pdf->Image($newIm,null,null,150,100);// x y h w 'png'
$pdf->Output("custom.pdf", "I"); // d send to browser, F send to local
// I auto open in browser window.
?>
$newIm='test.png';<br>
if(exif_imagetype($newIm)==IMAGETYPE_PNG) {<br>
$file_type="PNG";<br>
}elseif (exif_imagetype($newIm) ==IMAGETYPE_JPEG) {<br>
$file_type="JPG";<br>
}<br>
$pdf->Image($newIm,null,null,150,100,$file_type);<br>
In Fpdf,The Image synatax reads the whole image pixel by pixel and if the image base is in png and we give the extension as test.jpg then it will give an error so here i have used exif_imagetype() which will give me the image type of the image.And then i will pass this $file_type in the syntax to get the result.And in my case it has worked.

getting image dimensions in imagick

I'm trying to get the dimensions of images that i converted with imagick. This is my code:
$imagick = new \Imagick();
$imagick->setresolution(300,300);
$imagick->readimage($this->uploadPath . '/original/test.pdf');
$imagick->writeImages($this->uploadPath . "/large/preview-%d.jpg", false);
So i'm reading a multipage pdf file and i'm converting it to seperate jpg files.
What i want is to retrieve the jpg file dimensions from each image. Can i do this in an easy way? Or do i first need to write the images to the disc and then loop through each of them again?
What about Imagick::getImageGeometry? (http://www.php.net/manual/en/imagick.getimagegeometry.php)
In your case it should look something like this:
$imagick = new \Imagick();
$imagick->setresolution(300,300);
$imagick->readimage($this->uploadPath . '/original/test.pdf');
$geometryInfo = $imagick->getImageGeometry();
// now $geometryInfo is an associative array with 'width' and 'height' items in it
$imagick->writeImages($this->uploadPath . "/large/preview-%d.jpg", false);
Update:
If you want to read concrete page from multipage PDF, try to use such trick:
$pageIndex = 0; // you can create "for/foreach" loop to go through all pages
$imagick->readimage($this->uploadPath . '/original/test.pdf[' . $pageIndex . ']');

Create Thumbnail of ONLINE PDF for first page only using Imagick

I tried to make a thumbnail of a pdf file which is hosted on another server. My current code is:
<?php
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf");
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
The problem is that code is only generating thumbnail for LAST PAGE of the pdf file. How can I make a thumbnail for first page only? I tried to add [0] at the imagick line.
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf[0]");
but it didn't work. It only work for local pdf file, i.e:
$im = new imagick("my-pdf-file.pdf[0]");
Please help me solve this problem.. Thanks..
You'll need to reset the active image to the first page. This can be done with Imagick::setIteratorIndex.
<?php
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf");
$im->setIteratorIndex(0);
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
Try...
$im->setImageIndex(0); //this will return 1th page of the pdf file
$im->setImageFormat('jpg');
"This can be done with Imagick::setIteratorIndex. .."
..or not. Simply has no effect . Setting it to one crashes something, setting it to 0 gets the last page..
function make_thumbnail($filename)
{
try
{
$imagick= new Imagick($filename);
}
catch(ImagickException $e)
{
// failed to make a thimbynail. what now?
// load up our trusty truetype font png instead?
$imagick->destroy();
return "0"; // shove any rubbish in the db - it will just say no image available when asked.
}
$imagick->setIteratorIndex(0);// rewind to first page or image of a multi series
$imagick->setImageFormat("png"); // turn it into a png
$imagick = $imagick->flattenImages(); // remove any transparency
$imagick->scaleImage(300,0); //resize...to less than 300px wide
$d = $imagick->getImageGeometry();
$h = $d['height'];
if($h > 300)
$imagick->scaleImage(0,300);
$imagick->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$imagick->setImageCompressionQuality(0);
$imagick->setIteratorIndex(0);
$a = $imagick->getImageBlob(); // output as bytestream
$imagick->destroy();
return $a;
}

image GD and png masking

what would be basic code for masking one image with another in GD - one image with black shape and transparent background would be used to crop another image - a photo so that photo is in the shape of black image.
One way to do it is using phpThumb.
Basic reference here: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x31
If creating the new image on the fly it would be something as simple as:
<img src="../phpThumb.php?src=path/to/image/image.jp&fltr[]=mask|path/to/mask/mask.png&f=png" alt="">
To output into a png.
If doing this after an image upload to create a new image to be stored on the server, first figure out the basics of phpThumb and then set the mask parameters with all the rest:
For example:
...
require_once('phpThumb/phpthumb.class.php');
//Begin phpThumb work to resize image and create thumbnail
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . $destination;
$uploadfile = $uploaddir . $file;
$phpThumb = new phpThumb();
// set data source -- do this first, any settings must be made AFTER this call
$phpThumb->setSourceFilename($uploadfile);
$phpThumb->setParameter('w', 360); //change to update the picture size
$phpThumb->setParameter('h', 470); //change to update the picture size
$phpThumb->setParameter('fltr[]', 'mask|path/to/mask/mask.png'); //set mask
$phpThumb->setParameter('f', 'png'); //set png output format
$outputdir = $_SERVER['DOCUMENT_ROOT'] . $destination;
$output_filename = $outputdir . "masked" . $file;
$phpThumb->setParameter('config_allow_src_above_docroot', true);
if ($phpThumb->GenerateThumbnail()) { // this line is VERY important, do not remove it!
if ($phpThumb->RenderToFile($output_filename)) {
...

Categories