'profile-picture
'));
$naming_approaches[] = t('Describing its appearance: !name', array('!name' => 'square-85x85
'));
$output = '';
$output .= '
' . t('The Image module provides functionality for displaying images on your site though image styles.', array('!url' => url('admin/config/media/image-styles'))) .'
'; $output .= '' . t('Image styles allow your site to output an image in several different ways without affecting the original image. Any created images will automatically be refreshed if any changes are made to the image style.') .'
'; $output .= '' . t('Every image style must have a name, which will be used in the URL of generated images. There are two common approaches to naming image styles:') . '
'; $output .= theme('item_list', array('items' => $naming_approaches)); $output .= '' . t('Both approaches are common and which you choose depends on how you use the image style.') . '
'; $output .= '' . t('After creating an image style, effects may be added to the style. Image module comes with some basic effects such as crop, scale, desaturate, and rotate. In addition to the effects included with Image, other modules may provide additional effects. Multiple effects may be combined together, such as using the crop and scale effect and the desaturate effect, you could create square, grayscale thumbnails.'); return $output; case 'admin/config/media/image-styles': return '
' . 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.') . '
'; case 'admin/config/media/image-styles/edit/%/add/%': case 'admin/config/media/image-styles/edit/%/effects/%': $effect = ($arg[5] == 'add') ? image_effect_definition_load($arg[6]) : image_effect_load($arg[6], $arg[4]); return isset($effect['help']) ? ('' . $effect['help'] . '
') : NULL; } } /** * Implement hook_menu(). */ function image_menu() { $items = array(); $items['image/generate/%image_style'] = array( 'title' => 'Generate image style', 'page callback' => 'image_style_generate', 'page arguments' => array(2), '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'), 'type' => MENU_CALLBACK, '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'), 'type' => MENU_CALLBACK, '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'), 'type' => MENU_CALLBACK, 'file' => 'image.admin.inc', ); $items['admin/config/media/image-styles/edit/%image_style/effects/%image_effect'] = array( 'title' => 'Edit image effect', 'description' => 'Edit an exiting 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'), 'type' => MENU_CALLBACK, '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 exiting 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'), 'type' => MENU_CALLBACK, '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'), 'type' => MENU_CALLBACK, 'file' => 'image.admin.inc', ); return $items; } /** * Implement hook_theme(). */ function image_theme() { return array( // Theme functions in image.module. 'image_style' => array( 'arguments' => array( 'style_name' => NULL, 'path' => NULL, 'alt' => '', 'title' => '', 'attributes' => array(), 'getsize' => TRUE, ), ), // Theme functions in image.admin.inc. 'image_style_list' => array( 'arguments' => array('styles' => NULL), ), 'image_style_effects' => array( 'arguments' => array('form' => NULL), ), 'image_style_preview' => array( 'arguments' => array('style' => NULL), ), 'image_anchor' => array( 'arguments' => array('element' => NULL), ), 'image_resize_summary' => array( 'arguments' => array('data' => NULL), ), 'image_scale_summary' => array( 'arguments' => array('data' => NULL), ), 'image_crop_summary' => array( 'arguments' => array('data' => NULL), ), 'image_rotate_summary' => array( 'arguments' => array('data' => NULL), ), // Theme functions in image.field.inc. 'image_widget' => array( 'arguments' => array('element' => NULL), ), 'field_formatter_image' => array( 'arguments' => array('element' => NULL), ), 'field_formatter_image_link_content' => array( 'arguments' => array('element' => NULL), ), 'field_formatter_image_link_file' => array( 'arguments' => array('element' => NULL), ), ); } /** * Implement hook_permission(). */ 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.'), ), ); } /** * Implement hook_flush_caches(). */ function image_flush_caches() { return array('cache_image'); } /** * Implement hook_file_download(). * * Control the access to files underneath the styles directory. */ function image_file_download($uri) { $path = file_uri_target($uri); // Private file access for image style derivatives. if (strpos($path, 'styles/') === 0) { $args = explode('/', $path); // Discard the first part of the path (styles). array_shift($args); // Get the style name from the second part. $style_name = array_shift($args); // Then the remaining parts are the path to the image. $original_uri = file_uri_scheme($uri) . '://' . implode('/', $args); // Check that the file exists and is an image. if ($info = image_get_info($uri)) { // Check the permissions of the original to grant access to this image. $headers = module_invoke_all('file_download', $original_uri); if (!in_array(-1, $headers)) { return array( // Send headers describing the image's size, and MIME-type... 'Content-Type' => $info['mime_type'], 'Content-Length' => $info['file_size'], // ...and allow the file to be cached for two weeks (matching the // value we/ use for the mod_expires settings in .htaccess) and // ensure that caching proxies do not share the image with other // users. 'Expires' => gmdate(DATE_RFC1123, REQUEST_TIME + 1209600), 'Cache-Control' => 'max-age=1209600, private, must-revalidate', ); } } return -1; } // Private file access for the original files. Note that we only // check access for non-temporary images, since file.module will // grant access for all temporary files. $files = file_load_multiple(array(), array('uri' => $uri)); if (count($files)) { $file = reset($files); if ($file->status) { return file_file_download($uri, 'image'); } } } /** * Implement hook_file_move(). */ function image_file_move($file, $source) { // Delete any image derivatives at the original image path. image_path_flush($file->uri); } /** * Implement hook_file_delete(). */ function image_file_delete($file) { // Delete any image derivatives of this image. image_path_flush($file->uri); } /** * Implement hook_file_references(). */ function image_file_references($file) { $count = file_get_file_reference_count($file, NULL, 'image'); return $count ? array('image' => $count) : NULL; } /** * Implement hook_image_default_styles(). */ function image_image_default_styles() { $styles = array(); $styles['thumbnail'] = array( 'effects' => array( array( 'name' => 'image_scale', 'data' => array('width' => 100, 'height' => 100, 'upscale' => 1), 'weight' => 0, ), ) ); $styles['medium'] = array( 'effects' => array( array( 'name' => 'image_scale', 'data' => array('width' => 220, 'height' => 220, 'upscale' => 1), 'weight' => 0, ), ) ); $styles['large'] = array( 'effects' => array( array( 'name' => 'image_scale', 'data' => array('width' => 640, 'height' => 640, 'upscale' => 1), 'weight' => 0, ), ) ); return $styles; } /** * Clear cached versions of a specific file in all styles. * * @param $path * The Drupal file path to the original image. */ function image_path_flush($path) { $styles = image_styles(); foreach ($styles as $style) { $path = image_style_path($style['name'], $path); if (file_exists($path)) { file_unmanaged_delete($path); } } } /** * Get an array of all styles and their settings. * * @return * An array of styles keyed by the image style ID (isid). * @see image_style_load() */ function image_styles() { $styles = &drupal_static(__FUNCTION__); // Grab from cache or build the array. if (!isset($styles)) { if ($cache = cache_get('image_styles', 'cache')) { $styles = $cache->data; } else { $styles = array(); // Select the module-defined styles. 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['module'] = $module; $style['storage'] = IMAGE_STORAGE_DEFAULT; foreach ($style['effects'] as $ieid => $effect) { $definition = image_effect_definition_load($effect['name']); $effect = array_merge($definition, $effect); $effect['ieid'] = $ieid; $style['effects'][$ieid] = $effect; } $styles[$style_name] = $style; } } // Select all the user-defined styles. $user_styles = db_select('image_styles', NULL, array('fetch' => PDO::FETCH_ASSOC)) ->fields('image_styles') ->orderBy('name') ->execute() ->fetchAllAssoc('name', PDO::FETCH_ASSOC); // Allow the user styles to override the module styles. 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; } /** * Load a style by style name or ID. May be used as a loader for menu items. * * @param $name * The name of the style. * @param $isid * Optional. The numeric id of a style if the name is not known. * @param $include * If set, this loader will restrict to a specific type of image style, may be * one of the defined Image style storage constants. * @return * An image style array containing the following keys: * - "isid": The unique image style ID. * - "name": The unique image style name. * - "effects": An array of image effects within this image style. * If the image style name or ID is not valid, an empty array is returned. * @see image_effect_load() */ function image_style_load($name = NULL, $isid = NULL, $include = NULL) { $styles = image_styles(); // If retrieving by name. if (isset($name) && isset($styles[$name])) { $style = $styles[$name]; } // If retrieving by image style id. if (!isset($name) && isset($isid)) { foreach ($styles as $name => $database_style) { if (isset($database_style['isid']) && $database_style['isid'] == $isid) { break; } } } // Restrict to the specific type of flag. This bitwise operation basically // states "if the storage is X, then allow". if (isset($style) && (!isset($include) || ($style['storage'] & (int) $include))) { return $style; } // Otherwise the style was not found. return FALSE; } /** * Save an image style. * * @param style * An image style array. * @return * An image style array. In the case of a new style, 'isid' will be populated. */ function image_style_save($style) { if (isset($style['isid']) && is_numeric($style['isid'])) { // Load the existing style to make sure we account for renamed styles. $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 { drupal_write_record('image_styles', $style); $style['is_new'] = TRUE; } // Let other modules update as necessary on save. module_invoke_all('image_style_save', $style); // Clear all caches and flush. image_style_flush($style); return $style; } /** * Delete an image style. * * @param $style * An image style array. * @param $replacement_style_name * (optional) When deleting a style, specify a replacement style name so * that existing settings (if any) may be converted to a new style. * @return * TRUE on success. */ 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(); // Let other modules update as necessary on save. $style['old_name'] = $style['name']; $style['name'] = $replacement_style_name; module_invoke_all('image_style_delete', $style); return TRUE; } /** * Load all the effects for an image style. * * @param $style * An image style array. * @return * An array of image effects associated with specified image style in the * format array('isid' => array()), or an empty array if the specified style * has no effects. */ 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; } /** * Get an array of image styles suitable for using as select list options. * * @param $include_empty * If TRUE a