Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By slashrsm on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Introduced in version:
8.0-alpha2-dev
Issue links:
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
/**
* 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
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