Hello, I am trying to make a PDF preview in jpg/png thumbnail with PHP. For example when page is loading in php code a thumbnail is generated from page 1 of a PDF file and displayed on screen. I am using ImageMagick function 'convert' in PHP to generate such thumbnail image from PDF file but out put is first a file (PNG, JPG) and then I use it to load in main page. This method is slow because it first saved a picture file on disk then loads it (not to mention if PDF is on another web location it takes time to load PDF and create page 1 picture on file). This is what I got till now: exec("/usr/bin/convert pdffilehere.pdf[0] thumbnailfile.jpg"); this will create the thumbnail file from first page of pdf. Working great via convert function (imagemagick). Now comes my challange: how I can skip this step of creating the file (thumb) on disk, I just need a preview in browser window. Is there a ImageMagick function or option to generate TN on the fly ? Or can it be done in other way in PHP. Please just hint me and I will continue from there. Best, R.
Not as far as I know with ImageMagick since it's not integrated into php - apart from via exec()/system(). GD supports directly outputting to the browser however it doesn't support conversion from pdf files. Given that, there wont be a big delay in writing the file to the server, so you're unlikely to notice it. The delay is more likely to come from you either the retrieval of the pdf from the remote location, or the download of the image to the clients machine.
Thanks for your quick answer 'mfscripts'. I was searching and searching and no result so I guess you are right. Is there a way then to retreive just first page of a pdf not entire pdf in order to create first page thumbnail ? All the best!
One more thing: I found out a way or at least in theory and it is like: <?php /* Read page 1 */ $im = new imagick( 'test.pdf[0]' ); /* Convert to png */ $im->setImageFormat( "png" ); /* Send out */ header( "Content-Type: image/png" ); echo $im; ?> but I get class imagick is not defined. This is because on my hosting account was not installed this imagemagick function? or? I got a .rpm package (imageshaclk package for linux) but I don't have access to install it on hosting acct. Any ideas ? r.
hmm... don't think so? I've done some work with reading the encoding on pdfs before and as new versions come out they tend to change the structure completely so it's not even as though you can read the first x part of a file reliably. Are the pdf files you're retrieving quite large then? Could you preload/precache them before the user gets to the page somehow?
Not quite, I was also thinking about this. PDF are looked up by user search so are generated on the fly so in order to have them displayed system is downloading, reading + creating first page thumbnail and then I load it and it taked quite a while (not to mention if pdf file is >1MB etc). I can limit number of search results / page but this is my last option. I want to try to install ImageMagick in order to take advantage of imagick function but I don't know if it is possible vie cpanel for my hosting. Thanks so much for your help!
Interesting, I didn't know you could access it via php. Looks like it's available as a php extension but your host may have to enable it within the php.ini file: http://hasin.wordpress.com/2008/02/06/installing-imagick-extension-for-php-in-ubuntu-710/