Creating A Thumbnail Of A Word Document With PHP And LiveDocx

Saturday, September 24, 2011 - 15:25

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.

1
2
3
4
5
6
7
require_once('Zend/Service/LiveDocx/MailMerge.php');
 
$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
 
// Login to LiveDocx
$phpLiveDocx->setUsername('yourlivedocxusername')
            ->setPassword('yourlivedocxpassword');

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
 
require_once('Zend/Service/LiveDocx/MailMerge.php');
 
$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
 
// Login to LiveDocx
$phpLiveDocx->setUsername('yourlivedocxusername')
            ->setPassword('yourlivedocxpassword');
 
// Transmit the file we need to convert
$phpLiveDocx->setLocalTemplate('documenttoconvert.doc');
 
// Convert file
$phpLiveDocx->createDocument();
 
// 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);
}
Category: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

Add new comment