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

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Lestat

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

Post 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?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

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

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

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

Post 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]";
Lestat

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

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

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

Post 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.
Lestat

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

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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");
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply