diff --git a/core/lib/Drupal/Component/Plugin/Discovery/AnnotatedClassDiscovery.php b/core/lib/Drupal/Component/Plugin/Discovery/AnnotatedClassDiscovery.php index 5648b0d..9edfe91 100644 --- a/core/lib/Drupal/Component/Plugin/Discovery/AnnotatedClassDiscovery.php +++ b/core/lib/Drupal/Component/Plugin/Discovery/AnnotatedClassDiscovery.php @@ -115,6 +115,8 @@ public function getDefinitions() { // instead of requiring us to work with the annotation object. $definition = $annotation->get(); $definition['class'] = $class; + // Allow ID to be built later. Set an index ID as fallback. + $definition['id'] = isset($definition['id']) ? $definition['id'] : count($definitions); $definitions[$definition['id']] = $definition; } } diff --git a/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php index 4c1daa1..f2b1269 100644 --- a/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php +++ b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php @@ -19,11 +19,11 @@ class ImageToolkitOperation extends Plugin { /** - * The plugin ID. + * The machine name of the image toolkit operation implemented (e.g. "crop"). * * @var string */ - public $id; + public $operation; /** * The human-readable name of the image toolkit operation. @@ -54,11 +54,4 @@ 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/Operation/GDCrop.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDCrop.php new file mode 100644 index 0000000..ba1c8b5 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDCrop.php @@ -0,0 +1,66 @@ +createTmp($image, $data['width'], $data['height']); + + if (!imagecopyresampled($res, $image->getResource(), 0, 0, $data['x'], $data['y'], $data['width'], $data['height'], $data['width'], $data['height'])) { + return FALSE; + } + + // Destroy the original image and return the modified image. + imagedestroy($image->getResource()); + $image + ->setResource($res) + ->setWidth($data['width']) + ->setHeight($data['height']); + return TRUE; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDDesaturate.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDDesaturate.php new file mode 100644 index 0000000..61a4968 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDDesaturate.php @@ -0,0 +1,56 @@ + $image->getSource())); + return FALSE; + } + + return imagefilter($image->getResource(), IMG_FILTER_GRAYSCALE); + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDResize.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDResize.php new file mode 100644 index 0000000..bd976e3 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDResize.php @@ -0,0 +1,64 @@ +createTmp($image, $data['width'], $data['height']); + + if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $data['width'], $data['height'], $image->getWidth(), $image->getHeight())) { + return FALSE; + } + + imagedestroy($image->getResource()); + // Update image object. + $image + ->setResource($res) + ->setWidth($data['width']) + ->setHeight($data['height']); + return TRUE; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDRotate.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDRotate.php new file mode 100644 index 0000000..47ab5d3 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDRotate.php @@ -0,0 +1,104 @@ + $image->getSource())); + return FALSE; + } + + // Convert the hexadecimal background value to a color index value. + if (!empty($data['background'])) { + $rgb = array(); + for ($i = 16; $i >= 0; $i -= 8) { + $rgb[] = (($data['background'] >> $i) & 0xFF); + } + $data['background'] = imagecolorallocatealpha($image->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); + } + // Set the background color as transparent if $data['background'] is NULL. + else { + // Get the current transparent color. + $data['background'] = imagecolortransparent($image->getResource()); + + // If no transparent colors, use white. + if ($data['background'] == 0) { + $data['background'] = imagecolorallocatealpha($image->getResource(), 255, 255, 255, 0); + } + } + + // Images are assigned a new color palette when rotating, removing any + // transparency flags. For GIF images, keep a record of the transparent color. + if ($image->getType() == IMAGETYPE_GIF) { + $transparent_index = imagecolortransparent($image->getResource()); + if ($transparent_index != 0) { + $transparent_gif_color = imagecolorsforindex($image->getResource(), $transparent_index); + } + } + + $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)) { + $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 + ->setWidth(imagesx($image->getResource())) + ->setHeight(imagesy($image->getResource())); + return TRUE; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php index 6c552b3..8e24074 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php @@ -47,9 +47,7 @@ public function process($operation, ImageInterface $image, array $data = array() if (isset($this->operationPlugins[$operation])) { return $this->operationPlugins[$operation]->process($this, $image, $data); } - else { - return FALSE; - } + return FALSE; } } diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php deleted file mode 100644 index eb0720a..0000000 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitCrop.php +++ /dev/null @@ -1,67 +0,0 @@ -createTmp($image, $data['width'], $data['height']); - - if (!imagecopyresampled($res, $image->getResource(), 0, 0, $data['x'], $data['y'], $data['width'], $data['height'], $data['width'], $data['height'])) { - return FALSE; - } - - // Destroy the original image and return the modified image. - imagedestroy($image->getResource()); - $image - ->setResource($res) - ->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 deleted file mode 100644 index 5050d08..0000000 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitDesaturate.php +++ /dev/null @@ -1,57 +0,0 @@ - $image->getSource())); - return FALSE; - } - - return imagefilter($image->getResource(), IMG_FILTER_GRAYSCALE); - } - -} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php deleted file mode 100644 index 1330b1d..0000000 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitResize.php +++ /dev/null @@ -1,65 +0,0 @@ -createTmp($image, $data['width'], $data['height']); - - if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $data['width'], $data['height'], $image->getWidth(), $image->getHeight())) { - return FALSE; - } - - imagedestroy($image->getResource()); - // Update image object. - $image - ->setResource($res) - ->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 deleted file mode 100644 index 7c28ae9..0000000 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperation/GDToolkitRotate.php +++ /dev/null @@ -1,105 +0,0 @@ - $image->getSource())); - return FALSE; - } - - // Convert the hexadecimal background value to a color index value. - if (!empty($data['background'])) { - $rgb = array(); - for ($i = 16; $i >= 0; $i -= 8) { - $rgb[] = (($data['background'] >> $i) & 0xFF); - } - $data['background'] = imagecolorallocatealpha($image->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); - } - // Set the background color as transparent if $data['background'] is NULL. - else { - // Get the current transparent color. - $data['background'] = imagecolortransparent($image->getResource()); - - // If no transparent colors, use white. - if ($data['background'] == 0) { - $data['background'] = imagecolorallocatealpha($image->getResource(), 255, 255, 255, 0); - } - } - - // Images are assigned a new color palette when rotating, removing any - // transparency flags. For GIF images, keep a record of the transparent color. - if ($image->getType() == IMAGETYPE_GIF) { - $transparent_index = imagecolortransparent($image->getResource()); - if ($transparent_index != 0) { - $transparent_gif_color = imagecolorsforindex($image->getResource(), $transparent_index); - } - } - - $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)) { - $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 - ->setWidth(imagesx($image->getResource())) - ->setHeight(imagesy($image->getResource())); - return TRUE; - } - -} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php index b84159b..b03d814 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php @@ -29,25 +29,41 @@ class ImageToolkitOperationManager extends DefaultPluginManager { * The language manager. */ public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager) { - parent::__construct('Plugin/ImageToolkitOperation', $namespaces, 'Drupal\system\Annotation\ImageToolkitOperation'); - $this->discovery = new AlterDecorator($this->discovery, 'image_toolkit_operation'); + parent::__construct('Plugin/ImageToolkit/Operation', $namespaces, 'Drupal\system\Annotation\ImageToolkitOperation'); + + // Create valid IDs for plugin definitions. + $definitions = array(); + foreach ($this->discovery->getDefinitions() as $definition) { + $id = $definition['provider'] . '.' . $definition['toolkit'] . '.' . $definition['operation']; + $definition['id'] = $id; + $definitions[$id] = $definition; + } + $this->definitions = $definitions; + $this->setCacheBackend($cache_backend, $language_manager, 'image_toolkit_operation'); } /** - * @todo + * Returns the plugin ID for the plugin having the lowest weight, for a given + * toolkit and operation. + * + * @param string $toolkit + * The toolkit ID. + * @param string $operation + * The operation (e.g. "crop"). + * + * @return string + * A full image toolkit operation plugin ID (e.g. "system.gd.crop"). */ - public function getToolkitOperationPluginId($toolkit_id, $operation) { - $result = NULL; - $resultWeight = PHP_INT_MAX; + public function getToolkitOperationPluginId($toolkit, $operation) { $definitions = $this->getDefinitions(); + uasort($definitions, '\Drupal\Component\Utility\SortArray::sortByWeightElement'); + $pattern = '|\\.' . $toolkit . '\\.' . $operation . '$|'; foreach ($definitions as $plugin_id => $definition) { - if ($definition['toolkit'] === $toolkit_id && $definition['operation'] === $operation && $definition['weight'] < $resultWeight) { - $result = $plugin_id; - $resultWeight = $definition['weight']; + if (preg_match($pattern, $plugin_id)) { + return $plugin_id; } } - return $result; } }