Creating A Thumbnail Of A Word Document With PHP And LiveDocx

24th September 2011

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. require_once('Zend/Service/LiveDocx/MailMerge.php');
  2.  
  3. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  4.  
  5. // Login to LiveDocx
  6. $phpLiveDocx->setUsername('yourlivedocxusername')
  7. ->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. <?php
  2.  
  3. require_once('Zend/Service/LiveDocx/MailMerge.php');
  4.  
  5. $phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
  6.  
  7. // Login to LiveDocx
  8. $phpLiveDocx->setUsername('yourlivedocxusername')
  9. ->setPassword('yourlivedocxpassword');
  10.  
  11. // Transmit the file we need to convert
  12. $phpLiveDocx->setLocalTemplate('documenttoconvert.doc');
  13.  
  14. // Convert file
  15. $phpLiveDocx->createDocument();
  16.  
  17. // Get image for the document
  18. // (fromPage, toPage, zoomFactor, format)
  19. $bitmaps = $phpLiveDocx->getBitmaps(1, 1, 100, 'png');
  20.  
  21. // Create a holder for the thumbnail of a certain size
  22. $thumb_width = 250;
  23. $thumb_height = 250;
  24. $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  25.  
  26. // Create a thumbnail for the single image
  27. foreach ($bitmaps as $pageNumber => $bitmapData) {
  28. // Create filename
  29. $filename = sprintf('documentPage%d.png', $pageNumber);
  30.  
  31. // Get image as GD2 data
  32. $doc_image = imagecreatefromstring($bitmapData);
  33.  
  34. // Resize image
  35. imagecopyresampled($thumb, $doc_image, 0, 0, 0, 0, $thumb_width, $thumb_height, 794, 1123);
  36.  
  37. // Save image
  38. imagepng($thumb, $filename);
  39. }

 

Add new comment

The content of this field is kept private and will not be shown publicly.