Datamanager 2 Images type.

This type encapsulates a unlimited list of uploaded images each along with an optional number of derived images like thumbnails. Both the main image and the derived thumbnails will be run through a defined filter chain. The originally uploaded files can be kept optionally.

Similar to the downloads widget, the individual images are distinguished using md5 based hashes (not their actual filenames).

Image identifier naming conventions

The various images are distinguished using these suffixes to their identifiers:

The original image will be available with the "original" identifier prefix unless configured otherwise. The main image used for display is available as "main", which will be ensured to be web-compatible. (This distinction is important in case you upload TIFF or other non-web-compatible images. All derived images will be available under the prefixes defined in the schema configuration.

An optional "quick" thumbnail mode is available as well where you just specify the maximum frame of a thumbnail to-be-generated. The auto-generated image will then be available in the attachment identified as "thumbnail".

File type conversion and image filtering

The class uses the image filter system for the actual resizing and conversion work, you need to specify all operations including resize operations in the filtering chain declaration for the corresponding derived image.

Regarding file type conversions: The original uploaded image will always be run through an automatic type conversion as the very first step before any further processing is done. From that point, no further type conversion is done unless the user specifies another one in the filter chains for a derived type. A manual initial image conversion is not yet supported by the type.

All derived images will be computed from the initially converted, uploaded image, which should minimize the losses of the subsequent conversions. This intermediate image will not be kept an any place. The keep_original option will only save the unmodified, uploaded file.

The recreate_images call (not yet implemented) will recreate all derived images from the original image. If that image is not available, the generated main image is used instead.

Derived image naming warning

Be aware that the type holds no safety code to guard against duplicate image identifiers (e.g. defining a "main" image in the derived images list). The results of such a configuration is undefined.

In addition, you should only use lowercase alphanumeric letters to identify images, as other characters like underscores or spaces are reserved for type-internal usage.

Available configuration options:

  • boolean keep_original controls whether you want to keep the originally uploaded file available. This option is disabled by default.
  • string filter_chain The filter chain used to render the main image. This chain empty (null) by default.
  • array derived_images A list of derived images to construct from the main image. This option consists of a list of identifier/filter chain declarations. They will be constructed in order, each using a fresh copy of the (initially type-converted) original image. This options may be null (the default) indicating no derived images. Note, that the system will detect any explicit image type conversions you're doing in a filter chain, setting the attachments' Mime-Type property automagically.
  • array auto_thumbnail This array holds the maximum size of the thumbnail to create with automatic defaults. The array holds a maximum width/height pair as first and second element of the array, nothing else. The image will be available as "thumbnail". This image will be constructed after constructing all explicitly defined derived images. This option may be null (the default) indicating no thumbnail.
  • integer max_count Maximum number of images allowed for a field. Set this to zero or false for indefinite number
  • boolean sortable Should the attachments list be sortable. True if the sorting should be turned on, false if they should be sorted alphabetically by the title

Implementation note:

Due to the fact that Imagemagick is used for most operations, this type is currently only capable of operating based on actual files, not file handles.

todo Implement thumbnail interface.
todo Operation on file handles.
todo Derived-images recreation.
package midcom.helper.datamanager2

 Methods

_batch_handler ($extension, $file_data)

Parameters

$extension

$file_data

_info_heuristics (array $info, string $identifier)

Checks the info array for required mapping keys and regenerates them if missing

Parameters

$info

array&$info reference to the info array of given attachment

$identifier

string&$identifier reference to the attachments' identifier

_on_initialize ()

_sort_attachments ()

This override adds a sorting call for the images member, which is sorted by main image filename.

_sort_images_callback (array $a, array $b)

User-defined array sorting callback, used for sorting $images.

See the usort() documentation for further details.

access protected
todo compare based on metadata->score and filename (or title ???)

Parameters

$a

arrayThe first image list.

$b

arrayThe second image list.

Returns

intA value according to the rules from strcmp().

_store_att_map_parameters ($identifier, $attachment)

Overridden to add some of the mapping info this datatype needs backed up

_update_attachment_info ($identifier)

Adds information specific to this type to the attachment info block (based on the information in the $_attachment_map):
  • 'images_identifier' The identifier of the image set.
  • 'images_name' contains the image name (like 'thumbnail', 'main' etc.) used when defining the conversion rules.

In addition, this call will update the $images list as well.

If the attachment is not present in the _attachment_map, it will be skipped silently.

Parameters

$identifier

add_image (string $filename, string $tmpname, string $title)

Adds a new image to the list.

Unless specified, the function creates a unique image identifier based on the hash of the current timestamp, the uploaded filename and the name of the temporary upload file.

Parameters

$filename

stringThe name of the image attachment to be created.

$tmpname

stringThe file to load.

$title

stringThe title of the image.

Returns

booleanIndicating success.

apply_filter_images (string $images_identifier, string $filter)

Applies a given filter to all images (except original) under main identifier

Parameters

$images_identifier

stringidentifier for $this->images array

$filter

stringthe midcom_helper_imagefilter filter chain to apply

Returns

booleanindicating success/failure

convert_from_storage ($source)

First, we load the attachment_map information so that we can collect all images together.

Then we call the base class.

Parameters

$source

convert_to_html ()

The HTML-Version of the image type can take two forms, depending on type configuration:
  1. If an 'thumbnail' image is present, it is shown and encapsulated in an anchor tag leading to the 'main' image.
  2. If no 'thumbnail' image is present, the 'main' image is shown directly, without any anchor.

In case that there is no image uploaded, an empty string is returned.

convert_to_storage ()

Calls base class

delete_image (string $identifier)

Deletes the specified image.

Parameters

$identifier

stringThe identifier of the image to delete.

get_image_guids ($object, $field)

Get image GUIDs for a given DM2 schema field name and a given MidCOM DBA object
Static
see \http://trac.midgard-project.org/ticket/1806

Parameters

$object

$field

Returns

array

recreate ()

Recreate all images

recreate_derived_images ($force_prepare)

Recreates derived images
todo Implement

Parameters

$force_prepare

recreate_images ()

Recreates all images from either the "original" or the "main" image

recreate_main_image ()

Recreates main image if original is available
todo Implement

resolve_from_imagemap (string $source)

Resolve attachments map from imagemap
Static

Parameters

$source

string

Returns

stringAttachment map

rotate (string $images_identifier, string $direction)

Rotates applies a rotational filter to all images under given main identifier

Parameters

$images_identifier

stringimage to rotate

$direction

stringdirection to rotate to

Returns

booleanindicating success/failure

set_image (string $identifier, string $filename, string $tmpname, string $title)

Adds or updates an image to the type.

Loads and processes the $tmpname file on disk. The identifier is used to select the image in question.

Parameters

$identifier

stringThe image identifier to use.

$filename

stringThe name of the image attachment to be created.

$tmpname

stringThe file to load.

$title

stringThe title of the image.

Returns

booleanIndicating success.

update_image (string $identifier, string $filename, string $tmpname, string $title)

Updates an existing image.

Parameters

$identifier

stringThe image identifier to use.

$filename

stringThe name of the image attachment to be created.

$tmpname

stringThe file to load.

$title

stringThe title of the image.

Returns

booleanIndicating success.

update_image_title ($identifier, $title)

Parameters

$identifier

$title

_batch_handler_cleanup ($tmp_dir, $new_name)

Parameters

$tmp_dir

$new_name

_batch_handler_get_files_recursive ($path, $files)

Parameters

$path

$files

_save_image_listing ()

This saves a map of attachment identifiers to image identifier/name pairs.

It is updated accordingly on all save operations and complements the base type's _save_attachment_listing. It is stored to have a safe way of loading the images (heuristics could not be safe enough when subtypes create non-md5 based identifiers).

 Properties

 

array $_attachment_map

This array maps attachment identifiers to image identifier/derived image name pairs.

The main purpose of this member is for easier mapping between blobs and images.

The array is indexed by attachment identifier and contains arrays containing image identifier and derived image name consecutively (without any special indexes).

Updates are done on load of the types' data and on every attachment add/delete operation.

access protected
 

array $images

The list of images.

This is essentially a reordered version of the $attachments_info member: It is multi-dimensional, mapping the image identifier with all images available for that image. The actual images will only have the derived image name (like "thumbnail" as array key.

Example: Assume we have the images "original", "main" and "thumbnail" available for each image. While the $attachments and $attachments_info lists will just identify the images with their full handle, $attachments_info["{$md5identifier}_main"], this member will list the attachment as $images[$md5identifier]['main'] (as well as ...['original'] and ['thumbnail']).

The actual structures are references to the corresponding $_attachments_info entries.

see \$attachments_info
 

integer $max_count

Maximum amount of images allowed to be stored in the same field
 

string $output_mode

Output mode
 

boolean $sortable

Should the widget offer sorting feature
 

mixed $titles

Titles of each image, keyed by the MD5 identifier
 

string $_title

The current image title to use when operating on images.