hook_image_toolkits()
has been replaced by the plugin system.
To implement a toolkit in a module create a class in {module}/src/Plugin/ImageToolkit/{Toolkit}.php
and extend PluginBase and extend ImageToolkitBase
(which implements ImageToolkitInterface). The class must also declare a plugin annotation in its docblock comment. See the D8 example below and the @ImageToolkit
annotation declaration in the class' docblock.
D7
function system_image_toolkits() {
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'system') . '/' . 'image.gd.inc';
return array(
'gd' => array(
'title' => t('GD2 image manipulation toolkit'),
'available' => function_exists('image_gd_check_settings') && image_gd_check_settings(),
),
);
}
D8
<?php
/**
* @file
* Contains \Drupal\system\Plugin\ImageToolkit\GDToolkit;.
*/
namespace Drupal\system\Plugin\ImageToolkit;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\ImageToolkit\ImageToolkitBase;
use Drupal\Component\Utility\Image as ImageUtility;
/**
* Defines the GD2 toolkit for image manipulation within Drupal.
*
* @ImageToolkit(
* id = "gd",
* title = @Translation("GD2 image manipulation toolkit")
* )
*/
class GDToolkit extends ImageToolkitBase {
// Implements functions that:
// - configure the toolkit
// - manipulate images using the toolkit
// - check that the toolkit's dependencies are available
}
?>
The default image toolkit is still based on PHP's GD library and is implemented by the class \Drupal\system\Plugin\ImageToolkit\GDToolkit.
Settings for image toolkits are stored in the configuration system. The plugin id of default toolkit is stored in system.image.yml
and GDToolkit's settings are stored in system.image.gd.yml
Image toolkits are discovered using the Plugin system using \Drupal\system\Plugin\ImageToolkitManager
which is available as a service on the container. The toolkit must then be enabled using the admin/config/media/image-toolkit form. Additionally, the container has the image.toolkit
service which gives access to the default image toolkit.
Manipulating images
The function image_toolkit_invoke()
has been removed in preference for using methods on the toolkit. For example, rotating an image by 90 degrees:
D7
$image = image_load('sites/default/files/example.jpg');
image_toolkit_invoke('rotate', $image, array(90));
image_save($image, 'sites/default/files/example.jpg');
D8
$image = image_load('sites/default/files/example.jpg');
$image->toolkit->rotate(90);
image_save($image, 'sites/default/files/example.jpg');
Image Toolkit Derivatives
You can extend a toolkit plugin and still inherit all its operations (crop, scale, etc) by declaring a toolkit derivative. You can overwrite some operations but inherit the others. You'll need to specify the base image toolkit plugin ID, followed by a colon (":") as prefix in the derivative ID. Also you can (but it;s not mandatory) to extend the base image toolkit plugin class:
/**
* Defines a derivative of GD2 toolkit.
*
* @ImageToolkit(
* id = "gd:webp_support",
* title = @Translation("GD2 width WEBP support image manipulation toolkit.")
* )
*/
class GdWebpToolkit extends GDToolkit {
// Override what you need.
}
The you may define operations only for the derived toolkit. If the same operation, defined for derivative, overlaps a base toolkit operation, the derivative operation take precedence.