' . t('About') . ''; $output .= '
' . t('The Image module allows you to manipulate images on your website. It exposes a setting for using the Image toolkit, allows you to configure Image styles that can be used for resizing or adjusting images on display, and provides an Image field for attaching images to content. For more information, see the online handbook entry for Image module.', array('@image' => 'http://drupal.org/documentation/modules/image')) . '
'; $output .= '' . 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/manage/%/add/%': $effect = image_effect_definition_load($arg[7]); return isset($effect['help']) ? ('' . $effect['help'] . '
') : NULL; case 'admin/config/media/image-styles/manage/%/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; } } /** * Entity URI callback for image style. */ function image_style_entity_uri(ImageStyle $style) { return array( 'path' => 'admin/config/media/image-styles/manage/' . $style->id(), ); } /** * Implements hook_menu(). */ function image_menu() { $items = array(); // Generate image derivatives of publicly available files. // If clean URLs are disabled, image derivatives will always be served // through the menu system. // If clean URLs are enabled and the image derivative already exists, // PHP will be bypassed. $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, ); // Generate and deliver image derivatives of private files. // These image derivatives are always delivered through the menu system. $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, '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/manage/%image_style'] = array( 'title' => 'Edit style', 'title callback' => 'entity_page_label', 'title arguments' => array(5), '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/manage/%image_style/edit'] = array( 'title' => 'Edit', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, ); $items['admin/config/media/image-styles/manage/%image_style/delete'] = array( 'title' => 'Delete', 'description' => 'Delete an image style.', 'type' => MENU_LOCAL_TASK, 'weight' => 10, 'route_name' => 'image_style_delete', ); $items['admin/config/media/image-styles/manage/%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/manage/%image_style/effects/%image_effect/delete'] = array( 'title' => 'Delete image effect', 'description' => 'Delete an existing effect from a style.', 'route_name' => 'image_effect_delete', ); $items['admin/config/media/image-styles/manage/%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; } /** * Implements hook_theme(). */ function image_theme() { return array( // Theme functions in image.module. 'image_style' => array( 'variables' => array( 'style_name' => NULL, 'uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => array(), ), ), // Theme functions in image.admin.inc. '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), ), // Theme functions in image.field.inc. 'image_widget' => array( 'render element' => 'element', ), 'image_formatter' => array( 'variables' => array('item' => NULL, 'path' => NULL, 'image_style' => NULL), ), ); } /** * Implements 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.'), ), ); } /** * Implements hook_form_FORM_ID_alter(). */ function image_form_system_file_system_settings_alter(&$form, &$form_state) { $form['#submit'][] = 'image_system_file_system_settings_submit'; } /** * Form submission handler for system_file_system_settings(). * * Adds a menu rebuild after the public file path has been changed, so that the * menu router item depending on that file path will be regenerated. */ function image_system_file_system_settings_submit($form, &$form_state) { if ($form['file_public_path']['#default_value'] !== $form_state['values']['file_public_path']) { state()->set('menu_rebuild_needed', TRUE); } } /** * Implements 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); // Remove the scheme from the path. 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); // Confirm there's at least one module granting access and none denying access. if (!empty($headers) && !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'], // By not explicitly setting them here, this uses normal Drupal // Expires, Cache-Control and ETag headers to prevent proxy or // browser caching of private images. ); } } 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 = entity_load_multiple_by_properties('file', array('uri' => $uri)); if (count($files)) { $file = reset($files); if ($file->status) { return file_file_download($uri, 'image'); } } } /** * Implements hook_file_move(). */ function image_file_move(File $file, File $source) { // Delete any image derivatives at the original image path. image_path_flush($source->uri); } /** * Implements hook_file_predelete(). */ function image_file_predelete(File $file) { // Delete any image derivatives of this image. image_path_flush($file->uri); } /** * Implements hook_field_delete_field(). */ function image_field_delete_field($field) { if ($field['type'] != 'image') { return; } // The value of a managed_file element can be an array if #extended == TRUE. $fid = (isset($field['settings']['default_image']['fids']) ? $field['settings']['default_image']['fids'] : $field['settings']['default_image']); if ($fid && ($file = file_load($fid[0]))) { file_usage()->delete($file, 'image', 'default_image', $field['id']); } } /** * Implements hook_field_update_field(). */ function image_field_update_field($field, $prior_field, $has_data) { if ($field['type'] != 'image') { return; } // The value of a managed_file element can be an array if #extended == TRUE. $fid_new = (isset($field['settings']['default_image']['fids']) ? $field['settings']['default_image']['fids'] : $field['settings']['default_image']); $fid_old = (isset($prior_field['settings']['default_image']['fids']) ? $prior_field['settings']['default_image']['fids'] : $prior_field['settings']['default_image']); $file_new = $fid_new ? file_load($fid_new[0]) : FALSE; if ($fid_new != $fid_old) { // Is there a new file? if ($file_new) { $file_new->status = FILE_STATUS_PERMANENT; $file_new->save(); file_usage()->add($file_new, 'image', 'default_image', $field['uuid']); } // Is there an old file? if ($fid_old && ($file_old = file_load($fid_old[0]))) { file_usage()->delete($file_old, 'image', 'default_image', $field['uuid']); } } // If the upload destination changed, then move the file. 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); } } /** * Implements hook_field_delete_instance(). */ function image_field_delete_instance($instance) { // Only act on image fields. $field = field_read_field($instance['field_name']); if ($field['type'] != 'image') { return; } // The value of a managed_file element can be an array if the #extended // property is set to TRUE. $fid = $instance['settings']['default_image']; if (is_array($fid)) { $fid = $fid['fid']; } // Remove the default image when the instance is deleted. if ($fid && ($file = file_load($fid))) { file_usage()->delete($file, 'image', 'default_image', $instance['id']); } } /** * Implements hook_field_update_instance(). */ function image_field_update_instance($instance, $prior_instance) { // Only act on image fields. $field = field_read_field($instance['field_name']); if ($field['type'] != 'image') { return; } // The value of a managed_file element can be an array if the #extended // property is set to TRUE. $fid_new = $instance['settings']['default_image']; if (isset($fid_new['fids'])) { $fid_new = $fid_new['fids']; } $fid_old = $prior_instance['settings']['default_image']; if (isset($fid_old['fids'])) { $fid_old = $fid_old['fids']; } // If the old and new files do not match, update the default accordingly. $file_new = $fid_new ? file_load($fid_new[0]) : FALSE; if ($fid_new != $fid_old) { // Save the new file, if present. if ($file_new) { $file_new->status = FILE_STATUS_PERMANENT; $file_new->save(); file_usage()->add($file_new, 'image', 'default_image', $instance['uuid']); } // Delete the old file, if present. if ($fid_old && ($file_old = file_load($fid_old[0]))) { file_usage()->delete($file_old, 'image', 'default_image', $instance['uuid']); } } // If the upload destination changed, then move the file. 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); } } /** * Clears 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 = entity_load_multiple('image_style'); foreach ($styles as $style) { $image_path = image_style_path($style->id(), $path); if (file_exists($image_path)) { file_unmanaged_delete($image_path); } } } /** * Loads an ImageStyle object. * * @param string $name * The ID of the ImageStyle object to load. */ function image_style_load($name) { return entity_load('image_style', $name); } /** * Gets an array of image styles suitable for using as select list options. * * @param $include_empty * If TRUE a '- None -' option will be inserted in the options array. * @return * Array of image styles both key and value are set to style name. */ function image_style_options($include_empty = TRUE) { $styles = entity_load_multiple('image_style'); $options = array(); if ($include_empty && !empty($styles)) { $options[''] = t('- None -'); } foreach ($styles as $name => $style) { $options[$name] = $style->label(); } if (empty($options)) { $options[''] = t('No defined styles'); } return $options; } /** * Page callback: Generates a derivative, given a style and image path. * * After generating an image, transfer it to the requesting agent. * * @param $style * The image style */ function image_style_deliver($style, $scheme) { $args = func_get_args(); array_shift($args); array_shift($args); $target = implode('/', $args); // Check that the style is defined, the scheme is valid, and the image // derivative token is valid. (Sites which require image derivatives to be // generated without a token can set the // 'image.settings:allow_insecure_derivatives' configuration to TRUE to bypass // the latter check, but this will increase the site's vulnerability to // denial-of-service attacks.) $valid = !empty($style) && file_stream_wrapper_valid_scheme($scheme); if (!config('image.settings')->get('allow_insecure_derivatives')) { $valid = $valid && isset($_GET[IMAGE_DERIVATIVE_TOKEN]) && $_GET[IMAGE_DERIVATIVE_TOKEN] === image_style_path_token($style->name, $scheme . '://' . $target); } if (!$valid) { throw new AccessDeniedHttpException(); } $image_uri = $scheme . '://' . $target; $derivative_uri = image_style_path($style->id(), $image_uri); // If using the private scheme, let other modules provide headers and // control access to the file. if ($scheme == 'private') { if (file_exists($derivative_uri)) { file_download($scheme, file_uri_target($derivative_uri)); } else { $headers = module_invoke_all('file_download', $image_uri); if (in_array(-1, $headers) || empty($headers)) { throw new AccessDeniedHttpException(); } if (count($headers)) { foreach ($headers as $name => $value) { drupal_add_http_header($name, $value); } } } } // Don't try to generate file if source is missing. if (!file_exists($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 new Response(t('Error generating image, missing source file.'), 404); } // Don't start generating the image if the derivative already exists or if // generation is in progress in another thread. $lock_name = 'image_style_deliver:' . $style->id() . ':' . Crypt::hashBase64($image_uri); if (!file_exists($derivative_uri)) { $lock_acquired = lock()->acquire($lock_name); if (!$lock_acquired) { // Tell client to retry again in 3 seconds. Currently no browsers are known // to support Retry-After. drupal_add_http_header('Status', '503 Service Unavailable'); drupal_add_http_header('Retry-After', 3); print t('Image generation in progress. Try again shortly.'); drupal_exit(); } } // Try to generate the image, unless another thread just did it while we were // acquiring the lock. $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); $uri = $image->source; $headers = array( 'Content-Type' => $image->info['mime_type'], 'Content-Length' => $image->info['file_size'], ); return new BinaryFileResponse($uri, 200, $headers); } else { watchdog('image', 'Unable to generate the derived image located at %path.', array('%path' => $derivative_uri)); return new Response(t('Error generating image.'), 500); } } /** * Creates a new image derivative based on an image style. * * Generates an image derivative by creating the destination folder (if it does * not already exist), applying all image effects defined in $style->effects, * and saving a cached version of the resulting image. * * @param $style * An image style array. * @param $source * Path of the source file. * @param $destination * Path or URI of the destination file. * * @return * TRUE if an image derivative was generated, or FALSE if the image derivative * could not be generated. * * @see image_style_load() */ function image_style_create_derivative($style, $source, $destination) { // Get the folder for the final location of this style. $directory = drupal_dirname($destination); // Build the destination folder tree if it doesn't already exist. 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; } if (!$image = image_load($source)) { return FALSE; } if (!empty($style->effects)) { 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; } /** * Determines the dimensions of the styled image. * * Applies all of an image style's effects to $dimensions. * * @param $style_name * The name of the style to be applied. * @param $dimensions * Dimensions to be modified - an array with components width and height, in * pixels. */ function image_style_transform_dimensions($style_name, array &$dimensions) { module_load_include('inc', 'image', 'image.effects'); $style = entity_load('image_style', $style_name); if (!empty($style->effects)) { 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; } } } } /** * Flushes cached media for a style. * * @param $style * An image style array. */ function image_style_flush($style) { // Delete the style directory in each registered wrapper. $wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE); foreach ($wrappers as $wrapper => $wrapper_data) { file_unmanaged_delete_recursive($wrapper . '://styles/' . $style->id()); } // Let other modules update as necessary on flush. module_invoke_all('image_style_flush', $style); // Clear field caches so that formatters may be added for this style. field_info_cache_clear(); drupal_theme_rebuild(); // Clear page caches when flushing. if (module_exists('block')) { cache('block')->deleteAll(); } cache('page')->deleteAll(); } /** * Returns the URL for an image derivative given a style and image path. * * @param $style_name * The name of the style to be used with this image. * @param $path * The path to the image. * @return * The absolute URL where a style image can be downloaded, suitable for use * in an