Insert images with HTML
The embedHTML method transforms HTML and CSS to DOCX, preserving contents and format. One of the most widely used tags of the method is img, which allows to place images on documents. phpdocx by default needs the attribute src (the one that specifies the route to the image) to be a direct access to an image with JPG, PNG, GIF or SVG extension.
Image file with absolute URL
The following script adds an HTML code with an image referenced by an absolute URL, that is, a URL that includes its domain and name:
As there are no parameters for embedHTML, the image is downloaded and added to the document.
Set the parameter downloadImages as false to not download the image, so it's added as external file:
Setting downloadImages as false, if the image is not available in the remote address, such as cases as when you use localhost as host or if the file is deleted, the image won't appear in the document
Image file with a relative path
The embedHTML method also lets you download images by stating its host in the baseURL option:
With this option, the src attribute of the img tag must not include the host, just the relative path to the image. The baseURL option is especially helpful if there are images and other contents in CSS without a relative URL.
Image file as a stream
You can add any image available as a stream with the same process, regardless of its origin. This means that instead of typing a URL as in the previous examples, it is possible to add existing images, for example in a file system:
You can call an image from an FTP, too:
There's a comprehensive documentation regarding how to work with streams, available on the PHP official site: http://php.net/manual/en/book.stream.php.
base64 images are also supported in the src attribute.
As a stream
The previously reviewed scripts add images with JPG, JPEG, GIF or PNG extension. Another frequent use case is to run a PHP script to return the image you want to insert, e.g.:
In this use case, the script of the src parameter must return the image content. The most simple example of get-image.php would be like this:
You can run the output buffer methods as well:
Images can be located in a database, streams or somewhere else. The PHP script of the src attribute of the img tag must return the image you want to place.
Downloading images from secure URLs (HTTPS) may require a valid certificate in the PHP and OpenSSL setup: SSL error using file_get_contents.
It may also happen to be necessary to change the OpenSSL default setup. The following option configures the predefined security level in the openssl.cnf file (commonly placed in the directory /etc/ssl):
...to which we assign a lower security level:
...or else just comment the line:
phpdocx 10 added SVG support when transforming HTML to DOCX. This feature requires ImageMagick.
All licenses allow adding SVG contents using img tags using a file or a base64 content:
Premium licenses allow adding SVG contents using img tags as Basic and Advanced licenses, and also as inline tags using HTML Extended:
Scale image factor
phpdocx uses a default scale image factor of 7200 when adding images from HTML to adjust their dimensions. If needed, this value can be customized using the static variable HTML2WordML::$scaleImageFactor.
The following code sets 9600 as the scale image factor value applied to the imported images: