PDF to Image Conversion - PHP - php

Note: PDF files would be uploaded by user.
I am making a document sharing website(pdf). I don't want users to be able to copy/select the pdf file text. I am looking for a nice PDF/Image Document viewer. Currently using the following method:
I am using imagemagick to convert PDF file to Jpeg. But the process is too slow even after disabling OpenMP. I was looking forward to convert full PDF to different image files but decided to just convert the first image to a thumbnail because the process is so slow.
I am using it on my local machine and it takes more than 30 seconds to convert 40-50 pages in good quality. So if there are pdf files more than 100 pages, this process would ruin the user experience.
Is there any way to convert PDF to Image files?
Also is there a way to let this process (pdf to image) happen asynchronously? Like the user filling out file details while the pdf is being uploaded and converted, something like YT videos?

Enter the command in exec() function, this will help you out
<?php
$pdf_url = 'testfile.pdf'; // Url of the PDF
$image_name = 'output.jpg'; // Set the image name
exec('convert "'.$pdf_url.'" -colorspace RGB -resize 800 "'.$image_name.'"', $output, $return_var);
?>
Hope this helps you

¿You use a linux sever?
Just use convert command and put [0] sufix to pdf file for save a cover:
exec("convert '/home/sample.pdf[0]' '/home/test.png'");

Related

PHP - Possible to take a base64 encoded pdf data string and compress it?

So I have an XML file that has a base64 encoded data string for a pdf file, which just has an image taken from an iPad.
This pdf file can be excessively large, as much as 14MB with dimensions of 57"x38".
These images are taken from an iPad through a DocuSign session, thus I have no way at the moment of controlling their size or format before they get to my php listener script.
However, my script cannot work with such large files as my CRM's API file size max is 10MB, and I need a way of reducing the file size before I can upload it through my CRM's API.
Now if it was just a jpg, it would be ok as there are plenty of ways to reduce file size in PHP, but it is a PDF. I have found plenty of PHP extensions for making PDFs, but I haven't found any for reading a PDF and extracting an image from it.
So is there a way to extract the image from the PDF through PHP, or perhaps compress the pdf file?
UPDATE
I didn't think about the possibility of converting a pdf into a jpg, which apparently is easier to do with imagick. Having my server admin install it and I will see if I can make it work with my script.
UPDATE 2
So I was able to get imagick working and locally I am able to convert pdf files into jpg, and reduce file size dramatically.
However, I am running into an issue using it with my application. I get the following error from my CRM's API:
Failed to parse XML-RPC request: Invalid byte 1 of 1-byte UTF-8 sequence.
So the process is the following:
XML file has a base64 encoded data stream of the pdf file.
I decode this data
I then convert with imagick and reduce file size
I base64 encode and prep for upload
CODE
$imageBlob = base64_decode((string)$pdf->PDFBytes);
$imagick.$x = new Imagick();
$imagick.$x->readImageBlob($imageBlob);
$imagick.$x->setImageFormat('jpeg');
$imagick.$x->setImageCompressionQuality(60);
$imagick.$x->adaptiveResizeImage(1024,768,true);
$imageBlob = $imagick.$x->getImageBlob();
$PDFdata[] = base64_encode($imageBlob);
I can test the date by using the proper header and I can see the new jpeg fine, so I assume the data is properly formatted.
What I am missing?
Ok, so I figured it out.
Imagick was the way to go, and my use of it was good. I just goofed up on the file name because I wasn't using a proper dynamic variable name. Code should have looked like this:
CODE
$imageBlob = base64_decode((string)$pdf->PDFBytes);
${'imagick'.$x} = new Imagick();
${'imagick'.$x}->readImageBlob($imageBlob);
${'imagick'.$x}->setImageFormat('jpeg');
${'imagick'.$x}->setImageCompressionQuality(60);
${'imagick'.$x}->adaptiveResizeImage(1024,768,true);
$imageBlob = ${'imagick'.$x}->getImageBlob();
$PDFdata[] = base64_encode($imageBlob);
$PDFfile[] = $FormCustomField . $x . '.jpg';
So the error I was getting was because of an invalid file name, because the $x variable in the previous code was getting junk values. Now everything works fine.

how to convert any image file into EPS format in PHP?

I have an image processing system in PHP with Imagemagick. The existing system will be processing images of EPS format for some process and PNGs for the remaining process. So, I need to upload the same image file in EPS and PNG. I am doing the file upload facility now, which should automate the procedure of converting any format image file into EPS and PNG and should save in corresponding locations.
What I need now is to be able to convert any image format file into EPS and PNG, so then I can process and save them, but there are some DPI limitations. So I need to save the files into these EPS and PNG formats so that only the existing system can use those files properly.
Please advice me if there is any way to convert image files into EPS and PNG with PHP and Imagemagick.
Thanks in advance.
You can convert any image to eps format by following code
public function convertImageToEps(){
$imgUrl = WWW_ROOT.'imfcs.jpg';
$imagic = new Imagick();
$imagic->readImage($imgUrl);
$imagic->setImageFormat('eps');
$imagic->writeImage(WWW_ROOT.'simfcs.eps');
return true ;
}

PHP: how to create an image from another PNG image

