addImage

Inserts an image into the Word document.

PRO, PRO+ and CORPORATE PHPDocX >= 1.0

Description

public addImage (array $options)

You may use this method to insert images (jpg, gif, png or bmp) into your Word document.

You may easily control:

  • the embedding and positioning of the image within a complex paragraph,
  • the size (if not explicitely given, PHPDocX tries to read the width and height from the image headers),
  • the dpi or dots per inch (by default they are taken from the image headers or set to 96 dpi),
  • the scale (default 100%) and
  • the target: main document (default), headers or footers.

Together with some general formatting options.

If one wishes to insert an image within a complex paragraph or a table one should use this method in combination with the addText and the addTable method.

Parameters

options

The posible keys and values are (required options are shown with yellow background):

keyTypeDescription
name string Image path : ‘img/image.png’, …
sizeX int Image width : 1000, …
sizeY int Image height : 1000, …
spacingTop int Spacing top : 100, …
spacingBottom int Spacing bottom : 100, …
spacingLeft int Sacing left : 100, …
spacingRight int Spacing right : 100, …
textWrap int

Text wrap:

  • 0 (inline)
  • 1 (square)
  • 2 (front)
  • 3 (back)
  • 4 (top and bottom)
  • 5 (clear)
jc string Aligment : right, center,...
border bool Border discontinuous : 0, 1.
borderDiscontinuous int Border size : 1,2,3 …
scaling int % of size: 50, 100…
dpi int Dots per inch.
font string Font family, for example: ‘Times New Roman’, ‘Arial’,…
target string Document (default value), defaultHeader, firstHeader, evenHeader, defaultFooter, firstFooter, evenFooter. One should use this parameter in conjubction with the ‘rawWordML’ option to later insert the image in a header or footer.
float string (left, right, center) floating image. It only applies if textWrap is not inline (default value).
horizontalOffset int Given in emus (1cm = 360000 emus). Only applies if there is the image is not floating.
verticalOffset int Given in emus (1cm = 360000 emus)
rawWordML bool If true one may store the output into a variable.

Return values

If the option rawWordML is set to true (default value is false) it returns a string with the raw WordML code, else returns nothing.

Code samples

Example #1: inserts an image

require_once 'classes/CreateDocx.inc';

$docx = new CreateDocx();

$options = array(
    'name' => '../files/img/image.png',
    'scaling' => 50,
    'spacingTop' => 100,
    'spacingBottom' => 0,
    'spacingLeft' => 100,
    'spacingRight' => 0,
    'textWrap' => 1,
    'border' => 1,
    'borderDiscontinuous' => 1
);

$docx->addImage($options);

$docx->createDocx('example_image');

The resulting Word document looks like (download .docx file):

Change log

  • Available since 1.0
  • One may choose the dpi and other config options since 2.5.
  • rawWordML option included in 3.0

See also