Practical phpxlsx

Images

Introduction

Adding images to spreadsheets requires using the addImage method setting the position and the number of rows and columns to fill.

Valid format files for working with images are JPG, PNG, and GIF. Base64 images can also be added. SVG contents can be added using addSvg.

If you use a template, you can replace an existing image using the replaceVariableImage method.

Adding images

The method for adding new images in the active sheet is addImage:

This method includes the following parameters:

  • $image: image path, base64 content or stream to be added
  • $position: cell position in the active sheet (A1, C3, AB7...)
  • $options: array to set image sizes, editAs type, hyperlink...

The following sample code adds an image in the B5 position with column and row sizes:

Backgrounds

If instead of inserting an image in the active sheet you want to place it as background, use addBackgroundImage:

You have to indicate the path, base64 or stream of the image in it. For example:

Replacing images in a template

As explained on Working with templates, phpxlsx includes the replaceVariableImage method to replace images in templates.

Next - Charts