Forum Moderators: coopster

Message Too Old, No Replies

[php]pdf files to thumbnail

how to generate a preview thumbnail of a pdf file?

         

rolan

10:02 am on Jan 20, 2003 (gmt 0)

10+ Year Member



Hi,
i want to generate a previeiw´tghumbnail of a pdf file.
so far i figured out that ghostwrite and imagemagick is needed. so i asked my hoster to install it.

i found a script snippet that should perform this:


$filename = "foo";
$file1 = "/Library/WebServer/Documents/polo/pdf/$filename.pdf";
$file2 = "/Library/WebServer/Documents/polo/jpg/$filename.jpg";
$result1 = @exec("/sw/bin/gs -q -dNOPAUSE -dBATCH -sDEVICE=jpeg
-sOutputFile=$file2 $file1 2>&1");
$result2 = @exec("/usr/local/bin/convert -resize 120x120 $file2
$file2 2>&1");

i altered this to this


$filename = "norden";
$file1 = "$filename.pdf";
$file2 = "$filename.jpg";
$result1 = exec("/usr/bin/gs -q -dNOPAUSE -dBATCH -sDEVICE=jpeg -sOutputFile=$file2 $file1 2>&1");
$result2 = exec("/usr/bin/convert -resize 120x120 $file2 $file2 2>&1");
if($result1 && $result2)
{echo "something happend";}

i made a directory with chmod 777 and loaded this script and a pdf called norden.pdf up. when i call this script
"something happens " appears but no jpg is created.

does anyone has a clue to me? maybe the file is crated
at the /usr/bin/ folder?

thanx for your help.
roland

hakre

4:20 pm on Jan 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dear roland,

that is a really interesting idea, quite common i want to do in the near future. but i did not solve your problem yet. can you tell me, why you added '2>&1' at the end of each commandline?

i just picked up an intereseting cmdparam of gs. you can setup the size in pixels of the output, in your case the jpeg. shouldn't this prevent you from using the second shellexec then? the param is:
-g<width>x<height> sizes are in pixel.

rolan

4:49 pm on Jan 20, 2003 (gmt 0)

10+ Year Member



Hello hakre,
in fact i found this snippet by a google search :)
have a look :

[studio.imagemagick.org...]

well because i completlly new with ghostscript and image magic i dont know where to start.

i made some changes to the script to get some failure messages. i thought it might be possible that the file is still in the php_temp folder so i tried to copy it to my webroot like this:


$filename = "norden";
$file1 = " /home/customer/april5.de/pdftest/$filename.pdf";
$file2 = " /home/customer/april5.de/pdftest/$filename.jpg";
$result1 = exec("/usr/bin/gs -q -dNOPAUSE -dBATCH -sDEVICE=jpeg -sOutputFile=$file2 $file1 2>&1");
copy ($result1 ,". /home/customer/april5.de/pdftest/files/".$file1);
$result2 = exec("/usr/bin/convert -resize 120x120 $file2 $file2 2>&1");
if($result1 && $result2)
{echo "something happend";
copy ($result2 ,". /home/customer/april5.de/pdftest/files/".$file2);
}

and here the output, finally an error message. but i don´t know how to handle this hint. i should try the url maybe :o)


Warning: Unable to open 'GNU Ghostscript 6.53: Unrecoverable error, exit code 1' for reading: Datei oder Verzeichnis nicht gefunden in /home/customer/april5.de/pdftest/mkjpg.php on line 16
something happend
Warning: Unable to open 'convert: Unrecognized option (-resize).' for reading: Datei oder Verzeichnis nicht gefunden in /home/customer/april5.de/pdftest/mkjpg.php on line 20

so it seems that ghostscript and imagemagick is found.
i will try some path options.

roland

hakre

6:09 pm on Jan 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi roland,

i sended you a sticky mail this issue.

rolan

6:31 pm on Jan 20, 2003 (gmt 0)

10+ Year Member



here is (finally) an error message:
code:

$result1 = exec("/usr/bin/gs -q -sDEVICE=jpeg -sOutputFile=norden.pdf norden.jpg");
echo $result1;

output:
'Last OS error: 2'

rolan

1:48 pm on Jan 21, 2003 (gmt 0)

10+ Year Member



SOLUTION:

if imagemagic is conifured with ghostwrite.
simply this Imagemagick command will create an jpg from a pdf file:


$filename="yourpdffilename"
$path2convert="yourpath"; //i.e. /usr/bin/
exec($path2convert."convert $filename.pdf[0] $filename.jpg");

pdf[0] the 0 means the first page of the pdf file

to resize the jpg i use the gd lib of php.
maybe someone give me a hint how to resize it at once
because the -resize option doesn´t work.
HTH
roland

hakre

10:48 pm on Jan 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi rolan,

often it's easy to ask google ;). i found a php class [hotscripts.com] (not tested on my own) which is well rated and a demo is available, too.

rolan

3:06 pm on Jan 22, 2003 (gmt 0)

10+ Year Member



?
well, but where is the pdf option?
this is the way i resize jpg without a class ;)
this is the quick and dirty version. only to display if
imagemagick is working correct.

<?
$result1=exec("/usr/bin/convert norden.pdf[0] norden.jpg");
$file="norden.jpg";
$size = GetImageSize ("$file");
$hoch=120;
$breit=floor($size[0]/($size[1]/120));
$im = ImageCreateFromJPEG ($file);
$im2= ImageCreate ($breit, $hoch);
$breit2=$size[0]+1;
$hoch2=$size[1]+1;
imagecopyresized ($im2, $im, 0, 0, 0, 0, $breit, $hoch, $breit2, $hoch2);
$file3=substr("$file", 0,-4)."_tn.jpg";
imagejpeg ($im2, $file3, 75);
unlink ($file);
$newsize=GetImageSize ($file3)

?>
<img src="norden_tn.jpg" alt="" width="<?echo $newsize[0];?>" height="<?echo $newsize[1];?>" border="0">

hakre

3:18 pm on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok then maybe convert has a parameter for this? and if not, then a ghostcript only conversion has to be done with the 'papersize' set to pixels and output device to jpeg.

so what about convert?

rolan

3:36 pm on Jan 22, 2003 (gmt 0)

10+ Year Member



imagemagick man means yes. like

convert -resize 120x120

but it doesnt work. maybe becoz of the pdf conversion.
i dunno. anyway this will do it for my job.

thanks for your help.

roland

andreasfriedrich

3:47 pm on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use
convert -resize 200 cv.pdf[0] cv.jpg
to create a jpeg image that is 200 pixel high. This is a lot cleaner than using the php code you posted.

Andreas

<added after I read your post roland>using the resize option works for me.</added>

rolan

4:20 pm on Jan 22, 2003 (gmt 0)

10+ Year Member



youre right i would like to use imagemagick internal
resize option maybe my host admin made a failure with imagemagick.

this code don´t work:
exec("/usr/bin/convert -resize 200 norden.pdf[0] norden.jpg");

this is working:
exec("/usr/bin/convert norden.pdf[0] norden.jpg");

?
any ideas
roland

hakre

4:30 pm on Jan 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yes roland i found something:

convert -size 120x120 file.pdf -resize 120x120 thumbnail.jpg

maybe you'll have to add the [0] at the end of file.pdf again, but i don't know. i've taken it form the documentation [imagemagick.org].