NextGen Gallery PDF Support for this Wordpress Plugin

Da My-Lab.

NextGen Gallery is a very great plugin for Wordpress. Let's add to it another capability: the ability do build pdf thumbnail. Files involved are:

wp-content/plugins/nextgen-gallery/admin/functions.php

where you'll have to replace inside function create_thumbnail, that call to

 $thumb->save

to this more sophisticated call. It actually forces thumbnail to be saved in jpeg format, despite it ends with a .pdf; and select first page of a pdf, if it has multiple pages:

                       // save the new thumbnail
                       // ilcorsaronero@gmail.com MARK
                       $path_info = pathinfo($image->thumbPath);
                       if ($path_info['extension'] == "pdf")
                       {
                               @$thumb->save($image->thumbPath.'.jpg', $ngg->options['thumbquality']);
                               if (is_file($image->thumbPath.'.jpg'))
                               {
                                       rename($image->thumbPath.'.jpg',$image->thumbPath);
                               }
                               elseif (is_file($image->thumbPath.'-0.jpg'))
                               {
                                       rename($image->thumbPath.'-0.jpg',$image->thumbPath);
                               }
                       }
                       else
                       {
                               $thumb->save($image->thumbPath, $ngg->options['thumbquality']);
                       }


of course you'll have to enable pdf uploads. This is done by replacing:

$ext = array('jpeg', 'jpg', 'png', 'gif');

with

$ext = array('jpeg', 'jpg', 'png', 'gif','pdf');

everytime it occours inside functions.php file.

Next, last trick: leave out

@getimagesize

check, in functions.php; so that:

if ( !in_array($filepart['extension'], $ext)) || !@getimagesize($temp_file) ){

becomes

if ( !in_array($filepart['extension'], $ext)){ // || !@getimagesize($temp_file) ){

and

if (!@getimagesize($gallerypath . '/' . $picture) ) {
                               unset($new_images[$key]);
                               @unlink($gallerypath . '/' . $picture);          
                       }

becomes:

/* if (!@getimagesize($gallerypath . '/' . $picture) ) {
                               unset($new_images[$key]);
                               @unlink($gallerypath . '/' . $picture);          
                       } */

(getimagesize does not work with pdf file, hence we have to skip this step, or it should be performed in another way, such as through ImageMagick). Of course you have to use imagemagick as thumnailer, not gd embedded php library (this might pose some problem if you're on a shared hosting, otherwise, apt-get it and configure it trough NextGen Gallery -> Options -> Select graphic library: -> ImageMagick). Last but not least, don't use a picture highlighter, such as shadowbox, or jquery stuff like that: they won't work with pdf files, and you will end up showing nothing but the little preview out there.

Thumbnail cropping and resizing does not work with pdf, but believe me, it will be nice and clean in spite of all.

For those who love fancy boxes

If you still want to enjoy fancy boxes on the rest of your site, here is the howto. Just walk into your own theme folder (such as wp-content/themes/eNews) and create a folder named

nggallery

Inside it copy the file:

wp-content/plugins/nextgen-gallery/view/gallery.php

to

nggallery/gallery-pdf.php

And in this new copy, delete this code, where you find it:

<?php echo $image->thumbcode ?>

Next, when you'll want to show a pdf gallery, just remember to call nextgen this way:

[nggallery id=1 template=pdf]