Snippet: PHP Snippet Fetching ezimage datatype image alias properties

This is compatible with eZ Publish 4.

This is a Stub article. Help the eZ Publish community by expanding it!

Introduction

Did you know that you can use the eZ Publish kernel api to obtain image alias properties from content object attributes which use the ezimage datatype.

Example

This is a simple php example of how you may use the eZ Publish kernel api to obtain image alias properties. This example comes from a simple example custom module view.

<?php
 
/**
 * File image.php
 *
 * @package image.php
 * @version //autogentag//
 * @copyright Copyright (C) 2007 Brookins Consulting. All rights reserved.
 * @license http://www.gnu.org/licenses/gpl.txt GPL License
 */
 
$Module = $Params['Module'];
 
// START
 
// Assume this type of module/view url:
// www.site.com/myextension/mymodule/myview/1234
// 1234 maps to the view parameter, MyNode (eg: $Params["MyNode"])
 
// 0. Include the necessary files -- I would imagine that some are include
 
include_once( 'kernel/common/template.php' );
 
// 1. Create an instance of the database
$db = eZDB::instance();
 
// 2. Query the node via node id
$my_node = eZContentObject::fetchByNodeID( $Params["MyNode"] );
// $my_node = eZContentObject::fetchByNodeID( '86' );
 
// 3. Get a copy of the data_map
$my_node_data_map = $my_node->fetchDataMap();
 
// 4. Get a reference to the image
$my_node_image = $my_node_data_map["image"];
// $my_node_image_data = $my_node_image->DataText;
 
// Example Usage of the eZ Image Alias Handler Class / System
// include_once( 'kernel/classes/datatypes/ezimage/ezimagealiashandler.php' );
 
// 5. Fetch Alias Handler
$image_alias_handler = new eZImageAliasHandler( $my_node_image );
 
// 6. Fetch Alias Attributes*
 
// 7. Fetch Original Image Path as URI
$my_node_image_uri =  "/" . $image_alias_handler->attributeFromOriginal( 'url' );
 
// 8. Fetch Original Image Dimentions
$my_node_image_width = $image_alias_handler->attributeFromOriginal( 'width' );
$my_node_image_height = $image_alias_handler->attributeFromOriginal( 'height' );
 
// 9. Get the associated link
$my_node_link_object = $my_node_data_map["my_link"];
$my_node_link_url_object = eZURL::fetch( $my_node_link_object->DataInt );
$my_node_link_url=str_replace ( "/", "+", $my_node_link_url_object->attribute( 'url' ) );
 
// 10. Instantiate the template and set the template vars
$tpl = templateInit();
$tpl->setVariable( 'parameters', $Params );
$tpl->setVariable( 'my_node_image_path', $my_node_image_uri );
$tpl->setVariable( 'my_node_image_width', $my_node_image_width );
$tpl->setVariable( 'my_node_image_height', $my_node_image_height );
$tpl->setVariable( 'my_node_link_url', $my_node_link_url );
 
// END
 
// phpinfo();
eZExecution::cleanExit();
 
?>

References