diff --git a/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php index d4b1a77..4c1daa1 100644 --- a/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php +++ b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php @@ -26,9 +26,13 @@ class ImageToolkitOperation extends Plugin { public $id; /** - * The label of the image toolkit operation. + * The human-readable name of the image toolkit operation. * - * @var string + * The string should be wrapped in a @Translation(). + * + * @ingroup plugin_translatable + * + * @var \Drupal\Core\Annotation\Translation */ public $label; @@ -50,4 +54,11 @@ class ImageToolkitOperation extends Plugin { */ public $toolkit; + /** + * The machine name of the image toolkit operation implemented. + * + * @var string + */ + public $operation; + } 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 f6aa276..3a4bc5a 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php @@ -59,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->process('createTmp', $image, array('width' => $image->getWidth(), 'height' => $image->getHeight())); + $new_image = $this->createTmp($image, $image->getWidth(), $image->getHeight()); imagecopy($new_image, $resource, 0, 0, 0, 0, $image->getWidth(), $image->getHeight()); imagedestroy($resource); $image->setResource($new_image); @@ -129,6 +129,50 @@ public function getInfo(ImageInterface $image) { } /** + * Creates a truecolor image preserving transparency from a provided image. + * + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. + * @param int $width + * The new width of the new image, in pixels. + * @param int $height + * The new height of the new image, in pixels. + * + * @return resource + * A GD image handle. + */ + public function createTmp(ImageInterface $image, $width, $height) { + $res = imagecreatetruecolor($width, $height); + + if ($image->getType() == IMAGETYPE_GIF) { + // Grab transparent color index from image resource. + $transparent = imagecolortransparent($image->getResource()); + + if ($transparent >= 0) { + // The original must have a transparent color, allocate to the new image. + $transparent_color = imagecolorsforindex($image->getResource(), $transparent); + $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); + + // Flood with our new transparent color. + imagefill($res, 0, 0, $transparent); + imagecolortransparent($res, $transparent); + } + } + elseif ($image->getType() == IMAGETYPE_PNG) { + imagealphablending($res, FALSE); + $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); + imagefill($res, 0, 0, $transparency); + imagealphablending($res, TRUE); + imagesavealpha($res, TRUE); + } + else { + imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255)); + } + + return $res; + } + + /** * {@inheritdoc} */ public static function isAvailable() { diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php index 52a113c..ffb3554 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php @@ -13,63 +13,41 @@ /** * Base class implementation of an image toolkit. */ -abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface { - - /** - * {@inheritdoc} - */ - public function settingsForm() { - return array(); - } - - /** - * {@inheritdoc} - */ - public function settingsFormSubmit($form, &$form_state) { - } - - /** - * {@inheritdoc} - */ - public function load(ImageInterface $image) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function save(ImageInterface $image, $destination) { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public function getInfo(ImageInterface $image) { - return NULL; - } - - /** - * {@inheritdoc} - */ - public static function isAvailable() { - return FALSE; - } - - /** - * {@inheritdoc} - */ - public static function supportedTypes() { - return NULL; - } - - /** - * {@inheritdoc} - */ - public function process($operation, ImageInterface $image, array $variables = NULL) { - $plugin_id = ($operation == 'createTmp') ? 'gd_create_tmp' : 'gd_' . $operation; - $plugin = \Drupal::service('image.toolkit.operation.manager')->createInstance($plugin_id); - return $plugin->process($this, $image, $variables); +abstract class ImageToolkitBase extends PluginBase { + + /** + * Stores instantiated image toolkit operation plugins. + * + * @var array + */ + protected $operationPlugins = array(); + + /** + * Processes a toolkit operation. + * + * @param string $operation + * The toolkit operation to be processed. + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. + * @param array $data + * An array of variables to be used by the toolkit operation. + * + * @return mixed + * The return value of the image toolkit operation. + */ + public function process($operation, ImageInterface $image, array $data = array()) { + // Check if operation plugin has been instantiated already. If not, + // create an instance. + if (isset($this->operationPlugins[$operation])) { + $plugin = $this->operationPlugins[$operation]; + } + else { + $manager = \Drupal::service('image.toolkit.operation.manager'); + $plugin_id = $manager->getToolkitOperationPluginId($this->getPluginId(), $operation); + $plugin = $manager->createInstance($plugin_id); + $this->operationPlugins[$operation] = $plugin; + } + return $plugin->process($this, $image, $data); } } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php index 14f66d0..cbc3bfa 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php @@ -125,11 +125,11 @@ public static function supportedTypes(); * The toolkit operation to be processed. * @param \Drupal\Core\Image\ImageInterface $image * An image object. - * @param array $variables + * @param array $data * An array of variables to be used by the toolkit operation. * - * @return bool - * TRUE if the operation is successful, FALSE otherwise. + * @return mixed + * The return value of the image toolkit operation. */ - public function process($operation, ImageInterface $image, array $variables = NULL); + public function process($operation, ImageInterface $image, array $data = array()); } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCreateTmp.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCreateTmp.php deleted file mode 100644 index e301e21..0000000 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCreateTmp.php +++ /dev/null @@ -1,73 +0,0 @@ -getType() == IMAGETYPE_GIF) { - // Grab transparent color index from image resource. - $transparent = imagecolortransparent($image->getResource()); - - if ($transparent >= 0) { - // The original must have a transparent color, allocate to the new image. - $transparent_color = imagecolorsforindex($image->getResource(), $transparent); - $transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); - - // Flood with our new transparent color. - imagefill($res, 0, 0, $transparent); - imagecolortransparent($res, $transparent); - } - } - elseif ($image->getType() == IMAGETYPE_PNG) { - imagealphablending($res, FALSE); - $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127); - imagefill($res, 0, 0, $transparency); - imagealphablending($res, TRUE); - imagesavealpha($res, TRUE); - } - else { - imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255)); - } - - return $res; - } - -} 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 7f7d887..9cc7a72 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php @@ -18,10 +18,12 @@ * Defines GD2 Crop operation. * * @ImageToolkitOperation( - * id = "gd_crop", - * label = "crop", + * id = "system_gd_crop", + * label = @Translation("Crop"), * description = @Translation("GD2 toolkit crop"), - * toolkit = "gd" + * toolkit = "gd", + * operation = "crop", + * weight = 0 * ) */ class GDToolkitCrop extends ImageToolkitOperationBase { @@ -29,27 +31,29 @@ class GDToolkitCrop extends ImageToolkitOperationBase { /** * Crops an image. * + * The image resource, width, and height values will be modified by this + * call. + * + * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit + * An image toolkit. * @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. + * An image object. + * @param array $data + * An array of data to be used by the toolkit operation. + * - x: The starting x offset at which to start the crop, in pixels. + * - y: The starting y offset at which to start the crop, in pixels. + * - width: The width of the cropped area, in pixels. + * - height: The height of the cropped area, in pixels. * * @return bool * TRUE or FALSE, based on success. * * @see image_crop() */ - public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { - $res = $toolkit->process('createTmp', $image, array('width' => $variables['width'], 'height' => $variables['height'])); + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()) { + $res = $toolkit->createTmp($image, $data['width'], $data['height']); - if (!imagecopyresampled($res, $image->getResource(), 0, 0, $variables['x'], $variables['y'], $variables['width'], $variables['height'], $variables['width'], $variables['height'])) { + if (!imagecopyresampled($res, $image->getResource(), 0, 0, $data['x'], $data['y'], $data['width'], $data['height'], $data['width'], $data['height'])) { return FALSE; } @@ -57,8 +61,8 @@ public function process(ImageToolkitInterface $toolkit, ImageInterface $image, a imagedestroy($image->getResource()); $image ->setResource($res) - ->setWidth($variables['width']) - ->setHeight($variables['height']); + ->setWidth($data['width']) + ->setHeight($data['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 fd96215..5050d08 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitDesaturate.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitDesaturate.php @@ -18,10 +18,12 @@ * Defines GD2 Desaturate operation. * * @ImageToolkitOperation( - * id = "gd_desaturate", - * label = "desaturate", + * id = "system_gd_desaturate", + * label = @Translation("Desaturate"), * description = @Translation("GD2 toolkit desaturate"), - * toolkit = "gd" + * toolkit = "gd", + * operation = "desaturate", + * weight = 0 * ) */ class GDToolkitDesaturate extends ImageToolkitOperationBase { @@ -29,16 +31,20 @@ class GDToolkitDesaturate extends ImageToolkitOperationBase { /** * Converts an image resource to grayscale. * - * Note that transparent GIFs loose transparency when desaturated. + * Note that transparent GIFs loose transparency when desaturated. The image + * resource value will be modified by this call. * + * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit + * An image toolkit. * @param \Drupal\Core\Image\ImageInterface $image - * An image object. The $image->resource value will be modified by this - * call. + * An image object. + * @param array $data + * Not used, should be left empty. * * @return bool * TRUE or FALSE, based on success. */ - public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()) { // 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 1fa4195..1330b1d 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php @@ -18,10 +18,12 @@ * Defines GD2 resize operation. * * @ImageToolkitOperation( - * id = "gd_resize", - * label = "resize", + * id = "system_gd_resize", + * label = @Translation("Resize"), * description = @Translation("GD2 toolkit resize"), - * toolkit = "gd" + * toolkit = "gd", + * operation = "resize", + * weight = 0 * ) */ class GDToolkitResize extends ImageToolkitOperationBase { @@ -29,21 +31,25 @@ class GDToolkitResize extends ImageToolkitOperationBase { /** * Scales an image to the specified size. * + * The image resource, width, and height values will be modified by this + * call. + * + * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit + * An image toolkit. * @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. + * An image object. + * @param array $data + * An array of data to be used by the toolkit operation. + * - width: The new width of the resized image, in pixels. + * - height: The new height of the resized image, in pixels. * * @return bool * TRUE or FALSE, based on success. */ - public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { - $res = $toolkit->process('createTmp', $image, array('width' => $variables['width'], 'height' => $variables['height'])); + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()) { + $res = $toolkit->createTmp($image, $data['width'], $data['height']); - if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $variables['width'], $variables['height'], $image->getWidth(), $image->getHeight())) { + if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $data['width'], $data['height'], $image->getWidth(), $image->getHeight())) { return FALSE; } @@ -51,8 +57,8 @@ public function process(ImageToolkitInterface $toolkit, ImageInterface $image, a // Update image object. $image ->setResource($res) - ->setWidth($variables['width']) - ->setHeight($variables['height']); + ->setWidth($data['width']) + ->setHeight($data['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 321cb86..7c28ae9 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitRotate.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitRotate.php @@ -18,10 +18,12 @@ * Defines GD2 rotate operation. * * @ImageToolkitOperation( - * id = "gd_rotate", - * label = "rotate", + * id = "system_gd_rotate", + * label = @Translation("Rotate"), * description = @Translation("GD2 toolkit rotate"), - * toolkit = "gd" + * toolkit = "gd", + * operation = "rotate", + * weight = 0 * ) */ class GDToolkitRotate extends ImageToolkitOperationBase { @@ -29,24 +31,28 @@ class GDToolkitRotate extends ImageToolkitOperationBase { /** * Rotates an image the given number of degrees. * + * An image object. The image resource, width, and height values will + * be modified by this call. + * + * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit + * An image toolkit. * @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. + * An image object. + * @param array $data + * An array of data to be used by the toolkit operation. + * - degrees: The number of (clockwise) degrees to rotate the image. + * - 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 process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()) { // Ensure background variable is set. - $variables['background'] = isset($variables['background']) ? $variables['background'] : NULL; + $data['background'] = isset($data['background']) ? $data['background'] : NULL; // PHP installations using non-bundled GD do not have imagerotate. if (!function_exists('imagerotate')) { @@ -55,21 +61,21 @@ public function process(ImageToolkitInterface $toolkit, ImageInterface $image, a } // Convert the hexadecimal background value to a color index value. - if (!empty($variables['background'])) { + if (!empty($data['background'])) { $rgb = array(); for ($i = 16; $i >= 0; $i -= 8) { - $rgb[] = (($variables['background'] >> $i) & 0xFF); + $rgb[] = (($data['background'] >> $i) & 0xFF); } - $variables['background'] = imagecolorallocatealpha($image->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); + $data['background'] = imagecolorallocatealpha($image->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); } - // Set the background color as transparent if $variables['background'] is NULL. + // Set the background color as transparent if $data['background'] is NULL. else { // Get the current transparent color. - $variables['background'] = imagecolortransparent($image->getResource()); + $data['background'] = imagecolortransparent($image->getResource()); // If no transparent colors, use white. - if ($variables['background'] == 0) { - $variables['background'] = imagecolorallocatealpha($image->getResource(), 255, 255, 255, 0); + if ($data['background'] == 0) { + $data['background'] = imagecolorallocatealpha($image->getResource(), 255, 255, 255, 0); } } @@ -82,12 +88,12 @@ public function process(ImageToolkitInterface $toolkit, ImageInterface $image, a } } - $image->setResource(imagerotate($image->getResource(), 360 - $variables['degrees'], $variables['background'])); + $image->setResource(imagerotate($image->getResource(), 360 - $data['degrees'], $data['background'])); // GIFs need to reassign the transparent color after performing the rotate. if (isset($transparent_gif_color)) { - $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']); + $data['background'] = imagecolorexactalpha($image->getResource(), $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']); + imagecolortransparent($image->getResource(), $data['background']); } $image diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationBase.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationBase.php index 473bf07..8728865 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationBase.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationBase.php @@ -18,23 +18,8 @@ abstract class ImageToolkitOperationBase extends PluginBase implements ImageToolkitOperationInterface { /** - * @todo - */ -// protected $toolkit; - - /** - * {@inheritdoc} - */ -/* public function __construct(array $configuration, $plugin_id, array $plugin_definition) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->toolkit = \Drupal::service('plugin.manager.image.toolkit')->createInstance($plugin_definition['toolkit']); - }*/ - - /** * {@inheritdoc} */ - public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL) { - return FALSE; - } + public abstract function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()); } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php index c1b0358..b67347e 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php @@ -12,10 +12,6 @@ use Drupal\system\Plugin\ImageToolkitInterface; /** - * @todo - */ - -/** * Defines an interface for image toolkit operations. * * An image toolkit operation provides image manipulations like scaling, @@ -24,8 +20,18 @@ interface ImageToolkitOperationInterface extends PluginInspectionInterface { /** - * @todo + * Processes an image toolkit operation. + * + * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit + * An image toolkit. + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. + * @param array $data + * An array of data to be used by the toolkit operation. + * + * @return mixed + * The return value of the image toolkit operation. */ - public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $variables = NULL); + public function process(ImageToolkitInterface $toolkit, ImageInterface $image, array $data = array()); } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php index 4a4e09f..8452227 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php @@ -34,4 +34,20 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac $this->setCacheBackend($cache_backend, $language_manager, 'image_toolkit_operation'); } + /** + * @todo + */ + public function getToolkitOperationPluginId($toolkitName, $operation) { + $result = NULL; + $resultWeight = PHP_INT_MAX; + $definitions = $this->getDefinitions(); + foreach ($definitions as $plugin_id => $definition) { + if ($definition['toolkit'] === $toolkitName && $definition['operation'] === $operation && $definition['weight'] < $resultWeight) { + $result = $plugin_id; + $resultWeight = $definition['weight']; + } + } + return $result; + } + }