diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 3bb9a7d..f7248c2 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -326,7 +326,7 @@ public function crop($x, $y, $width, $height) { $width = (int) round($width); $height = (int) round($height); - return $this->toolkit->operation('crop', array($this, $x, $y, $width, $height)); + return $this->toolkit->process('crop', $this, array('x' => $x, 'y' => $y, 'width' => $width, 'height' => $height)); } /** @@ -336,21 +336,21 @@ public function resize($width, $height) { $width = (int) round($width); $height = (int) round($height); - return $this->toolkit->operation('resize', array($this, $width, $height)); + return $this->toolkit->process('resize', $this, array('width' => $width, 'height' => $height)); } /** * {@inheritdoc} */ public function desaturate() { - return $this->toolkit->operation('desaturate', array($this)); + return $this->toolkit->process('desaturate', $this); } /** * {@inheritdoc} */ public function rotate($degrees, $background = NULL) { - return $this->toolkit->operation('rotate', array($this, $degrees, $background)); + return $this->toolkit->process('rotate', $this, array('degrees' => $degrees, 'background' => $background)); } /** diff --git a/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php index 3498251..d4b1a77 100644 --- a/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php +++ b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php @@ -26,7 +26,14 @@ class ImageToolkitOperation extends Plugin { public $id; /** - * The title of the image toolkit. + * The label of the image toolkit operation. + * + * @var string + */ + public $label; + + /** + * The description of the image toolkit operation. * * The string should be wrapped in a @Translation(). * @@ -34,20 +41,13 @@ class ImageToolkitOperation extends Plugin { * * @var \Drupal\Core\Annotation\Translation */ - public $title; + public $description; /** * The id of the image toolkit plugin for which the operation is implemented. * * @var \Drupal\system\Plugin\ImageToolkit */ - public $toolkit_id; - - /** - * The toolkit operation implemented. - * - * @var string - */ - public $operation; + public $toolkit; } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php index 0a11c33..f6aa276 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php @@ -11,6 +11,7 @@ use Drupal\system\Annotation\ImageToolkit; use Drupal\Core\Annotation\Translation; use Drupal\Core\Image\ImageInterface; +use Drupal\system\Plugin\ImageToolkitBase; use Drupal\system\Plugin\ImageToolkitInterface; /** @@ -21,7 +22,7 @@ * title = @Translation("GD2 image manipulation toolkit") * ) */ -class GDToolkit extends PluginBase implements ImageToolkitInterface { +class GDToolkit extends ImageToolkitBase implements ImageToolkitInterface { /** * {@inheritdoc} @@ -58,7 +59,7 @@ public function load(ImageInterface $image) { if (!imageistruecolor($resource)) { // Convert indexed images to true color, so that filters work // correctly and don't result in unnecessary dither. - $new_image = $this->operation('createTmp', array($image, $image->getWidth(), $image->getHeight())); + $new_image = $this->process('createTmp', $image, array('width' => $image->getWidth(), 'height' => $image->getHeight())); imagecopy($new_image, $resource, 0, 0, 0, 0, $image->getWidth(), $image->getHeight()); imagedestroy($resource); $image->setResource($new_image); @@ -147,14 +148,4 @@ public static function supportedTypes() { return array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); } - /** - * @todo - */ - public function operation($name, $arguments) { - $plugin_id = ($name == 'createTmp') ? 'gd_create_tmp' : 'gd_' . $name; - $operation = \Drupal::service('image.toolkit.operation.manager')->createInstance($plugin_id); - $args = array($this); - $args = array_merge($args, $arguments); - return call_user_func_array(array($operation, $name), $args); - } } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php new file mode 100644 index 0000000..52a113c --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php @@ -0,0 +1,75 @@ +createInstance($plugin_id); + return $plugin->process($this, $image, $variables); + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php index ebc8a04..14f66d0 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php @@ -117,4 +117,19 @@ public static function isAvailable(); * IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.). */ public static function supportedTypes(); + + /** + * Processes a toolkit operation. + * + * @param string $operation + * The toolkit operation to be processed. + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. + * @param array $variables + * An array of variables to be used by the toolkit operation. + * + * @return bool + * TRUE if the operation is successful, FALSE otherwise. + */ + public function process($operation, ImageInterface $image, array $variables = NULL); } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCreateTmp.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCreateTmp.php index e371ddd..e301e21 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCreateTmp.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCreateTmp.php @@ -8,21 +8,23 @@ namespace Drupal\system\Plugin\ImageToolkitOperation; use Drupal\Core\Plugin\PluginBase; -use Drupal\system\Annotation\ImageToolkitOperation; use Drupal\Core\Annotation\Translation; use Drupal\Core\Image\ImageInterface; +use Drupal\system\Annotation\ImageToolkitOperation; +use Drupal\system\Plugin\ImageToolkitInterface; +use Drupal\system\Plugin\ImageToolkitOperationBase; /** * Defines GD2 CreateTmp operation. * * @ImageToolkitOperation( * id = "gd_create_tmp", - * title = @Translation("GD2 toolkit createTmp"), - * toolkit_id = "gd", - * operation = "createTmp" + * label = "createTmp", + * description = @Translation("GD2 toolkit createTmp"), + * toolkit = "gd" * ) */ -class GDToolkitCreateTmp extends PluginBase { +class GDToolkitCreateTmp extends ImageToolkitOperationBase { /** * Creates a truecolor image preserving transparency from a provided image. @@ -37,8 +39,8 @@ class GDToolkitCreateTmp extends PluginBase { * @return resource * A GD image handle. */ - public function createTmp(ImageToolkitInterface $toolkit, ImageInterface $image, $width, $height) { - $res = imagecreatetruecolor($width, $height); + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { + $res = imagecreatetruecolor($variables['width'], $variables['height']); if ($image->getType() == IMAGETYPE_GIF) { // Grab transparent color index from image resource. diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php index bf82e2a..7f7d887 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php @@ -8,29 +8,48 @@ namespace Drupal\system\Plugin\ImageToolkitOperation; use Drupal\Core\Plugin\PluginBase; -use Drupal\system\Annotation\ImageToolkitOperation; use Drupal\Core\Annotation\Translation; use Drupal\Core\Image\ImageInterface; +use Drupal\system\Annotation\ImageToolkitOperation; +use Drupal\system\Plugin\ImageToolkitInterface; +use Drupal\system\Plugin\ImageToolkitOperationBase; /** * Defines GD2 Crop operation. * * @ImageToolkitOperation( * id = "gd_crop", - * title = @Translation("GD2 toolkit crop"), - * toolkit_id = "gd", - * operation = "crop" + * label = "crop", + * description = @Translation("GD2 toolkit crop"), + * toolkit = "gd" * ) */ -class GDToolkitCrop extends PluginBase { +class GDToolkitCrop extends ImageToolkitOperationBase { /** - * {@inheritdoc} + * Crops an image. + * + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. The $image->resource, $image->info['width'], and + * $image->info['height'] values will be modified by this call. + * @param int $x + * The starting x offset at which to start the crop, in pixels. + * @param int $y + * The starting y offset at which to start the crop, in pixels. + * @param int $width + * The width of the cropped area, in pixels. + * @param int $height + * The height of the cropped area, in pixels. + * + * @return bool + * TRUE or FALSE, based on success. + * + * @see image_crop() */ - public function crop(ImageToolkitInterface $toolkit, ImageInterface $image, $x, $y, $width, $height) { - $res = $this->createTmp($image, $width, $height); + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { + $res = $toolkit->process('createTmp', $image, array('width' => $variables['width'], 'height' => $variables['height'])); - if (!imagecopyresampled($res, $image->getResource(), 0, 0, $x, $y, $width, $height, $width, $height)) { + if (!imagecopyresampled($res, $image->getResource(), 0, 0, $variables['x'], $variables['y'], $variables['width'], $variables['height'], $variables['width'], $variables['height'])) { return FALSE; } @@ -38,8 +57,8 @@ public function crop(ImageToolkitInterface $toolkit, ImageInterface $image, $x, imagedestroy($image->getResource()); $image ->setResource($res) - ->setWidth($width) - ->setHeight($height); + ->setWidth($variables['width']) + ->setHeight($variables['height']); return TRUE; } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitDesaturate.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitDesaturate.php index 0121ee3..fd96215 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitDesaturate.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitDesaturate.php @@ -8,26 +8,37 @@ namespace Drupal\system\Plugin\ImageToolkitOperation; use Drupal\Core\Plugin\PluginBase; -use Drupal\system\Annotation\ImageToolkitOperation; use Drupal\Core\Annotation\Translation; use Drupal\Core\Image\ImageInterface; +use Drupal\system\Annotation\ImageToolkitOperation; +use Drupal\system\Plugin\ImageToolkitInterface; +use Drupal\system\Plugin\ImageToolkitOperationBase; /** * Defines GD2 Desaturate operation. * * @ImageToolkitOperation( * id = "gd_desaturate", - * title = @Translation("GD2 toolkit desaturate"), - * toolkit_id = "gd", - * operation = "desaturate" + * label = "desaturate", + * description = @Translation("GD2 toolkit desaturate"), + * toolkit = "gd" * ) */ -class GDToolkitDesaturate extends PluginBase { +class GDToolkitDesaturate extends ImageToolkitOperationBase { /** - * {@inheritdoc} + * Converts an image resource to grayscale. + * + * Note that transparent GIFs loose transparency when desaturated. + * + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. The $image->resource value will be modified by this + * call. + * + * @return bool + * TRUE or FALSE, based on success. */ - public function desaturate(ImageToolkitInterface $toolkit, ImageInterface $image) { + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { // PHP installations using non-bundled GD do not have imagefilter. if (!function_exists('imagefilter')) { watchdog('image', 'The image %file could not be desaturated because the imagefilter() function is not available in this PHP installation.', array('%file' => $image->getSource())); diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php index c119c09..1fa4195 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php @@ -8,30 +8,42 @@ namespace Drupal\system\Plugin\ImageToolkitOperation; use Drupal\Core\Plugin\PluginBase; -use Drupal\system\Annotation\ImageToolkitOperation; -use Drupal\system\Plugin\ImageToolkitInterface; use Drupal\Core\Annotation\Translation; use Drupal\Core\Image\ImageInterface; +use Drupal\system\Annotation\ImageToolkitOperation; +use Drupal\system\Plugin\ImageToolkitInterface; +use Drupal\system\Plugin\ImageToolkitOperationBase; /** * Defines GD2 resize operation. * * @ImageToolkitOperation( * id = "gd_resize", - * title = @Translation("GD2 toolkit resize"), - * toolkit_id = "gd", - * operation = "resize" + * label = "resize", + * description = @Translation("GD2 toolkit resize"), + * toolkit = "gd" * ) */ -class GDToolkitResize extends PluginBase { +class GDToolkitResize extends ImageToolkitOperationBase { /** - * {@inheritdoc} + * Scales an image to the specified size. + * + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. The $image->resource, $image->info['width'], and + * $image->info['height'] values will be modified by this call. + * @param int $width + * The new width of the resized image, in pixels. + * @param int $height + * The new height of the resized image, in pixels. + * + * @return bool + * TRUE or FALSE, based on success. */ - public function resize(ImageToolkitInterface $toolkit, ImageInterface $image, $width, $height) { - $res = $toolkit->operation('createTmp', array($image, $width, $height)); + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { + $res = $toolkit->process('createTmp', $image, array('width' => $variables['width'], 'height' => $variables['height'])); - if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $width, $height, $image->getWidth(), $image->getHeight())) { + if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $variables['width'], $variables['height'], $image->getWidth(), $image->getHeight())) { return FALSE; } @@ -39,8 +51,8 @@ public function resize(ImageToolkitInterface $toolkit, ImageInterface $image, $w // Update image object. $image ->setResource($res) - ->setWidth($width) - ->setHeight($height); + ->setWidth($variables['width']) + ->setHeight($variables['height']); return TRUE; } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitRotate.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitRotate.php index 82c2cf9..321cb86 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitRotate.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitRotate.php @@ -8,27 +8,46 @@ namespace Drupal\system\Plugin\ImageToolkitOperation; use Drupal\Core\Plugin\PluginBase; -use Drupal\system\Annotation\ImageToolkitOperation; -use Drupal\system\Plugin\ImageToolkitInterface; use Drupal\Core\Annotation\Translation; use Drupal\Core\Image\ImageInterface; +use Drupal\system\Annotation\ImageToolkitOperation; +use Drupal\system\Plugin\ImageToolkitInterface; +use Drupal\system\Plugin\ImageToolkitOperationBase; /** * Defines GD2 rotate operation. * * @ImageToolkitOperation( * id = "gd_rotate", - * title = @Translation("GD2 toolkit rotate"), - * toolkit_id = "gd", - * operation = "rotate" + * label = "rotate", + * description = @Translation("GD2 toolkit rotate"), + * toolkit = "gd" * ) */ -class GDToolkitRotate extends PluginBase { +class GDToolkitRotate extends ImageToolkitOperationBase { /** - * {@inheritdoc} + * Rotates an image the given number of degrees. + * + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. The $image->resource, $image->info['width'], and + * $image->info['height'] values will be modified by this call. + * @param int $degrees + * The number of (clockwise) degrees to rotate the image. + * @param string $background + * (optional) An hexadecimal integer specifying the background color to use + * for the uncovered area of the image after the rotation. E.g. 0x000000 for + * black, 0xff00ff for magenta, and 0xffffff for white. For images that + * support transparency, this will default to transparent. Otherwise it will + * be white. + * + * @return bool + * TRUE or FALSE, based on success. */ - public function rotate(ImageToolkitInterface $toolkit, ImageInterface $image, $degrees, $background = NULL) { + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { + // Ensure background variable is set. + $variables['background'] = isset($variables['background']) ? $variables['background'] : NULL; + // PHP installations using non-bundled GD do not have imagerotate. if (!function_exists('imagerotate')) { watchdog('image', 'The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', array('%file' => $image->getSource())); @@ -36,21 +55,21 @@ public function rotate(ImageToolkitInterface $toolkit, ImageInterface $image, $d } // Convert the hexadecimal background value to a color index value. - if (isset($background)) { + if (!empty($variables['background'])) { $rgb = array(); for ($i = 16; $i >= 0; $i -= 8) { - $rgb[] = (($background >> $i) & 0xFF); + $rgb[] = (($variables['background'] >> $i) & 0xFF); } - $background = imagecolorallocatealpha($image->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); + $variables['background'] = imagecolorallocatealpha($image->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); } - // Set the background color as transparent if $background is NULL. + // Set the background color as transparent if $variables['background'] is NULL. else { // Get the current transparent color. - $background = imagecolortransparent($image->getResource()); + $variables['background'] = imagecolortransparent($image->getResource()); // If no transparent colors, use white. - if ($background == 0) { - $background = imagecolorallocatealpha($image->getResource(), 255, 255, 255, 0); + if ($variables['background'] == 0) { + $variables['background'] = imagecolorallocatealpha($image->getResource(), 255, 255, 255, 0); } } @@ -63,12 +82,12 @@ public function rotate(ImageToolkitInterface $toolkit, ImageInterface $image, $d } } - $image->setResource(imagerotate($image->getResource(), 360 - $degrees, $background)); + $image->setResource(imagerotate($image->getResource(), 360 - $variables['degrees'], $variables['background'])); // GIFs need to reassign the transparent color after performing the rotate. if (isset($transparent_gif_color)) { - $background = imagecolorexactalpha($image->getResource(), $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']); - imagecolortransparent($image->getResource(), $background); + $variables['background'] = imagecolorexactalpha($image->getResource(), $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']); + imagecolortransparent($image->getResource(), $variables['background']); } $image diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationBase.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationBase.php new file mode 100644 index 0000000..473bf07 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationBase.php @@ -0,0 +1,40 @@ +toolkit = \Drupal::service('plugin.manager.image.toolkit')->createInstance($plugin_definition['toolkit']); + }*/ + + /** + * {@inheritdoc} + */ + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { + return FALSE; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php new file mode 100644 index 0000000..c1b0358 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php @@ -0,0 +1,31 @@ +discovery = new AlterDecorator($this->discovery, 'image_toolkit_operation'); $this->setCacheBackend($cache_backend, $language_manager, 'image_toolkit_operation'); }