Can any binary be converted to image in php

Is it possible to convert one binary file to another (actually for an image),

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

The whole idea is to present the user with a preview when downloading a file. Suppose we have a doc file, if a user hovered on it or clicked on a preview, we show users a preview of the file.

Any help would be greatly appreciated.
Thanks

+4
source share
2 answers

Problem to convert pdf / doc to preview images

you need to install - ImageMagick - GhostScript

Create GIF thumbnail of the 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 in 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 First Page Image PDF

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

Create large PNG images of 1024px ALL pages in PDF

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

There is no way to do this programmatically without writing / implementing the entire rendering mechanism, which would be slow, difficult and not particularly efficient.

Instead, you need to take a screenshot and crop / resize it to any size.

By the way, if you use Windows while holding ALT , when you press the prt scn button, only the active window will be displayed, which can make this easier.

0
source

Source: https://habr.com/ru/post/1488637/


All Articles