Image operators
Three operators of eZTemplateImageOperator (lib/eztemplate/classes/eztemplateimageoperator.php), registered
through lib/eztemplate/classes/eztemplateautoload.php, work with images through PHP's GD extension (the class checks
for ImageTTFBBox, ImageCreate, ImageTTFText, ImagePNG and the other functions it needs, and logs an error
instead of working when one is missing):
texttoimagerenders a text as a PNG image with a TrueType font;imagecombines layers into one image;imagefilereads an existing image file.
They have nothing to do with the images of content objects, which come from the image datatype and its aliases.
All three return an image object (eZImageLayer, eZImageTextLayer or eZImageObject), which prints as an <img>
tag and offers the attributes filepath, filename, width, height, alternative_text, imagepath and
has_size.
Contents
Settings: texttoimage.ini
settings/texttoimage.ini holds the paths and the font classes:
| Setting | Value on a development installation | Meaning |
|---|---|---|
[ImageSettings] UseCache | enabled | reuse an image already rendered with the same text and settings |
[PathSettings] FontDir[] | design/standard/fonts | where the .ttf fonts are looked for |
[PathSettings] CacheDir | var/cache/texttoimage | where rendered images are written |
[DefaultSettings] | Family=1942, PointSize=40, white background, black text | used when a class does not say otherwise |
[1942], [a_d_mono], [sketchy], [smartie] ... | font family and adjustments | the classes a template can name |
texttoimage
texttoimage
Renders the piped text with the font settings of a class, overridden by any parameter given. The image is written to
<CacheDir>/imagetext/ under a name made from an MD5 of the text and the settings; with UseCache an existing file is
reused.
| Parameter | Type | Required | Default |
|---|---|---|---|
class | string, a section of texttoimage.ini | no | 'default' (falls back to [DefaultSettings]) |
family | string, font file name without .ttf | no | from the class |
pointsize | integer | no | from the class |
angle | integer | no | from the class |
bgcolor | color (#rrggbb or an array of three numbers) | no | from the class |
textcolor | color | no | from the class |
x, y, w, h | integer adjustments | no | from the class |
usecache | boolean, or a string used as the cache name | no | [ImageSettings] UseCache |
storeimage | boolean | no | true |
Input: the text, piped. Returns an eZImageTextLayer. When the font is not found it returns nothing; when the font
cannot be opened it logs "Could not open font". Generated reference:
texttoimage.
It writes files. The template below was rendered once on a development installation while this page was prepared, and it created the
directory var/cache/texttoimage/imagetext/ with the PNG file (a cache directory: it can be cleared, and the image is
made again on the next request):
'Fit & Healthy'|texttoimage( '1942' )Its output:
<img border="0" src="/var/cache/texttoimage/imagetext/b/d/3/bd3fdc7ef41d4f843b93d8d9703be5d4/Fit_amp_Healthy.png" alt="Fit &amp; Healthy" width="450" height="52" />Note the alt text: the operator escapes the text with htmlspecialchars() when it creates the layer
($alternativeText), and the <img> output escapes it again, so & arrives as &amp;. Pass text without
characters that need escaping, or write the <img> tag yourself from imagepath, width and height.
image
image
Builds one image from layers. Every parameter is either a string, which becomes the alternative text, or an array of
a layer and a hash of placement options: halign (left, right, center), valign (top, bottom, center),
x, y, xrel, yrel and transparency. The result is written to <CacheDir>/imageobject/ (reused with
UseCache).
| Parameter | Type | Required | Default |
|---|---|---|---|
| layers and alternative text | array( layer, hash( options ) ) or string, any number | yes | none |
Input: none. Returns an eZImageObject. Generated reference: image.
It writes files, like texttoimage. This template, also rendered once on a development installation, created
var/cache/texttoimage/imageobject/:
image( array( 'Fit & Healthy'|texttoimage( '1942' ), hash( 'halign', 'center', 'valign', 'center' ) ), 'Fit and Healthy' )Its output:
<img border="0" src="/var/cache/texttoimage/imageobject/5/e/1/5e1e308e65728a71d3bcfdcfd00dbd81/Fit_and_Healthy.png" alt="Fit and Healthy" width="450" height="52" />imagefile
imagefile
Reads an image file and returns it as a layer: eZImageLayer::createForFile() reads the size with getimagesize()
and loads the image into memory. Nothing is written. The alternative text is the file name without its extension.
| Parameter | Type | Required | Default |
|---|---|---|---|
filename | string, a path relative to the installation | yes | none |
options | array | no | array() |
Input: none. Returns an eZImageLayer, which can be used as a layer of image. Generated reference:
imagefile.
def $img = imagefile( 'design/standard/images/add.png' )
filepath: $img.filepath
filename: $img.filename
size: $img.width x $img.height (has_size: $img.has_size|choose( 'no', 'yes' ))
alternative_text: $img.alternative_textOutput:
filepath: design/standard/images
filename: add.png
size: 16 x 16 (has_size: yes)
alternative_text: addThe imagefile example gave the same output compiled on a development installation. The two writing examples were not run a second time,
interpreted or compiled.
Related pages
- Icon operators: icons for classes, MIME types and flags
- Image datatype examples: images of content objects
- All template operators (generated)
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.