Create PDF thumbnails with imagecache and ImageMagick while GD is still the default toolkit

Last updated on
30 April 2025

This tutorial can be considered as a continuation of http://drupal.org/node/460132.

Its purpose is to create single-page and double-page thumbnails of PDF files:

  • without selecting ImageMagick as the default toolkit, as GD is often the preferred toolkit
  • using imagecache so that the creation process is automated

Example

An example of result can be seen here.

The libraries you need on your server:

  • ImageMagick
  • Ghostscript

The Drupal modules you need to install and enable:

  • Content Construction Kit Module
  • FileField Module
  • ImageAPI: enable both ImageAPI GD2 and ImageAPI ImageMagick
  • ImageCache 6.x-2.0-beta10
  • ImageCache Actions: enable Imagecache Custom Actions too
  • FileField Paths, Pathauto and Transliteration

Instructions

  1. Select GD2 as your default toolkit
  2. Patch ImageCache 6.x-2.0-beta10 with this patch
  3. Create a new imagecache preset with a custom imagecache action to generate a single-page thumbnail. Code is: (do not include the <?php ?> tag)
    <?php
    $w = 246; // change to your preferred thumbnail width
    if (!_imageapi_imagemagick_convert($image->source.'[0]', $image->source.'.png', array(0 => '-thumbnail '.$w))) return FALSE;
    $img = imagecreatefrompng($image->source.'.png');
    file_delete($image->source.'.png');
    $image->resource = $img;
    $image->toolkit = 'imageapi_gd';
    $image->info = array('extension' => 'jpeg');
    return TRUE;
    ?>
    
  4. Create a new imagecache preset with a custom imagecache action to generate a double-page thumbnail. Code is: (do not include the <?php ?> tag)
    <?php
    $w = 256; // change to your preferred thumbnail width (this is the width of *one* page)
    if (!_imageapi_imagemagick_convert($image->source.'[0]', $image->source.'0.png', array(0 => '-thumbnail '.$w))) return FALSE;
    if (!_imageapi_imagemagick_convert($image->source.'[1]', $image->source.'1.png', array(0 => '-thumbnail '.$w))) return FALSE;
    $data = getimagesize($image->source.'0.png');
    $h = $data[1];
    $img1 = imagecreatefrompng($image->source.'0.png');
    $img2 = imagecreatefrompng($image->source.'1.png');
    $img3 = imagecreatetruecolor($w * 2, $h);
    imagecopy($img3, $img1, 0, 0, 0, 0, $w, $h);
    imagecopy($img3, $img2, $w, 0, 0, 0, $w, $h);
    imagedestroy($img1);
    imagedestroy($img2);
    file_delete($image->source.'0.png');
    file_delete($image->source.'1.png');
    $color = imagecolorallocate($img3, 0xBE, 0xC0, 0xC2); //grey line
    imageline($img3, $w, 0, $w, $h, $color);
    imagecolordeallocate($img3, $color);
    $image->resource = $img3;
    $image->toolkit = 'imageapi_gd';
    $image->info = array('extension' => 'jpeg');
    return TRUE;
    ?>
    
  5. Add a FileField to a content type so that you can upload PDF files
  6. Ensure that FileField Path settings are set so that file's path will be properly sanitized
  7. Set display of this field to the desired thumbnail preset

Et voilà !

See also

PDF to ImageField is a module that solves this sort of task without custom code

Help improve this page

Page status: Not set

You can: