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 or GIF 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 neither downloaded nor added. Instead, the method shows the image from the remote content. If the image is not available in the remote or local server, such as cases as when you use localhost as host or if the file is deleted, the image won't appear on the DOCX.
Run the parameter downloadImages to download and insert the image on a 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.
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.