Get a thumbnail from Pdf or Video file using phMagick

August 9, 2009 by nuno costa 20 comments

phMagick :: Thumbnail from Pdf or Video file

Please note that for videos you must have the correct codecs installed in your server and for pdf you need ghostscript

 

It's quite simple to use

$p = new phmagick('','destination.png');
 
$p->acquireFrame('filename.pdf');

 

parameters:

file: the path to the file

20 comments so far Add Your Comment

  1. by Khairil Harith on October 11 2009 at 20:57

    I have noticed that PDF 2 PNG is possible but only the first page; Is it possible to get either the full pages or we can specify what page to convert from PDF 2 PNG, or maybe both?

    TQ

  2. by nuno costa on October 12 2009 at 16:19

    Hi Khairil

     function acquireFrame($file, $frames=0){

    try using frames= 99999 or something like that and you will end up with a series of files numbered sequentially each one representing a page from the PDF

  3. by Khairil Harith on October 13 2009 at 21:30

    Thank-you sir, I will try it immediately. Another question sir, other than PDF or Video file, is it possible to get an image from a full html page, with a given URL?

  4. by Khairil Harith on October 13 2009 at 21:51

    Sorry sir, the ‘ function acquireFrame($file, $frames=0){‘ i’ve changed to 99999 doesn’t work. It only list the page number starting from 0, any more ideas?

  5. by nuno costa on October 14 2009 at 12:47

    Hi Khairil,

    I’ve never tested it, but imagemagick supports converting html to an image so it should work.

    It’s my bad, I’ve explained you the wrong way.

    If you want all the pdf pages saved as an image do like this :

    $p = new phmagick( 'test.pdf','results/pdf-save.png');
    $p->convert();

    you will end up with pdf-save-0.png, pdf-save-1.png, and so on

    you you just want a specific page then you use the acquireFrame() with $frames equal to the page number (pages start at 0)

  6. by Khairil Harith on October 15 2009 at 23:07

    Thanks a lot, i’ll buy you a beer one day if my current project sells, and thats a promise

  7. by Premanand on November 9 2009 at 14:18

    Hi,

    I am getting following error message, when i am converting from PDF file to image file using phmagick class.

    Imagemagick version : ImageMagick-6.5.7-6-Q16

    Error :
    Array
    (
    [0] => convert: `%s’: %s “gswin32c.exe” -q -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dEPSCrop -dAlignToPixels=0 -dGridFitTT=0 “-sDEVICE=pnmraw” -dTextAlphaBits=4 -dGraphicsAlphaBits=4 “-r72x72″ -dFirstPage=1 -dLastPage=1 “-sOutputFile=C:/WINDOWS/TEMP/magick-WoV5rojT” “-fC:/WINDOWS/TEMP/magick-WTLcMqP_” “-fC:/WINDOWS/TEMP/magick-aE6VvNda”.
    [1] => convert: Postscript delegate failed `1.pdf’: No such file or directory.
    [2] => convert: missing an image filename `imgpreview.png’.
    )
    Please share your thoughts. Thank you!

  8. by nuno costa on November 9 2009 at 14:50

    Hello,

    You need to have GhostScript installed on your server

  9. by Premanand on November 9 2009 at 14:51

    I have used Phmagick class. Its converting correctly from PDF file to image, but i am getting below warning message.

    “**** Warning: Fonts with Subtype = /TrueType should be embedded. [1] => But Verdana is not embedded. [2] => **** Warning: Fonts with Subtype = /TrueType should be embedded. [3] => But Verdana,Bold is not embedded. [4] => **** Warning: Fonts with Subtype = /TrueType should be embedded. [5] => But Arial is not embedded. [6] => **** Warning: Fonts with Subtype = /TrueType should be embedded. [7] => But TimesNewRoman is not embedded. [8] => **** Warning: Fonts with Subtype = /TrueType should be embedded. [9] => But Arial,Bold is not embedded. [10] => [11] => **** This file had errors that were repaired or ignored. [12] => **** The file was produced by: [13] => **** >>>> Acrobat
    PDFWriter 5.0 for Windows NT <<< **** Please notify the author of the software that produced this [15] => **** file that it does not conform to Adobe’s published PDF [16] => **** specification. [17] => )”

    Please share your thoughts. Thank you!

  10. by nuno costa on November 9 2009 at 15:35

    I believe it has something to do with your ghostscript installation or missing fonts

  11. by Roland on February 12 2010 at 12:28

    Hi,

    This is great. It seems to work on short pdfs but on pdfs with more pages my thumb comes out as a skewed black and white image, any ideas on what I might be doing wrong here?

  12. by Rafael on February 27 2010 at 00:56

    I want to obtain the number of pages in the pdf to see how many images were generated…

  13. by nuno costa on March 1 2010 at 19:45

    Hi Rafael,

    I don’t think you can do that with imagemagick

  14. by nuno costa on March 1 2010 at 20:01

    Hi Roland

    If you can email me the problematic pdf I can have a look, have you tried to update your ghostscript install ?
    (imagemagick uses ghostscript to convert pdf so the problem might be there )

  15. by chiu on May 18 2011 at 09:11

    Hi,
    I am getting following error message, when i am converting from PDF file to image file using phmagick class.My server os is linux,and I know the gs command is at ‘opt/bin/’ ,what should I do .
    Thank you.

    phMagick: Error executing “/opt/bin/convert -scale “200x>” -quality 80 -strip “ttt.pdf” “/opt/bin/”"
    return code: 1
    command output :”sh: gs: command not found
    convert: Postscript delegate failed `ttt.pdf’: @ error/pdf.c/ReadPDFImage/663.
    convert: missing an image filename `/opt/bin/’ @ error/convert.c/ConvertImageCommand/3011.”
    Notice: exception ‘phMagickException’ in /share/MD0_DATA/Web/edit/phmagick/phmagick.php:189 Stack trace: #0 /share/MD0_DATA/Web/edit/phmagick/plugins/resize.php(46): phmagick->execute(‘/opt/bin/conver…’) #1 [internal function]: phMagick_resize->resize(Object(phmagick), 200, 0) #2 /share/MD0_DATA/Web/edit/phmagick/phmagick.php(205): call_user_func_array(Array, Array) #3 [internal function]: phmagick->__call(‘resize’, Array) #4 /share/MD0_DATA/Web/try.php(8): phmagick->resize(200, 0) #5 {main} in /share/MD0_DATA/Web/edit/phmagick/phmagick.php on line 189

    Array
    (
    [0] => Array
    (
    [cmd] => /opt/bin/convert -scale “200x>” -quality 80 -strip “ttt.pdf” “/opt/bin/”
    [return] => 1
    [output] => Array
    (
    [0] => sh: gs: command not found
    [1] => convert: Postscript delegate failed `ttt.pdf’: @ error/pdf.c/ReadPDFImage/663.
    [2] => convert: missing an image filename `/opt/bin/’ @ error/convert.c/ConvertImageCommand/3011.
    )

    )

    )
    1

  16. by Jay on May 20 2011 at 12:32

    Hi,

    I can’t seem to convert PDFs to JPGs at all. I managed to convert a JPG to PDF, but not the other way round.

    I installed Ghostscript. Is there something I should do so Imgmagick and ghostscript are working together properly?

    I don’t get any error from phMagick.

  17. by EuaLin on June 22 2011 at 12:27

    Hi there!

    One quick question, please. General speaking, would it be safe for my server if I enable the exec() command in order to make the aforementioned script run? If not, how could I skip this command?

    EuaLin

  18. by nuno costa on August 1 2011 at 20:47

    You need the exec command

    as long as you sanitize the user input and make good use of open_basedir() you should be safe

  19. by nuno costa on August 1 2011 at 20:51

    please check imagemagick.org website

  20. by nuno costa on August 1 2011 at 20:51

    please install ghostscript

More from francodacosta.com

© francodacosta.com - All rights reserved