4

Can one binary files be converted to another(in fact to image),

like
a pdf to image,
a doc to image,
a xls to image etc...

The whole idea is to present a preview to the user while downloading the file. like suppose we have a doc file if user hover on it or clicked on the preview button we show users preview of a file.

Any help would be highly appreciated.
Thanks

tpatja
  • 690
  • 4
  • 11
avisheks
  • 1,178
  • 10
  • 27
  • I believe there is the same question already answered: http://stackoverflow.com/questions/467793/how-do-i-convert-a-pdf-document-to-a-preview-image-in-php – dragn May 15 '14 at 14:51
  • I'm sorry, the question I linked above is about PDF files only. – dragn May 15 '14 at 14:59

2 Answers2

0

The Problem convert pdf / doc to images for preview

you need to install - ImageMagick - GhostScript

Create GIF Thumbnail of First PDF Page

<?php
    //the path to the PDF file
    $strPDF = "my_pdf.pdf";
    exec("convert \"{$strPDF}[0]\" -colorspace RGB -geometry 200 \"output.gif\"");
?>

Create JPEG Thumbnails of ALL Pages Within PDF

<?php
    //the path to the PDF file
    $strPDF = "my_pdf.pdf";
    exec("convert \"{$strPDF}\" -colorspace RGB -geometry 200 \"output.jpg\"");
?>

Create Large PNG 1024px Image of First PDF Page

<?php
    //the path to the PDF file
    $strPDF = "my_pdf.pdf";
    exec("convert \"{$strPDF}[0]\" -colorspace RGB -geometry 1024 \"output.png\"");
?>

Create Large PNG 1024px Images of ALL Pages Within PDF

<?php
    //the path to the PDF file
    $strPDF = "my_pdf.pdf"; 
    exec("convert \"{$strPDF}\" -colorspace RGB -geometry 1024 \"output.png\"");
?>
Abhishek Jaiswal
  • 2,524
  • 1
  • 21
  • 24
0

There isn't any way to do this programmatically without writing/implementing an entire rendering engine, which would be slow, difficult, and not particularly effective.

Instead, what you need to do is take a screen capture yourself and crop/resize it to whatever dimensions you wish.

Incidentally, if you are using Windows, holding ALT when you press the prt scn button will capture only the active window, which might make this easier.

Invictus
  • 181
  • 9