Creating A Thumbnail Of A Word Document With PHP And LiveDocx

In any list of documents in a system it is a good idea to add some thumbnails of the document so that your users can get a quick overview of what a document looks like before downloading it. This is a good alternative to just displaying an icon of the document type.

Creating Word document icons is very simple thanks to a service called LiveDocx. LiveDocx was created as a web service to allow the easy creation of most document formats from a simple template. However, it is possible to send a normal Word document as the template file and get an image of the file in return.

The best way to use LiveDocx with PHP is to use the LiveDocx class contained within Zend Framework. You can use this outside of a Zend Framework application by including the file Zend/Service/LiveDocx/MailMerge.php at the top of your script.

Note: You will need to register an account with LiveDocx before you can use the service. They have a good free service option that will allow you to create a limited number of images per day. To log into LiveDocx using the Zend Framework classes you just create a new instance of Zend_Service_LiveDocx_MailMerge and set the username and password fields. You are logged in automatically when ever you communicate with the LiveDocx service.


$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();

// Login to LiveDocx

To get an image of a Word document you must first send it to LiveDocx by using the method setLocalTemplate(). The file we are using isn't really a template, but it will still be understood by the service in a way that we can use. Once the template is in place you can call the createDocument() method to get LiveDocx to process the template. Once the template is processed we can then extract one or more images that we can then turn into thumbnails. The method getBitmaps() is used to convert the document we created into an image file, which we can then save. The getBitmaps() method takes the start and end pages, the zoom factor and the image type to be created (mulitple formats are available). The zoom factor is a percentage value that dictates how large the returned image should be. This is a percentage of the total image size of 794 pixels wide and 1123 pixels high. An alternative to this is to generate the full size image and then convert it into a thumbnail afterwards. I find this a slightly better alternative as it means you can then crop (or apply any effect you want) to the image when generating the thumbnail.

The code below shows the creation of an image from a Word document, and then the conversion of that image into a thumbnail using the PHP GD2 library. The thumbnail created is 250 pixels square and therefore crops the original rectangular image.



$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();

// Login to LiveDocx

// Transmit the file we need to convert

// Convert file

// Get image for the document
// (fromPage, toPage, zoomFactor, format)
$bitmaps = $phpLiveDocx->getBitmaps(1, 1, 100, 'png');

// Create a holder for the thumbnail of a certain size
$thumb_width = 250;
$thumb_height = 250;
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);

// Create a thumbnail for the single image
foreach ($bitmaps as $pageNumber => $bitmapData) {
    // Create filename
    $filename = sprintf('documentPage%d.png', $pageNumber);

    // Get image as GD2 data
    $doc_image = imagecreatefromstring($bitmapData);

    // Resize image
    imagecopyresampled($thumb, $doc_image, 0, 0, 0, 0, $thumb_width, $thumb_height, 794, 1123);

    // Save image
    imagepng($thumb, $filename);


Add new comment

The content of this field is kept private and will not be shown publicly.
1 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.