Change record status: 
Project: 
Introduced in branch: 
8.x
Introduced in version: 
8.0-alpha2-dev
Description: 

Image effects have moved from hook implementations in Drupal 7 to plugins in Drupal 8.

To port existing image effects, create a class in a file like '{module}/src/Plugin/ImageEffect/{ImageEffectType}.php. In most cases, you will want to extend the \Drupal\image\ImageEffectBase base class.

The following hooks are removed and are now methods in \Drupal\image\ImageEffectInterface or \Drupal\image\ConfigurableImageEffectInterface:

  • hook_effect_info() -> replaced by annotation-based plugin discovery, using the \Drupal\image\Annotation\ImageEffect annotation class.
  • 'effect callback' (previously provided with effect via hook_effect_info()) -> ImageEffectInterface::applyEffect()
  • 'summary theme' (previously provided with effect via hook_effect_info()) -> ImageEffectInterface::getSummary()
  • 'dimensions callback' (previously provided with effect via hook_effect_info()) -> ImageEffectInterface::transformDimensions()
  • 'form callback' (previously provided with effect via hook_effect_info()) -> ConfigurableImageEffectInterface::getForm()

Code example:

Example for the email field.

Drupal 7

<?php
/**
 * Implements hook_image_effect_info().
 */
function image_image_effect_info() {
  $effects = array(
    'image_desaturate' => array(
      'label' => t('Desaturate'),
      'help' => t('Desaturate converts an image to grayscale.'),
      'effect callback' => 'image_desaturate_effect',
      'dimensions passthrough' => TRUE,
    ),
  
    // other image effect entries ...
  );

  return $effects;
}

/**
 * Effect callback for desaturate image effect.
 */
function image_desaturate_effect($image, $data) {
  if (!image_desaturate($image)) {
    watchdog('image', 'Image desaturate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
    return FALSE;
  }
  return TRUE;
}
?>

Drupal 8

<?php
namespace Drupal\image\Plugin\ImageEffect;

use Drupal\Core\Annotation\Translation;
use Drupal\image\Annotation\ImageEffect;
use Drupal\image\ImageEffectBase;

/**
 * Desaturates (grayscale) an image resource.
 *
 * @ImageEffect(
 *   id = "image_desaturate",
 *   label = @Translation("Desaturate"),
 *   description = @Translation("Desaturate converts an image to grayscale.")
 * )
 */
class DesaturateImageEffect extends ImageEffectBase {

  /**
   * {@inheritdoc}
   */
  public function applyEffect($image) {
    if (!image_desaturate($image)) {
      watchdog('image', 'Image desaturate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit->getPluginId(), '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
      return FALSE;
    }
    return TRUE;
  }

  // ... other image effect API implementations
}
?>
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done