I have a small Minecraft server where people can upload their skins. Minecraft skins are small png images. Is it possible to convert this png image to another png image via PHP (e.g. GD library)?
I have made this image to help me explain what I am trying to do:
Yes, it's possible. You'd need multiple imagecopy commands to pull out sections of the skin image and paste it into the proper spots in the "output" image.
Basic order of operations would be:
$input = imagecreatefrompng('skin.png');
$output = imagecreatetruecolor(800, 600); // whatever the dimensions should be.
imagecopy($output, $input, 0,0, 10,20, 50,60);
imagecopy(...);
...
...
The first copy command is saying "take a 50x60 section of the input image, starting at coordinates 10x20, and paste it into the destination image in the top left corner".
The actual sequence/coordinates/sizes will be up to you to figure out.
If you're not doing a 1:1 copy of the image and are doing resizing, then you'll want imagecopyresampled() instead.
Here is the PHP manual for creating images from png :
http://php.net/manual/en/function.imagecreatefrompng.php
Here is a simple tutorial :
http://www.phptutorial.info/?imagecreatefrompng
You can do this with CSS
Here is a tutorial: http://www.w3schools.com/css/css_image_sprites.asp

imagemagick - downsize/crop/save to jpeg in one go?

I have a php script that will receive a bunch of images uploaded.
What I need to do is create a small thumbnail of each, on the fly using imagemagick.
I can do that easy enough but I also need to crop it so that the thumbnail is always 100x100.
the images supplied won't be the same proportions so simply downsizing won't work.
Can I downsize, crop to 100x100 and save to jpeg all in one step?
I believe this should do what you want:
convert 'just_uploaded/*' -resize 100x100^ -gravity center -extent 100x100 -set filename:f '%t' +adjoin 'just_uploaded_thumbs/%[filename:f].jpg'
resize will downsize, extent (in combination with gravity) will crop, and the rest takes care of saving with a modified name, in JPEG format, in a different directory.
Short answer: no. That'll be 3 steps, no less.
Longer answer: you can do it using the command line interface. In PHP, the only way is to write a function that will do what you ask. Then, for each image, you can just call your function. I'm not sure how this is more beneficial than just using the 3 Imagick functions separately...
I like the sfThumbnailPlugin. It wraps around both ImageMagick or GD
http://www.symfony-project.org/plugins/sfThumbnailPlugin
Example:
public function executeUpload()
{
// Retrieve the name of the uploaded file
$fileName = $this->getRequest()->getFileName('file');
// Create the thumbnail
$thumbnail = new sfThumbnail(150, 150);
$thumbnail->loadFile($this->getRequest()->getFilePath('file'));
$thumbnail->save(sfConfig::get('sf_upload_dir').'/thumbnail/'.$fileName, 'image/png');
// Move the uploaded file to the 'uploads' directory
$this->getRequest()->moveFile('file', sfConfig::get('sf_upload_dir').'/'.$fileName);
// Do whatever is next
$this->redirect('media/show?filename='.$fileName);
}

Generate Thumbnails with PHP for a wide range of file formats

I have had a client request on a upload facility for his clients, but after upload that a image thumbnail to be created.
All normal images are ok but he his talking about .psd, .pdf, .eps, .ppt
Having a good look around I think wih imagemagick & ghostscript will cater for most of these but I cant find a solution of PPT or EPS.
Im hoping that imagemagick will be able to do eps as it can do a psd.
Any suggestion on EPS or PPT file format.
Thank you if you can advice.
I'm late to the party I know, however...
This is what I use for .PDF, .EPS and .AI thumbnailing. (Assuming all necessary ImageMagick distros installed)
$file = 'filename.pdf.eps.ai';
$cache = $_SERVER['DOCUMENT_ROOT'].'/cache/';//ensure dir is writeable
$ext = "jpg";//just the extension
$dest = $cache.$file.'.'.$ext;
if (file_exists($dest)){
$img = new imagick();
$img->readImage($dest);
header( "Content-Type: image/jpg" );
echo $img;
exit;
} else {
$img = new imagick($_SERVER['DOCUMENT_ROOT'].'/'.$file.'[0]');
$img->setImageFormat($ext);
$width = $img->getImageheight();
//$img->cropImage($width, $width, 0, 0);
$img->scaleImage(105, 149, true);
$img->writeImage($dest);
header( "Content-Type: image/jpg" );
echo $img;
exit;
}
Don't know why it works, but it does - One code to rule them all right?
PPT is a powerpoint presentation. So creating an image that is a PPT would require some library that can pull this off.
Here are some resources to help you out.
Generate Powerpoint file on the fly
EPS is a vector format, so not unless you have your image as vector objects, you wont be able to do this correctly.
Just some thoughts - none of these things has been tested by myself.
EPS:
You should be able to convert your EPS to a PDF with ghostscript. Using imagemagick & ghostscript you can convert the PDF to some bitmap format (GIF, PNG or JPG).
PPT:
This seems to be somehow more complicated. If your are on a Windows machine you could resort to use the Powerpoint API from within a small hand-written converter. Another possibility would perhaps be to use Apache POI-HSLF whichs is a Java API to the Powerpoint file format. This would require a Java program for the conversion process. The last resort could be that study the Powerpoint binary file format and see if there is e.g. a thumbnail embedded (perhaps beeing used for the file icon in Windows Explorer) that could be extracted.
You could find some free icon sets and use a default icon for all .ppt file and another for all .eps. You can then further extend this for all file formats that cannot be converted to a image, such as audio files. Not the perfect solution but something a user may feel more comfortable with then just having text.

Categories