Global navigation

   Documentation Center
   eZ Studio & eZ Platform
     User Manual
     Technical Manual
     Glossary
   eZ Publish 4.x / legacy

 
eZ Publish (5.x)

eZ Publish 5.x | For eZ Platform & eZ Studio topics see Technical manual and User manual, for eZ Publish 4.x and Legacy topics see eZ Publish legacy

Skip to end of metadata
Go to start of metadata

FieldType identifier: ezimage
Validators: File size
Value object: \eZ\Publish\Core\FieldType\Image\Value
Associated Services: ezpublish.fieldType.ezimage.variation_service

The image FieldType allows you to store an image file.

A variation service handles conversion of the original image into different formats and sizes through a set of pre-configured named variations: large, small, medium, black & white thumbnail...

Value object

The value property of an Image Field will return an \eZ\Publish\Core\FieldType\Image\Value object with the following properties:

PropertyTypeExampleDescription
idstring0/8/4/1/1480-1-eng-GB/image.png

The image's unique identifier. Usually the path, or a part of the path. To get the full path, use URI property.

alternativeTextstringThis is a piece of textThe alternative text, as entered in the field's properties
fileNamestringimage.pngThe original image's filename, without the path
fileSizeint37931The original image's size, in bytes
uristringvar/ezdemo_site/storage/images/0/8/4/1/1480-1-eng-GB/image.pngThe original image's URI
imageIdstring240-1480A special image ID, used by REST

Using the variation Service, variations of the original image can be obtained. Those are \eZ\Publish\SPI\Variation\Values\ImageVariation objects with the following properties:

PropertyTypeExampleDescription
widthint640The variation's width in pixels
heightint480The variation's height in pixels
namestringmediumThe variation's identifier
infomixed Extra info, such as EXIF data
fileSizeint  
mimeTypestring  
fileNamestring  
dirPathstring  
uristring The variation's uri
lastModifiedDateTime When the variation was last modified

FieldType options

The Image FieldType supports one FieldDefinition option: the maximum size for the file.

Using an Image Field

In templates

When displayed using ez_render_field, an Image Field will output this type of HTML:

ez_render_field accepts several parameters:

  • which variation must be used can be set using the alias parameter
  • size and height can be forced for the image, for instance to handle custom screen densities. If not specified, the variation's attributes will be used if available.

The raw Field can also be used if needed. Image variations for the Field's content can be obtained using the ez_image_alias Twig helper:

The variation's properties can be used to generate the required output:

 

With the REST API

Image Fields within REST are exposed by the application/vnd.ez.api.Content media-type. An Image Field will look like this:

inputUri

From 5.2 version, new images must be inputed using the inputUri property from Image\Value.

The keys id and path still work, but a deprecation warning will be thrown.

Version >= 5.2
Before 5.2  Expand source

 

Children of the fieldValue node will list the general properties of the Field's original image (fileSize, fileName, inputUri...), as well as variations. For each variation, an uri is provided. Requested through REST, this resource will generate the variation if it doesn't exist yet, and list the variation details:

From PHP code

Getting an image variation

The variation service, ezpublish.fieldType.ezimage.variation_service,  can be used to generate/get variations for a Field. It expects a VersionInfo, the Image Field and the variation name, as a string (large, medium...)

Manipulating image content

From PHP

As for any Fieldtype, there are several ways to input content to a Field. For an Image, the quickest is to call setField() on the ContentStruct:

In order to customize the Image's alternative texts, you must first get an Image\Value object, and set this property. For that, you can use the Image\Value::fromString() method, that accepts the path to a local file:

You can also provide an hash of Image\Value properties, either to setField(), or to the constructor:

From REST

The REST API expects Field values to be provided in a hash-like structure. Those keys are identical to those expected by the Image\Value constructor: fileName, alternativeText. In addition, image data can be provided using the data property, with the image's content encoded as base64.

Creating an image Field

Updating an image Field

Updating an Image Field requires that you re-send existing data. This can be done by re-using the Field obtained via REST, removing the variations key, and updating alternativeText, fileName or data. If you do not want to change the image itself, do not provide the data key.

Naming

Each storage engine determines how image files are named.

Legacy Storage Engine naming

Images are stored within the following directory structure:

<varDir>/<StorageDir>/<ImagesStorageDir>/<FieIdId[-1]>/<FieIdId[-2]>/<FieIdId[-3]>/<FieIdId[-4]>/<FieldId>-<VersionNumber>-<LanguageCode>/

With the following values:

  • VarDir = var (default)
  • StorageDirstorage (default)
  • ImagesStorageDir = images (default)
  • FieldId = 1480
  • VersionNumber = 1
  • LanguageCode = eng-GB

Images will be stored to web/var/ezdemo_site/storage/images/0/8/4/1/1480-1-eng-GB.

Using the field ID digits in reverse order as the folder structure maximizes sharding of files through multiple folders on the filesystem.

Within this folder, images will be named like the uploaded file, suffixed with an underscore and the variation name:

  • MyImage.png
  • MyImage_large.png
  • MyImage_rss.png

Changelog

VersionDescription
5.2adding inputURI for input by API
5.4
2014.11
id property contains only the path of the image, not the full path

 

 

1 Comment

  1. Unknown User (krieftm)

    If you want to pass the VariationService (ezpublish.fieldType.ezimage.variation_service) by service.yml to the constructor of your class, you can use the class

    eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator

     for type-hinting. The service is an alias for this class.