View source
<?php
function imagecache_token_token_info() {
$styles = image_styles();
$styles_tokens = array();
$image_style = array();
foreach ($styles as $style => $desc) {
$styles_tokens[$style] = array(
'name' => $style,
'description' => t("@s image style", array(
"@s" => $style,
)),
'type' => 'image-style',
);
foreach (_imagegecache_token_image_attributes() as $attribute => $none) {
$image_style[$attribute] = array(
'name' => $attribute,
'description' => t("@s image style attribute: @a", array(
"@s" => $style,
"@a" => $attribute,
)),
);
}
}
return array(
'types' => array(
'image-field' => array(
'name' => 'Image Field',
'description' => 'Image Field',
'needs-data' => 'image-field',
),
'image-style' => array(
'name' => 'Image Style',
'description' => 'Image Style',
'needs-data' => 'image-style',
),
),
'tokens' => array(
'image-field' => $styles_tokens,
'image-style' => $image_style,
),
);
}
function imagecache_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'entity' && !empty($data['entity'])) {
$fields = field_info_field_map();
$supported = variable_get('imagecache_token_fields', array());
$supported = array_filter($supported);
foreach ($fields as $field_name => $field) {
$field['field_name'] = $field_name;
if ($field['type'] == 'image' || $field['type'] == 'file' || $field['type'] == 'media') {
if ($field['type'] == 'image') {
$is_supported = TRUE;
}
elseif (empty($supported)) {
continue;
}
else {
$is_supported = FALSE;
foreach ($field['bundles'] as $entity_type => $bundles) {
foreach ($bundles as $bundle) {
$key = $field['type'] . ':' . $field['field_name'] . ':' . $entity_type . ':' . $bundle;
if (!empty($supported[$key])) {
$is_supported = TRUE;
break 2;
}
}
}
if (!$is_supported) {
continue;
}
}
if (($image_field_tokens = token_find_with_prefix($tokens, $field['field_name'])) && ($field_object = field_get_items($data['entity_type'], $data['entity'], $field['field_name']))) {
$replacements += token_generate('image-field', $image_field_tokens, array(
'image-field' => $field_object,
), $options);
}
}
}
}
$images_style_data =& drupal_static(__FUNCTION__);
if ($type == 'image-field' && !empty($data['image-field'])) {
foreach ($tokens as $token => $original) {
$output = array();
$explode = explode(':', $token);
$type = isset($explode[0]) ? $explode[0] : FALSE;
$attribute = isset($explode[1]) ? $explode[1] : FALSE;
if (in_array($attribute, array(
'first',
'second',
'third',
'last',
)) || is_numeric($attribute)) {
switch ($attribute) {
case 'first':
$index = 0;
break;
case 'second':
$index = 1;
break;
case 'third':
$index = 2;
break;
case 'last':
$index = sizeof($data['image-field']) - 1;
break;
default:
$index = $attribute;
}
if (isset($data['image-field'][$index]) && !empty($data['image-field'][$index]['uri'])) {
if (isset($explode[3]) && $explode[3] === 'relative') {
$url = parse_url(image_style_url($type, $data['image-field'][$index]['uri']));
$output[] = $url['path'];
}
else {
$output[] = image_style_url($type, $data['image-field'][$index]['uri']);
}
$styled_uri = image_style_path($type, $data['image-field'][$index]['uri']);
_imagecache_token_generate_image($styled_uri, $type, $data['image-field'][$index]['uri']);
}
$replacements[$original] = implode('', $output);
}
else {
foreach ($data['image-field'] as $field) {
if (isset($attribute) && !empty($attribute)) {
$styled_uri = image_style_path($type, $field['uri']);
_imagecache_token_generate_image($styled_uri, $type, $field['uri']);
switch ($attribute) {
case 'render':
$output[] = theme('image_formatter', array(
'item' => $field,
'image_style' => $type,
));
break;
case 'mimetype':
case 'width':
case 'height':
case 'extension':
case 'filesize':
if (empty($images_style_data[$styled_uri])) {
$images_style_data[$styled_uri] = image_get_info($styled_uri);
}
switch ($attribute) {
case 'width':
$output[] = $images_style_data[$styled_uri]['width'];
break;
case 'height':
$output[] = $images_style_data[$styled_uri]['height'];
break;
case 'mimetype':
$output[] = $images_style_data[$styled_uri]['mime_type'];
break;
case 'extension':
$output[] = $images_style_data[$styled_uri]['extension'];
break;
case 'filesize':
$output[] = $images_style_data[$styled_uri]['file_size'];
break;
}
break;
case 'path':
if (isset($explode[2]) && $explode[2] === 'relative') {
$url = parse_url(image_style_url($type, $field['uri']));
$output[] = $url['path'];
}
else {
$output[] = image_style_url($type, $field['uri']);
}
break;
default:
if (isset($field[$attribute])) {
$output[] = $field[$attribute];
}
}
}
else {
$output[] = image_style_url($type, $field['uri']);
}
}
$replacements[$original] = implode(', ', $output);
}
}
}
return $replacements;
}
function _imagegecache_token_image_attributes() {
return array(
'style_name' => NULL,
'path' => NULL,
'path:relative' => NULL,
'width' => NULL,
'height' => NULL,
'extension' => NULL,
'filesize' => NULL,
'alt' => '',
'title' => NULL,
'mimetype' => NULL,
'render' => '',
'first' => '',
'second' => '',
'third' => '',
'last' => '',
'uri' => '',
);
}
function _imagecache_token_generate_image($styled_uri, $type, $field_uri) {
if (!file_exists($styled_uri)) {
$style = image_style_load($type);
global $imagecrop_style;
$imagecrop_style = $type;
image_style_create_derivative($style, $field_uri, $styled_uri);
}
}