Images on templates with PHPDocX

  • May 03, 2011

Learn to change a picture or photo in a template with PHPdocx.
In this example we show you how simple it is with PHPDocX to include photos in a document template at “run time”.
One may need to show a photo that may change in time, for example, some stock chart that we may download from an online service. Or simply we may want to create a list of personnel that may change in time and we want to include their picture…
This how to do it.
We should first include a “placeholder” image with the properties we would like our “dynamical photo” to have: size, format, etcetera.
Afterwords we should include the variable we want to parse with PHPDocX as an “alt text” for the image.
If you do not know how to insert an alternative text for an image with Word the required steps are :

Right click on the image
Choose “Size” from the context menu
Click on the “Alternative text” tab of the popup window
Fill the input text with, for example, $IMAGE$ (although you may use any other variable name).

In this case the PHPDocX method fAddTemplateImage(‘IMAGE’, ‘logo_php.gif’ ) searches for an existing image in the document with the alternative text ‘$IMAGE$’ and replace it with logo_php.gif .
In this example we will just include yet another photo of our logo ☺. (download the template here)

require_once '../../classes/';

$objDocx = new CreateDocx();


$objDocx->addTemplateImage('IMAGE', '../files/img/logo_phpdocx.gif');