Page 1 of 1

php: help with shell command (pdf->jpg)

Posted: 2007-10-16T11:05:13-07:00
by Lestat
php 5.2.4
imageMagick
Ghostscript

I've figured how to write php to convert a pdf to a jpg thumbnail. But I'm having trouble understanding the commands a little bit.

I'm successful in creating a thumbnail from pdf like so...

Code: Select all

/***
*
* Create Thumbnail
*
*/

flush();
ob_flush();
$targetThumb = "$dir_targetDir/" . basename($fileName, '.pdf') . "_thumb.jpg";
$pdf = $filePath;
$command = "e:\\php\convert $pdf -colorspace RGB -geometry 200 $targetThumb ";
exec("$command");
My desired output is a single .pdf. However if there is a multipage pdf (in this case 3 pages) it outputs 3 thumbnails...
test.pdf_thumb-0.jpg
test.pdf_thumb-1.jpg
test.pdf_thumb-2.jpg

I have tried:

Code: Select all

$command = "e:\\php\convert $pdf[0] -colorspace RGB -geometry 200 $targetThumb ";
The result for that was poor, as there was NO conversion. This is understandable because at this point $pdf[0] does not exist. $pdf is a filepath/name, not an array.

How do I get the page information to tell IM I only want the first one?

Re: php: help with shell command (pdf->jpg)

Posted: 2007-10-16T13:03:57-07:00
by el_supremo
You need to add the "[0]" to the end of the filename. I don't know PHP, but something like this should do it:
$pdf = $filePath"[0]";
so that, for example, the filename "test.pdf" is converted to "test.pdf[0]" before being used in the call to convert.
Pete

Re: php: help with shell command (pdf->jpg)

Posted: 2007-10-16T13:04:30-07:00
by Bonzo
I would try something simple first and then build on it when you get it to work. You are on the right track but as you say "$pdf is a filepath/name, not an array."
You need to add the [0] to the path I suppose you could use $pdf .= "[0]";

Code: Select all

exec("convert input.pdf[0] -resize 100x100 small_pdf.jpg");
You just beat me to it Pete.
Note you would need to use $pdf = $path."[0]";

Re: php: help with shell command (pdf->jpg) [ SOLVED ]

Posted: 2007-10-16T13:28:31-07:00
by Lestat
Right on Bonzo. That was the ticket. Positive results as follows:

Code: Select all

/***
*
* Create Thumbnail
*
*/

flush();
ob_flush();
$targetThumb = "$dir_targetDir/" . basename($fileName, '.pdf') . "_thumb.jpg";
$pdf = $filePath;
$pdf .= "[0]";
$command = "e:\\php\convert $pdf -colorspace RGB -geometry 200 $targetThumb ";
exec("$command");
..generates page 1, of a 3 page .pdf :D

P.S. Great examples pages you are making up. I was visiting a bit yest and got some 404 action. I'm assuming your were working on the pages as I surfed hehe. I've steered clear of many of the magicWand functions as I needed faster results by using some of the command line functions off these pages. I'll be certain to spend some time there after this immediate project is done.

Re: php: help with shell command (pdf->jpg)

Posted: 2007-10-16T14:16:20-07:00
by Bonzo
I am glad you are sorted now and thank you for the comments about my site.

I have had a look and can not find any 404 pages tonight, my server may have been playing up yesterday. If you recive any more please could you let me know what they relate to.
I have checked the server log and have a gif image missing and so that log is filled up with referances to that !

I prefer the command line and php over magickwand as its another lot of code that needs to be learnt. I am told that Magickwand is quicker than the exec method.

Re: php: help with shell command (pdf->jpg)

Posted: 2007-10-16T14:23:12-07:00
by Lestat
I am told that Magickwand is quicker than the exec method.
I'm sure it is. What I meant by fast, is getting something to do the trick NOW vs. the learning curve of anything. Not speed...most of thats over my head. But it would make sense that php functions would be quicker since its one less machine call. :D

Re: php: help with shell command (pdf->jpg)

Posted: 2007-10-16T19:26:24-07:00
by anthony
Bonzo wrote:I would try something simple first and then build on it when you get it to work. You are on the right track but as you say "$pdf is a filepath/name, not an array."
You need to add the [0] to the path I suppose you could use $pdf .= "[0]";

Code: Select all

exec("convert input.pdf[0] -resize 100x100 small_pdf.jpg");
NOTE the quotes was to prevent the shell handling the '[0]' construct. The above fails in this. Quote it on the exec.

Code: Select all

  exec("convert 'input.pdf[0]' -resize 100x100 small_pdf.jpg");