image.module

File

core/modules/image/image.module

View source
<?php


/**
 * @file
 */

use Drupal\image\Entity\ImageStyle;
use Drupal\image\Hook\ImageThemeHooks;

/**
 * The name of the query parameter for image derivative tokens.
 */
define('IMAGE_DERIVATIVE_TOKEN', 'itok');

/**
 * Clears cached versions of a specific file in all styles.
 *
 * @param string $path
 *   The Drupal file path to the original image.
 */
function image_path_flush($path) : void {
  $styles = ImageStyle::loadMultiple();
  foreach ($styles as $style) {
    $style->flush($path);
  }
}

/**
 * Gets an array of image styles suitable for using as select list options.
 *
 * @param bool $include_empty
 *   If TRUE a '- None -' option will be inserted in the options array.
 *
 * @return string[]
 *   Array of image styles both key and value are set to style name.
 */
function image_style_options($include_empty = TRUE) : array {
  $styles = ImageStyle::loadMultiple();
  $options = [];
  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;
}

/**
 * Prepares variables for image style templates.
 *
 * Default template: image-style.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - width: The width of the image.
 *   - height: The height of the image.
 *   - style_name: The name of the image style to be applied.
 *   - uri: URI of the source image before styling.
 *   - alt: The alternative text for text-based browsers. HTML 4 and XHTML 1.0
 *     always require an alt attribute. The HTML 5 draft allows the alt
 *     attribute to be omitted in some cases. Therefore, this variable defaults
 *     to an empty string, but can be set to NULL for the attribute to be
 *     omitted. Usually, neither omission nor an empty string satisfies
 *     accessibility requirements, so it is strongly encouraged for code using
 *     '#theme' => 'image_style' to pass a meaningful value for this variable.
 *     - https://www.w3.org/TR/REC-html40/struct/objects.html#h-13.8
 *     - https://www.w3.org/TR/xhtml1/dtds.html
 *     - http://dev.w3.org/html5/spec/Overview.html#alt
 *   - title: The title text is displayed when the image is hovered in some
 *     popular browsers.
 *   - attributes: Associative array of additional attributes to be placed in
 *     the img tag.
 *
 * @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
 *   template_preprocess functions are registered directly in hook_theme().
 *
 * @see https://www.drupal.org/node/3504125
 */
function template_preprocess_image_style(&$variables) : void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
  \Drupal::service(ImageThemeHooks::class)->preprocessImageStyle($variables);
}

/**
 * Prepares variables for image style preview templates.
 *
 * Default template: image-style-preview.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - style: \Drupal\image\ImageStyleInterface image style being previewed.
 *
 * @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
 *    template_preprocess functions are registered directly in hook_theme().
 *
 * @see https://www.drupal.org/node/3504125
 */
function template_preprocess_image_style_preview(&$variables) : void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
  \Drupal::service(ImageThemeHooks::class)->preprocessImageStylePreview($variables);
}

/**
 * Prepares variables for image anchor templates.
 *
 * Default template: image-anchor.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: An associative array containing the image.
 *
 * @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
 *    template_preprocess functions are registered directly in hook_theme().
 *
 * @see https://www.drupal.org/node/3504125
 */
function template_preprocess_image_anchor(&$variables) : void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
  \Drupal::service(ImageThemeHooks::class)->preprocessImageAnchor($variables);
}

/**
 * Prepares variables for image widget templates.
 *
 * Default template: image-widget.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: A render element representing the image field widget.
 *
 * @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
 *    template_preprocess functions are registered directly in hook_theme().
 *
 * @see https://www.drupal.org/node/3504125
 */
function template_preprocess_image_widget(&$variables) : void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
  \Drupal::service(ImageThemeHooks::class)->preprocessImageWidget($variables);
}

/**
 * Prepares variables for image formatter templates.
 *
 * Default template: image-formatter.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - item: An ImageItem object.
 *   - item_attributes: An optional associative array of html attributes to be
 *     placed in the img tag.
 *   - image_style: An optional image style.
 *   - url: An optional \Drupal\Core\Url object.
 *
 * @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial
 *    template_preprocess functions are registered directly in hook_theme().
 *
 * @see https://www.drupal.org/node/3504125
 */
function template_preprocess_image_formatter(&$variables) : void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
  \Drupal::service(ImageThemeHooks::class)->preprocessImageFormatter($variables);
}

/**
 * Returns the offset in pixels from the anchor.
 *
 * @param string $anchor
 *   The anchor ('top', 'left', 'bottom', 'right', 'center').
 * @param int $current_size
 *   The current size, in pixels.
 * @param int $new_size
 *   The new size, in pixels.
 *
 * @return int|string
 *   The offset from the anchor, in pixels, or the anchor itself, if its value
 *   isn't one of the accepted values.
 *
 * @deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Use
 *   \Drupal\Component\Utility\Image::getKeywordOffset() instead.
 *
 * @see https://www.drupal.org/node/3268441
 */
function image_filter_keyword($anchor, $current_size, $new_size) {
  @trigger_error('image_filter_keyword() is deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Use \\Drupal\\Component\\Utility\\Image::getKeywordOffset() instead. See https://www.drupal.org/node/3268441', E_USER_DEPRECATED);
  switch ($anchor) {
    case 'top':
    case 'left':
      return 0;
    case 'bottom':
    case 'right':
      return $current_size - $new_size;
    case 'center':
      return $current_size / 2 - $new_size / 2;
    default:
      return $anchor;
  }
}

Functions

Title Deprecated Summary
image_filter_keyword

in drupal:11.1.0 and is removed from drupal:12.0.0. Use \Drupal\Component\Utility\Image::getKeywordOffset() instead.

Returns the offset in pixels from the anchor.
image_path_flush Clears cached versions of a specific file in all styles.
image_style_options Gets an array of image styles suitable for using as select list options.
template_preprocess_image_anchor

in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme().

Prepares variables for image anchor templates.
template_preprocess_image_formatter

in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme().

Prepares variables for image formatter templates.
template_preprocess_image_style

in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme().

Prepares variables for image style templates.
template_preprocess_image_style_preview

in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme().

Prepares variables for image style preview templates.
template_preprocess_image_widget

in drupal:11.3.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme().

Prepares variables for image widget templates.

Constants

Title Deprecated Summary
IMAGE_DERIVATIVE_TOKEN The name of the query parameter for image derivative tokens.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.