View source
<?php
define('IMAGE_STORAGE_NORMAL', 1);
define('IMAGE_STORAGE_OVERRIDE', 2);
define('IMAGE_STORAGE_DEFAULT', 4);
define('IMAGE_STORAGE_EDITABLE', IMAGE_STORAGE_NORMAL | IMAGE_STORAGE_OVERRIDE);
define('IMAGE_STORAGE_MODULE', IMAGE_STORAGE_OVERRIDE | IMAGE_STORAGE_DEFAULT);
define('IMAGE_DERIVATIVE_TOKEN', 'itok');
require_once DRUPAL_ROOT . '/modules/image/image.field.inc';
function image_help($path, $arg) {
switch ($path) {
case 'admin/help#image':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Image module allows you to manipulate images on your website. It exposes a setting for using the <em>Image toolkit</em>, allows you to configure <em>Image styles</em> that can be used for resizing or adjusting images on display, and provides an <em>Image</em> field for attaching images to content. For more information, see the online handbook entry for <a href="@image">Image module</a>.', array(
'@image' => 'http://drupal.org/documentation/modules/image',
)) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Manipulating images') . '</dt>';
$output .= '<dd>' . t('With the Image module you can scale, crop, resize, rotate and desaturate images without affecting the original image using <a href="@image">image styles</a>. When you change an image style, the module automatically refreshes all created images. Every image style must have a name, which will be used in the URL of the generated images. There are two common approaches to naming image styles (which you use will depend on how the image style is being applied):', array(
'@image' => url('admin/config/media/image-styles'),
));
$output .= '<ul><li>' . t('Based on where it will be used: eg. <em>profile-picture</em>') . '</li>';
$output .= '<li>' . t('Describing its appearance: eg. <em>square-85x85</em>') . '</li></ul>';
$output .= t('After you create an image style, you can add effects: crop, scale, resize, rotate, and desaturate (other contributed modules provide additional effects). For example, by combining effects as crop, scale, and desaturate, you can create square, grayscale thumbnails.') . '<dd>';
$output .= '<dt>' . t('Attaching images to content as fields') . '</dt>';
$output .= '<dd>' . t("Image module also allows you to attach images to content as fields. To add an image field to a <a href='@content-type'>content type</a>, go to the content type's <em>manage fields</em> page, and add a new field of type <em>Image</em>. Attaching images to content this way allows image styles to be applied and maintained, and also allows you more flexibility when theming.", array(
'@content-type' => url('admin/structure/types'),
)) . '</dd>';
$output .= '</dl>';
return $output;
case 'admin/config/media/image-styles':
return '<p>' . t('Image styles commonly provide thumbnail sizes by scaling and cropping images, but can also add various effects before an image is displayed. When an image is displayed with a style, a new file is created and the original image is left unchanged.') . '</p>';
case 'admin/config/media/image-styles/edit/%/add/%':
$effect = image_effect_definition_load($arg[7]);
return isset($effect['help']) ? '<p>' . $effect['help'] . '</p>' : NULL;
case 'admin/config/media/image-styles/edit/%/effects/%':
$effect = $arg[5] == 'add' ? image_effect_definition_load($arg[6]) : image_effect_load($arg[7], $arg[5]);
return isset($effect['help']) ? '<p>' . $effect['help'] . '</p>' : NULL;
}
}
function image_menu() {
$items = array();
$directory_path = file_stream_wrapper_get_instance_by_scheme('public')
->getDirectoryPath();
$items[$directory_path . '/styles/%image_style'] = array(
'title' => 'Generate image style',
'page callback' => 'image_style_deliver',
'page arguments' => array(
count(explode('/', $directory_path)) + 1,
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['system/files/styles/%image_style'] = array(
'title' => 'Generate image style',
'page callback' => 'image_style_deliver',
'page arguments' => array(
3,
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['admin/config/media/image-styles'] = array(
'title' => 'Image styles',
'description' => 'Configure styles that can be used for resizing or adjusting images on display.',
'page callback' => 'image_style_list',
'access arguments' => array(
'administer image styles',
),
'file' => 'image.admin.inc',
);
$items['admin/config/media/image-styles/list'] = array(
'title' => 'List',
'description' => 'List the current image styles on the site.',
'page callback' => 'image_style_list',
'access arguments' => array(
'administer image styles',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
'file' => 'image.admin.inc',
);
$items['admin/config/media/image-styles/add'] = array(
'title' => 'Add style',
'description' => 'Add a new image style.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'image_style_add_form',
),
'access arguments' => array(
'administer image styles',
),
'type' => MENU_LOCAL_ACTION,
'weight' => 2,
'file' => 'image.admin.inc',
);
$items['admin/config/media/image-styles/edit/%image_style'] = array(
'title' => 'Edit style',
'description' => 'Configure an image style.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'image_style_form',
5,
),
'access arguments' => array(
'administer image styles',
),
'file' => 'image.admin.inc',
);
$items['admin/config/media/image-styles/delete/%image_style'] = array(
'title' => 'Delete style',
'description' => 'Delete an image style.',
'load arguments' => array(
NULL,
(string) IMAGE_STORAGE_NORMAL,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'image_style_delete_form',
5,
),
'access arguments' => array(
'administer image styles',
),
'file' => 'image.admin.inc',
);
$items['admin/config/media/image-styles/revert/%image_style'] = array(
'title' => 'Revert style',
'description' => 'Revert an image style.',
'load arguments' => array(
NULL,
(string) IMAGE_STORAGE_OVERRIDE,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'image_style_revert_form',
5,
),
'access arguments' => array(
'administer image styles',
),
'file' => 'image.admin.inc',
);
$items['admin/config/media/image-styles/edit/%image_style/effects/%image_effect'] = array(
'title' => 'Edit image effect',
'description' => 'Edit an existing effect within a style.',
'load arguments' => array(
5,
(string) IMAGE_STORAGE_EDITABLE,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'image_effect_form',
5,
7,
),
'access arguments' => array(
'administer image styles',
),
'file' => 'image.admin.inc',
);
$items['admin/config/media/image-styles/edit/%image_style/effects/%image_effect/delete'] = array(
'title' => 'Delete image effect',
'description' => 'Delete an existing effect from a style.',
'load arguments' => array(
5,
(string) IMAGE_STORAGE_EDITABLE,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'image_effect_delete_form',
5,
7,
),
'access arguments' => array(
'administer image styles',
),
'file' => 'image.admin.inc',
);
$items['admin/config/media/image-styles/edit/%image_style/add/%image_effect_definition'] = array(
'title' => 'Add image effect',
'description' => 'Add a new effect to a style.',
'load arguments' => array(
5,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'image_effect_form',
5,
7,
),
'access arguments' => array(
'administer image styles',
),
'file' => 'image.admin.inc',
);
return $items;
}
function image_theme() {
return array(
'image_style' => array(
'variables' => array(
'style_name' => NULL,
'path' => NULL,
'width' => NULL,
'height' => NULL,
'alt' => '',
'title' => NULL,
'attributes' => array(),
),
),
'image_style_list' => array(
'variables' => array(
'styles' => NULL,
),
),
'image_style_effects' => array(
'render element' => 'form',
),
'image_style_preview' => array(
'variables' => array(
'style' => NULL,
),
),
'image_anchor' => array(
'render element' => 'element',
),
'image_resize_summary' => array(
'variables' => array(
'data' => NULL,
),
),
'image_scale_summary' => array(
'variables' => array(
'data' => NULL,
),
),
'image_crop_summary' => array(
'variables' => array(
'data' => NULL,
),
),
'image_rotate_summary' => array(
'variables' => array(
'data' => NULL,
),
),
'image_widget' => array(
'render element' => 'element',
),
'image_formatter' => array(
'variables' => array(
'item' => NULL,
'path' => NULL,
'image_style' => NULL,
),
),
);
}
function image_permission() {
return array(
'administer image styles' => array(
'title' => t('Administer image styles'),
'description' => t('Create and modify styles for generating image modifications such as thumbnails.'),
),
);
}
function image_form_system_file_system_settings_alter(&$form, &$form_state) {
$form['#submit'][] = 'image_system_file_system_settings_submit';
}
function image_system_file_system_settings_submit($form, &$form_state) {
if ($form['file_public_path']['#default_value'] !== $form_state['values']['file_public_path']) {
variable_set('menu_rebuild_needed', TRUE);
}
}
function image_flush_caches() {
return array(
'cache_image',
);
}
function image_file_download($uri) {
$path = file_uri_target($uri);
if (strpos($path, 'styles/') === 0) {
$args = explode('/', $path);
array_shift($args);
$style_name = array_shift($args);
array_shift($args);
$original_uri = file_uri_scheme($uri) . '://' . implode('/', $args);
if ($info = image_get_info($uri)) {
$headers = module_invoke_all('file_download', $original_uri);
if (!empty($headers) && !in_array(-1, $headers)) {
return array(
'Content-Type' => $info['mime_type'],
'Content-Length' => $info['file_size'],
);
}
}
return -1;
}
$files = file_load_multiple(array(), array(
'uri' => $uri,
));
if (count($files)) {
$file = reset($files);
if ($file->status) {
return file_file_download($uri, 'image');
}
}
}
function image_file_move($file, $source) {
image_path_flush($source->uri);
}
function image_file_delete($file) {
image_path_flush($file->uri);
}
function image_image_default_styles() {
$styles = array();
$styles['thumbnail'] = array(
'label' => 'Thumbnail (100x100)',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 100,
'height' => 100,
'upscale' => 1,
),
'weight' => 0,
),
),
);
$styles['medium'] = array(
'label' => 'Medium (220x220)',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 220,
'height' => 220,
'upscale' => 1,
),
'weight' => 0,
),
),
);
$styles['large'] = array(
'label' => 'Large (480x480)',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 480,
'height' => 480,
'upscale' => 0,
),
'weight' => 0,
),
),
);
return $styles;
}
function image_image_style_save($style) {
if (isset($style['old_name']) && $style['old_name'] != $style['name']) {
$instances = field_read_instances();
foreach ($instances as $instance) {
if ($instance['widget']['module'] == 'image') {
$instance_changed = FALSE;
foreach ($instance['display'] as $view_mode => $display) {
if ($display['type'] == 'image' && $display['settings']['image_style'] == $style['old_name']) {
$instance['display'][$view_mode]['settings']['image_style'] = $style['name'];
$instance_changed = TRUE;
}
}
if ($instance['widget']['settings']['preview_image_style'] == $style['old_name']) {
$instance['widget']['settings']['preview_image_style'] = $style['name'];
$instance_changed = TRUE;
}
if ($instance_changed) {
field_update_instance($instance);
}
}
}
}
}
function image_image_style_delete($style) {
image_image_style_save($style);
}
function image_field_delete_field($field) {
if ($field['type'] != 'image') {
return;
}
$fid = is_array($field['settings']['default_image']) ? $field['settings']['default_image']['fid'] : $field['settings']['default_image'];
if ($fid && ($file = file_load($fid))) {
file_usage_delete($file, 'image', 'default_image', $field['id']);
}
}
function image_field_update_field($field, $prior_field, $has_data) {
if ($field['type'] != 'image') {
return;
}
$fid_new = is_array($field['settings']['default_image']) ? $field['settings']['default_image']['fid'] : $field['settings']['default_image'];
$fid_old = is_array($prior_field['settings']['default_image']) ? $prior_field['settings']['default_image']['fid'] : $prior_field['settings']['default_image'];
$file_new = $fid_new ? file_load($fid_new) : FALSE;
if ($fid_new != $fid_old) {
if ($file_new) {
$file_new->status = FILE_STATUS_PERMANENT;
file_save($file_new);
file_usage_add($file_new, 'image', 'default_image', $field['id']);
}
if ($fid_old && ($file_old = file_load($fid_old))) {
file_usage_delete($file_old, 'image', 'default_image', $field['id']);
}
}
if ($file_new && file_uri_scheme($file_new->uri) != $field['settings']['uri_scheme']) {
$directory = $field['settings']['uri_scheme'] . '://default_images/';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
file_move($file_new, $directory . $file_new->filename);
}
}
function image_field_delete_instance($instance) {
$field = field_read_field($instance['field_name']);
if ($field['type'] != 'image') {
return;
}
$fid = $instance['settings']['default_image'];
if (is_array($fid)) {
$fid = $fid['fid'];
}
if ($fid && ($file = file_load($fid))) {
file_usage_delete($file, 'image', 'default_image', $instance['id']);
}
}
function image_field_update_instance($instance, $prior_instance) {
$field = field_read_field($instance['field_name']);
if ($field['type'] != 'image') {
return;
}
$fid_new = $instance['settings']['default_image'];
if (is_array($fid_new)) {
$fid_new = $fid_new['fid'];
}
$fid_old = $prior_instance['settings']['default_image'];
if (is_array($fid_old)) {
$fid_old = $fid_old['fid'];
}
$file_new = $fid_new ? file_load($fid_new) : FALSE;
if ($fid_new != $fid_old) {
if ($file_new) {
$file_new->status = FILE_STATUS_PERMANENT;
file_save($file_new);
file_usage_add($file_new, 'image', 'default_image', $instance['id']);
}
if ($fid_old && ($file_old = file_load($fid_old))) {
file_usage_delete($file_old, 'image', 'default_image', $instance['id']);
}
}
if ($file_new && file_uri_scheme($file_new->uri) != $field['settings']['uri_scheme']) {
$directory = $field['settings']['uri_scheme'] . '://default_images/';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
file_move($file_new, $directory . $file_new->filename);
}
}
function image_path_flush($path) {
$styles = image_styles();
foreach ($styles as $style) {
$image_path = image_style_path($style['name'], $path);
if (file_exists($image_path)) {
file_unmanaged_delete($image_path);
}
}
}
function image_styles() {
$styles =& drupal_static(__FUNCTION__);
if (!isset($styles)) {
if ($cache = cache_get('image_styles', 'cache')) {
$styles = $cache->data;
}
else {
$styles = array();
foreach (module_implements('image_default_styles') as $module) {
$module_styles = module_invoke($module, 'image_default_styles');
foreach ($module_styles as $style_name => $style) {
$style['name'] = $style_name;
$style['label'] = empty($style['label']) ? $style_name : $style['label'];
$style['module'] = $module;
$style['storage'] = IMAGE_STORAGE_DEFAULT;
foreach ($style['effects'] as $key => $effect) {
$definition = image_effect_definition_load($effect['name']);
$effect = array_merge($definition, $effect);
$style['effects'][$key] = $effect;
}
$styles[$style_name] = $style;
}
}
$user_styles = db_select('image_styles', NULL, array(
'fetch' => PDO::FETCH_ASSOC,
))
->fields('image_styles')
->orderBy('name')
->execute()
->fetchAllAssoc('name', PDO::FETCH_ASSOC);
foreach ($user_styles as $style_name => $style) {
$style['module'] = NULL;
$style['storage'] = IMAGE_STORAGE_NORMAL;
$style['effects'] = image_style_effects($style);
if (isset($styles[$style_name]['module'])) {
$style['module'] = $styles[$style_name]['module'];
$style['storage'] = IMAGE_STORAGE_OVERRIDE;
}
$styles[$style_name] = $style;
}
drupal_alter('image_styles', $styles);
cache_set('image_styles', $styles);
}
}
return $styles;
}
function image_style_load($name = NULL, $isid = NULL, $include = NULL) {
$styles = image_styles();
if (isset($name) && isset($styles[$name])) {
$style = $styles[$name];
}
if (!isset($name) && isset($isid)) {
foreach ($styles as $name => $database_style) {
if (isset($database_style['isid']) && $database_style['isid'] == $isid) {
$style = $database_style;
break;
}
}
}
if (isset($style) && (!isset($include) || $style['storage'] & (int) $include)) {
return $style;
}
return FALSE;
}
function image_style_save($style) {
if (isset($style['isid']) && is_numeric($style['isid'])) {
$old_style = image_style_load(NULL, $style['isid']);
image_style_flush($old_style);
drupal_write_record('image_styles', $style, 'isid');
if ($old_style['name'] != $style['name']) {
$style['old_name'] = $old_style['name'];
}
}
else {
if (empty($style['label'])) {
$style['label'] = $style['name'];
}
drupal_write_record('image_styles', $style);
$style['is_new'] = TRUE;
}
module_invoke_all('image_style_save', $style);
image_style_flush($style);
return $style;
}
function image_style_delete($style, $replacement_style_name = '') {
image_style_flush($style);
db_delete('image_effects')
->condition('isid', $style['isid'])
->execute();
db_delete('image_styles')
->condition('isid', $style['isid'])
->execute();
$style['old_name'] = $style['name'];
$style['name'] = $replacement_style_name;
module_invoke_all('image_style_delete', $style);
return TRUE;
}
function image_style_effects($style) {
$effects = image_effects();
$style_effects = array();
foreach ($effects as $effect) {
if ($style['isid'] == $effect['isid']) {
$style_effects[$effect['ieid']] = $effect;
}
}
return $style_effects;
}
function image_style_options($include_empty = TRUE, $output = CHECK_PLAIN) {
$styles = image_styles();
$options = array();
if ($include_empty && !empty($styles)) {
$options[''] = t('<none>');
}
foreach ($styles as $name => $style) {
$options[$name] = $output == PASS_THROUGH ? $style['label'] : check_plain($style['label']);
}
if (empty($options)) {
$options[''] = t('No defined styles');
}
return $options;
}
function image_style_deliver($style, $scheme) {
$args = func_get_args();
array_shift($args);
array_shift($args);
$target = implode('/', $args);
$valid = !empty($style) && file_stream_wrapper_valid_scheme($scheme);
if (!variable_get('image_allow_insecure_derivatives', FALSE) || strpos(ltrim($target, '\\/'), 'styles/') === 0) {
$valid = $valid && isset($_GET[IMAGE_DERIVATIVE_TOKEN]) && $_GET[IMAGE_DERIVATIVE_TOKEN] === image_style_path_token($style['name'], $scheme . '://' . $target);
}
if (!$valid) {
return MENU_ACCESS_DENIED;
}
$image_uri = $scheme . '://' . $target;
$derivative_uri = image_style_path($style['name'], $image_uri);
if ($scheme == 'private') {
if (file_exists($derivative_uri)) {
file_download($scheme, file_uri_target($derivative_uri));
}
else {
$headers = file_download_headers($image_uri);
if (empty($headers)) {
return MENU_ACCESS_DENIED;
}
if (count($headers)) {
foreach ($headers as $name => $value) {
drupal_add_http_header($name, $value);
}
}
}
}
if (!is_file($image_uri)) {
watchdog('image', 'Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', array(
'%source_image_path' => $image_uri,
'%derivative_path' => $derivative_uri,
));
return MENU_NOT_FOUND;
}
$lock_name = 'image_style_deliver:' . $style['name'] . ':' . drupal_hash_base64($image_uri);
if (!file_exists($derivative_uri)) {
$lock_acquired = lock_acquire($lock_name);
if (!$lock_acquired) {
drupal_add_http_header('Status', '503 Service Unavailable');
drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
drupal_add_http_header('Retry-After', 3);
print t('Image generation in progress. Try again shortly.');
drupal_exit();
}
}
$success = file_exists($derivative_uri) || image_style_create_derivative($style, $image_uri, $derivative_uri);
if (!empty($lock_acquired)) {
lock_release($lock_name);
}
if ($success) {
$image = image_load($derivative_uri);
file_transfer($image->source, array(
'Content-Type' => $image->info['mime_type'],
'Content-Length' => $image->info['file_size'],
));
}
else {
watchdog('image', 'Unable to generate the derived image located at %path.', array(
'%path' => $derivative_uri,
));
drupal_add_http_header('Status', '500 Internal Server Error');
drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
print t('Error generating image.');
drupal_exit();
}
}
function image_style_create_derivative($style, $source, $destination) {
if (!($image = image_load($source))) {
return FALSE;
}
$directory = drupal_dirname($destination);
if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
watchdog('image', 'Failed to create style directory: %directory', array(
'%directory' => $directory,
), WATCHDOG_ERROR);
return FALSE;
}
foreach ($style['effects'] as $effect) {
image_effect_apply($image, $effect);
}
if (!image_save($image, $destination)) {
if (file_exists($destination)) {
watchdog('image', 'Cached image file %destination already exists. There may be an issue with your rewrite configuration.', array(
'%destination' => $destination,
), WATCHDOG_ERROR);
}
return FALSE;
}
return TRUE;
}
function image_style_transform_dimensions($style_name, array &$dimensions) {
module_load_include('inc', 'image', 'image.effects');
$style = image_style_load($style_name);
if (!is_array($style)) {
return;
}
foreach ($style['effects'] as $effect) {
if (isset($effect['dimensions passthrough'])) {
continue;
}
if (isset($effect['dimensions callback'])) {
$effect['dimensions callback']($dimensions, $effect['data']);
}
else {
$dimensions['width'] = $dimensions['height'] = NULL;
}
}
}
function image_style_flush($style) {
$wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE);
foreach ($wrappers as $wrapper => $wrapper_data) {
if (file_exists($directory = $wrapper . '://styles/' . $style['name'])) {
file_unmanaged_delete_recursive($directory);
}
}
module_invoke_all('image_style_flush', $style);
cache_clear_all('image_styles', 'cache');
cache_clear_all('image_effects:', 'cache', TRUE);
drupal_static_reset('image_styles');
drupal_static_reset('image_effects');
field_info_cache_clear();
drupal_theme_rebuild();
if (module_exists('block')) {
cache_clear_all('*', 'cache_block', TRUE);
}
cache_clear_all('*', 'cache_page', TRUE);
}
function image_style_url($style_name, $path) {
$uri = image_style_path($style_name, $path);
$original_uri = file_uri_scheme($path) ? file_stream_wrapper_uri_normalize($path) : file_build_uri($path);
$token_query = array();
if (!variable_get('image_suppress_itok_output', FALSE)) {
$token_query = array(
IMAGE_DERIVATIVE_TOKEN => image_style_path_token($style_name, $original_uri),
);
}
if (!variable_get('clean_url') && file_uri_scheme($uri) == 'public' && !file_exists($uri)) {
$directory_path = file_stream_wrapper_get_instance_by_uri($uri)
->getDirectoryPath();
return url($directory_path . '/' . file_uri_target($uri), array(
'absolute' => TRUE,
'query' => $token_query,
));
}
$file_url = file_create_url($uri);
if ($token_query) {
$file_url .= (strpos($file_url, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
}
return $file_url;
}
function image_style_path_token($style_name, $uri) {
return substr(drupal_hmac_base64($style_name . ':' . $uri, drupal_get_private_key() . drupal_get_hash_salt()), 0, 8);
}
function image_style_path($style_name, $uri) {
$scheme = file_uri_scheme($uri);
if ($scheme) {
$path = file_uri_target($uri);
}
else {
$path = $uri;
$scheme = file_default_scheme();
}
return $scheme . '://styles/' . $style_name . '/' . $scheme . '/' . $path;
}
function image_default_style_save($style) {
$style = image_style_save($style);
$effects = array();
foreach ($style['effects'] as $effect) {
$effect['isid'] = $style['isid'];
$effect = image_effect_save($effect);
$effects[$effect['ieid']] = $effect;
}
$style['effects'] = $effects;
return $style;
}
function image_default_style_revert($style) {
image_style_flush($style);
db_delete('image_effects')
->condition('isid', $style['isid'])
->execute();
db_delete('image_styles')
->condition('isid', $style['isid'])
->execute();
return TRUE;
}
function image_effect_definitions() {
global $language;
$langcode = $language->language;
$effects =& drupal_static(__FUNCTION__);
if (!isset($effects)) {
if ($cache = cache_get("image_effects:{$langcode}")) {
$effects = $cache->data;
}
else {
$effects = array();
include_once DRUPAL_ROOT . '/modules/image/image.effects.inc';
foreach (module_implements('image_effect_info') as $module) {
foreach (module_invoke($module, 'image_effect_info') as $name => $effect) {
$effect['module'] = $module;
$effect['name'] = $name;
$effect['data'] = isset($effect['data']) ? $effect['data'] : array();
$effects[$name] = $effect;
}
}
uasort($effects, '_image_effect_definitions_sort');
drupal_alter('image_effect_info', $effects);
cache_set("image_effects:{$langcode}", $effects);
}
}
return $effects;
}
function image_effect_definition_load($effect, $style_name = NULL) {
$definitions = image_effect_definitions();
if (isset($style_name)) {
$style = image_style_load($style_name, NULL);
if ($style['storage'] == IMAGE_STORAGE_DEFAULT) {
return FALSE;
}
}
return isset($definitions[$effect]) ? $definitions[$effect] : FALSE;
}
function image_effects() {
$effects =& drupal_static(__FUNCTION__);
if (!isset($effects)) {
$effects = array();
$result = db_select('image_effects', NULL, array(
'fetch' => PDO::FETCH_ASSOC,
))
->fields('image_effects')
->orderBy('image_effects.weight', 'ASC')
->execute();
foreach ($result as $effect) {
$effect['data'] = unserialize($effect['data']);
$definition = image_effect_definition_load($effect['name']);
if ($definition) {
$effect = array_merge($definition, $effect);
$effects[$effect['ieid']] = $effect;
}
}
}
return $effects;
}
function image_effect_load($ieid, $style_name, $include = NULL) {
if (($style = image_style_load($style_name, NULL, $include)) && isset($style['effects'][$ieid])) {
return $style['effects'][$ieid];
}
return FALSE;
}
function image_effect_save($effect) {
if (!empty($effect['ieid'])) {
drupal_write_record('image_effects', $effect, 'ieid');
}
else {
drupal_write_record('image_effects', $effect);
}
$style = image_style_load(NULL, $effect['isid']);
image_style_flush($style);
return $effect;
}
function image_effect_delete($effect) {
db_delete('image_effects')
->condition('ieid', $effect['ieid'])
->execute();
$style = image_style_load(NULL, $effect['isid']);
image_style_flush($style);
}
function image_effect_apply($image, $effect) {
module_load_include('inc', 'image', 'image.effects');
$function = $effect['effect callback'];
if (function_exists($function)) {
return $function($image, $effect['data']);
}
return FALSE;
}
function theme_image_style($variables) {
$dimensions = array(
'width' => $variables['width'],
'height' => $variables['height'],
);
image_style_transform_dimensions($variables['style_name'], $dimensions);
$variables['width'] = $dimensions['width'];
$variables['height'] = $dimensions['height'];
$variables['path'] = image_style_url($variables['style_name'], $variables['path']);
return theme('image', $variables);
}
function image_filter_keyword($value, $current_pixels, $new_pixels) {
switch ($value) {
case 'top':
case 'left':
return 0;
case 'bottom':
case 'right':
return $current_pixels - $new_pixels;
case 'center':
return $current_pixels / 2 - $new_pixels / 2;
}
return $value;
}
function _image_effect_definitions_sort($a, $b) {
return strcasecmp($a['name'], $b['name']);
}