Reputation: 28913
I've worked out how to make nice thumbnails from single-page PDFs. But as my below code shows the best I can do for 2+ page PDFs is to choose to do the first page:
$w=320;$h=240;
$fname="my.pdf";
$saveFname = "mypdf.jpg";
$im = new \imagick($fname);
if($im->getNumberImages()>=2){
$im->setiteratorindex(0); //Just do first page
}
$im->thumbnailImage($w,$h,/*bestfit=*/true,/*fill=*/true);
$im->writeImage($saveFname);
What I'd like to do is have pages 1 and 2 side-by-side, and making best use of the available space. (If there is an option to do tiling of all pages, or selected pages, then even better.)
I thought montageImage
might be the answer, but I cannot find a complete example, so could not work out how to specify the PDF pages to process.
Notes on above listing:
setiteratorindex()
is needed to select the page to process.Upvotes: 1
Views: 1094
Reputation: 207435
Modified Version
This may do what you want more simply!
montage -density 288 input.pdf[0,1] -resize 50% -mode Concatenate -tile 2x thumb.jpg
Original Solution
I am not sure how you would do it in php
, but here is a script that does what I think you want at the command line with ImageMagick:
#!/bin/bash
PDF=$1
TMPA="TMPA-$$.JPG"
TMPB="TMPB-$$.JPG"
out=1
#
# Get number of pages in PDF
NUMPAGES=$(identify "$PDF" | wc -l)
echo $PDF has $NUMPAGES pages
#
# Iterate over all pages, two at a time
for ((p=0;p<$NUMPAGES;))
do
convert -density 288 a.pdf[$p] -alpha remove -resize 50% "$TMPA"
((p++))
convert -density 288 a.pdf[$p] -alpha remove -resize 50% "$TMPB"
((p++))
convert +append "$TMPA" "$TMPB" thumb${out}.jpg
((out++))
rm "$TMPA" "$TMPB" 2> /dev/null
done
Save as thumbnailer
and use like this:
chmod +x thumbnailer
./thumbnailer xyz.pdf
It produces a "twos-up" output of the PDF, in thumbnail files thumb{1..n/2}.jpg
Hopefully there are some hints in there as to what parameters to use and what functions to call in `php.
Upvotes: 3