From eeda4c602d8bb57810b33094718c20b2b9689ab3 Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Wed, 2 Oct 2013 18:01:37 +0300 Subject: [PATCH] Issue #2073759 by mondrake, claudiu.cristea | fietserwin: Allow contrib to implement toolkit specific code for image effects. --- core/core.services.yml | 3 + .../Plugin/Discovery/AnnotatedClassDiscovery.php | 2 + core/lib/Drupal/Core/Image/Image.php | 8 +- .../system/Annotation/ImageToolkitOperation.php | 57 +++++++++++ .../system/Plugin/ImageToolkit/GDToolkit.php | 109 +-------------------- .../Plugin/ImageToolkit/Operation/GDCrop.php | 66 +++++++++++++ .../Plugin/ImageToolkit/Operation/GDDesaturate.php | 56 +++++++++++ .../Plugin/ImageToolkit/Operation/GDResize.php | 64 ++++++++++++ .../Plugin/ImageToolkit/Operation/GDRotate.php | 104 ++++++++++++++++++++ .../lib/Drupal/system/Plugin/ImageToolkitBase.php | 53 ++++++++++ .../Drupal/system/Plugin/ImageToolkitInterface.php | 87 +++------------- .../system/Plugin/ImageToolkitOperationBase.php | 25 +++++ .../Plugin/ImageToolkitOperationInterface.php | 37 +++++++ .../system/Plugin/ImageToolkitOperationManager.php | 69 +++++++++++++ .../image_test/Plugin/ImageToolkit/TestToolkit.php | 37 +------ .../ImageToolkitOperation/TestToolkitCrop.php | 58 +++++++++++ .../TestToolkitDesaturate.php | 52 ++++++++++ .../ImageToolkitOperation/TestToolkitResize.php | 54 ++++++++++ .../ImageToolkitOperation/TestToolkitRotate.php | 60 ++++++++++++ core/tests/Drupal/Tests/Core/Image/ImageTest.php | 105 ++++++++++---------- 20 files changed, 841 insertions(+), 265 deletions(-) create mode 100644 core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php create mode 100644 core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDCrop.php create mode 100644 core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDDesaturate.php create mode 100644 core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDResize.php create mode 100644 core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/Operation/GDRotate.php create mode 100644 core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php create mode 100644 core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationBase.php create mode 100644 core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationInterface.php create mode 100644 core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationManager.php create mode 100644 core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitCrop.php create mode 100644 core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitDesaturate.php create mode 100644 core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitResize.php create mode 100644 core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitRotate.php diff --git a/core/core.services.yml b/core/core.services.yml index 24ef51e..7d0bb62 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -527,6 +527,9 @@ services: image.toolkit.manager: class: Drupal\system\Plugin\ImageToolkitManager arguments: ['@container.namespaces', '@cache.cache', '@language_manager'] + image.toolkit.operation.manager: + class: Drupal\system\Plugin\ImageToolkitOperationManager + arguments: ['@container.namespaces', '@cache.cache', '@language_manager'] image.toolkit: class: Drupal\system\Plugin\ImageToolkitInterface factory_method: getDefaultToolkit diff --git a/core/lib/Drupal/Component/Plugin/Discovery/AnnotatedClassDiscovery.php b/core/lib/Drupal/Component/Plugin/Discovery/AnnotatedClassDiscovery.php index 5e522fb..d296100 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/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 0f2fc85..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->crop($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->resize($this, $width, $height); + return $this->toolkit->process('resize', $this, array('width' => $width, 'height' => $height)); } /** * {@inheritdoc} */ public function desaturate() { - return $this->toolkit->desaturate($this); + return $this->toolkit->process('desaturate', $this); } /** * {@inheritdoc} */ public function rotate($degrees, $background = NULL) { - return $this->toolkit->rotate($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 new file mode 100644 index 0000000..f2b1269 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Annotation/ImageToolkitOperation.php @@ -0,0 +1,57 @@ +createTmp($image, $width, $height); - - if (!imagecopyresampled($res, $image->getResource(), 0, 0, 0, 0, $width, $height, $image->getWidth(), $image->getHeight())) { - return FALSE; - } - - imagedestroy($image->getResource()); - // Update image object. - $image - ->setResource($res) - ->setWidth($width) - ->setHeight($height); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function rotate(ImageInterface $image, $degrees, $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())); - return FALSE; - } - - // Convert the hexadecimal background value to a color index value. - if (isset($background)) { - $rgb = array(); - for ($i = 16; $i >= 0; $i -= 8) { - $rgb[] = (($background >> $i) & 0xFF); - } - $background = imagecolorallocatealpha($image->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); - } - // Set the background color as transparent if $background is NULL. - else { - // Get the current transparent color. - $background = imagecolortransparent($image->getResource()); - - // If no transparent colors, use white. - if ($background == 0) { - $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 - $degrees, $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); - } - - $image - ->setWidth(imagesx($image->getResource())) - ->setHeight(imagesy($image->getResource())); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function crop(ImageInterface $image, $x, $y, $width, $height) { - $res = $this->createTmp($image, $width, $height); - - if (!imagecopyresampled($res, $image->getResource(), 0, 0, $x, $y, $width, $height, $width, $height)) { - return FALSE; - } - - // Destroy the original image and return the modified image. - imagedestroy($image->getResource()); - $image - ->setResource($res) - ->setWidth($width) - ->setHeight($height); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function desaturate(ImageInterface $image) { - // 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())); - return FALSE; - } - - return imagefilter($image->getResource(), IMG_FILTER_GRAYSCALE); - } - - /** - * {@inheritdoc} - */ public function load(ImageInterface $image) { $function = 'imagecreatefrom' . image_type_to_extension($image->getType(), FALSE); if (function_exists($function) && $resource = $function($image->getSource())) { @@ -293,4 +191,5 @@ public static function isAvailable() { public static function supportedTypes() { return array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF); } + } 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 new file mode 100644 index 0000000..8e24074 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitBase.php @@ -0,0 +1,53 @@ +operationPlugins[$operation])) { + $manager = \Drupal::service('image.toolkit.operation.manager'); + $plugin_id = $manager->getToolkitOperationPluginId($this->getPluginId(), $operation); + $this->operationPlugins[$operation] = $manager->createInstance($plugin_id); + } + // Return the the value returned by the operation process method. + if (isset($this->operationPlugins[$operation])) { + return $this->operationPlugins[$operation]->process($this, $image, $data); + } + return FALSE; + } + +} diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php index 5918160..cbc3bfa 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitInterface.php @@ -60,78 +60,6 @@ public function settingsForm(); public function settingsFormSubmit($form, &$form_state); /** - * 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(ImageInterface $image, $width, $height); - - /** - * 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(ImageInterface $image, $degrees, $background = NULL); - - /** - * 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(ImageInterface $image, $x, $y, $width, $height); - - /** - * 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(ImageInterface $image); - - /** * Creates an image resource from a file. * * @param \Drupal\Core\Image\ImageInterface $image @@ -189,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 $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()); } 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..8728865 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitOperationBase.php @@ -0,0 +1,25 @@ +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'); + } + + /** + * 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, $operation) { + $definitions = $this->getDefinitions(); + uasort($definitions, '\Drupal\Component\Utility\SortArray::sortByWeightElement'); + $pattern = '|\\.' . $toolkit . '\\.' . $operation . '$|'; + foreach ($definitions as $plugin_id => $definition) { + if (preg_match($pattern, $plugin_id)) { + return $plugin_id; + } + } + } + +} diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php index 778e9e2..508bd47 100644 --- a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.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("A dummy toolkit that works") * ) */ -class TestToolkit extends PluginBase implements ImageToolkitInterface { +class TestToolkit extends ImageToolkitBase implements ImageToolkitInterface { /** * {@inheritdoc} @@ -76,38 +77,6 @@ public function save(ImageInterface $image, $destination) { } /** - * {@inheritdoc} - */ - public function crop(ImageInterface $image, $x, $y, $width, $height) { - $this->logCall('crop', array($image, $x, $y, $width, $height)); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function resize(ImageInterface $image, $width, $height) { - $this->logCall('resize', array($image, $width, $height)); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function rotate(ImageInterface $image, $degrees, $background = NULL) { - $this->logCall('rotate', array($image, $degrees, $background)); - return TRUE; - } - - /** - * {@inheritdoc} - */ - public function desaturate(ImageInterface $image) { - $this->logCall('desaturate', array($image)); - return TRUE; - } - - /** * Stores the values passed to a toolkit call. * * @param string $op @@ -119,7 +88,7 @@ public function desaturate(ImageInterface $image) { * @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestReset() * @see \Drupal\system\Tests\Image\ToolkitTestBase::imageTestGetAllCalls() */ - protected function logCall($op, $args) { + public function logCall($op, $args) { $results = \Drupal::state()->get('image_test.results') ?: array(); $results[$op][] = $args; \Drupal::state()->set('image_test.results', $results); diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitCrop.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitCrop.php new file mode 100644 index 0000000..eaa6643 --- /dev/null +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitCrop.php @@ -0,0 +1,58 @@ +logCall('crop', array($image, $data['x'], $data['y'], $data['width'], $data['height'])); + return TRUE; + } + +} diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitDesaturate.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitDesaturate.php new file mode 100644 index 0000000..a60a3e0 --- /dev/null +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitDesaturate.php @@ -0,0 +1,52 @@ +logCall('desaturate', array($image)); + return TRUE; + } + +} diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitResize.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitResize.php new file mode 100644 index 0000000..9026534 --- /dev/null +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitResize.php @@ -0,0 +1,54 @@ +logCall('resize', array($image, $data['width'], $data['height'])); + return TRUE; + } + +} diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitRotate.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitRotate.php new file mode 100644 index 0000000..7aaf0aa --- /dev/null +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkitOperation/TestToolkitRotate.php @@ -0,0 +1,60 @@ +logCall('rotate', array($image, $data['degrees'], $data['background'])); + return TRUE; + } + +} diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index 700973e..b393d00 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -219,10 +219,11 @@ public function testProcessInfoFails() { */ public function testScaleWidth() { $this->toolkit->expects($this->once()) - ->method('resize') + ->method('process') + ->with($this->equalTo('resize')) ->will($this->returnArgument(2)); - $height = $this->image->scale(44); - $this->assertEquals($height, 50); + $result = $this->image->scale(44); + $this->assertEquals($result['height'], 50); } /** @@ -230,11 +231,12 @@ public function testScaleWidth() { */ public function testScaleHeight() { $this->toolkit->expects($this->once()) - ->method('resize') - ->will($this->returnArgument(1)); + ->method('process') + ->with($this->equalTo('resize')) + ->will($this->returnArgument(2)); - $width = $this->image->scale(NULL, 50); - $this->assertEquals($width, 44); + $result = $this->image->scale(NULL, 50); + $this->assertEquals($result['width'], 44); } /** @@ -243,55 +245,52 @@ public function testScaleHeight() { public function testScaleSame() { // Dimensions are the same, resize should not be called. $this->toolkit->expects($this->never()) - ->method('resize') - ->will($this->returnArgument(1)); + ->method('process'); - $width = $this->image->scale(88, 100); - $this->assertEquals($width, 88); + $this->image->scale(88, 100); } /** * Tests \Drupal\Core\Image\Image::scaleAndCrop(). */ public function testScaleAndCropWidth() { - $this->toolkit->expects($this->once()) - ->method('resize') - ->will($this->returnValue(TRUE)); - - $this->toolkit->expects($this->once()) - ->method('crop') - ->will($this->returnArgument(1)); + $this->toolkit->expects($this->exactly(2)) + ->method('process') + ->with($this->logicalOr( + $this->equalTo('resize'), + $this->equalTo('crop') + )) + ->will($this->returnArgument(2)); - $x = $this->image->scaleAndCrop(34, 50); - $this->assertEquals($x, 5); + $result = $this->image->scaleAndCrop(34, 50); + $this->assertEquals($result['x'], 5); } /** * Tests \Drupal\Core\Image\Image::scaleAndCrop(). */ public function testScaleAndCropHeight() { - $this->toolkit->expects($this->once()) - ->method('resize') - ->will($this->returnValue(TRUE)); - - $this->toolkit->expects($this->once()) - ->method('crop') + $this->toolkit->expects($this->exactly(2)) + ->method('process') + ->with($this->logicalOr( + $this->equalTo('resize'), + $this->equalTo('crop') + )) ->will($this->returnArgument(2)); - $y = $this->image->scaleAndCrop(44, 40); - $this->assertEquals($y, 5); + $result = $this->image->scaleAndCrop(44, 40); + $this->assertEquals($result['y'], 5); } /** * Tests \Drupal\Core\Image\Image::scaleAndCrop(). */ public function testScaleAndCropFails() { - $this->toolkit->expects($this->once()) - ->method('resize') + $this->toolkit->expects($this->exactly(1)) + ->method('process') + ->with($this->equalTo('resize')) ->will($this->returnValue(FALSE)); - $this->toolkit->expects($this->never()) - ->method('crop'); $this->image->scaleAndCrop(44, 40); } @@ -300,11 +299,12 @@ public function testScaleAndCropFails() { */ public function testCropWidth() { $this->toolkit->expects($this->once()) - ->method('crop') - ->will($this->returnArgument(4)); + ->method('process') + ->with($this->equalTo('crop')) + ->will($this->returnArgument(2)); // Cropping with width only should preserve the aspect ratio. - $height = $this->image->crop(0, 0, 44, NULL); - $this->assertEquals($height, 50); + $result = $this->image->crop(0, 0, 44, NULL); + $this->assertEquals($result['height'], 50); } /** @@ -312,11 +312,12 @@ public function testCropWidth() { */ public function testCropHeight() { $this->toolkit->expects($this->once()) - ->method('crop') - ->will($this->returnArgument(3)); + ->method('process') + ->with($this->equalTo('crop')) + ->will($this->returnArgument(2)); // Cropping with height only should preserve the aspect ratio. - $width = $this->image->crop(0, 0, NULL, 50); - $this->assertEquals($width, 44); + $result = $this->image->crop(0, 0, NULL, 50); + $this->assertEquals($result['width'], 44); } /** @@ -324,10 +325,11 @@ public function testCropHeight() { */ public function testCrop() { $this->toolkit->expects($this->once()) - ->method('crop') - ->will($this->returnArgument(3)); - $width = $this->image->crop(0, 0, 44, 50); - $this->assertEquals($width, 44); + ->method('process') + ->with($this->equalTo('crop')) + ->will($this->returnArgument(2)); + $result = $this->image->crop(0, 0, 44, 50); + $this->assertEquals($result['width'], 44); } /** @@ -335,14 +337,15 @@ public function testCrop() { */ public function testResize() { $this->toolkit->expects($this->exactly(2)) - ->method('resize') - ->will($this->returnArgument(1)); + ->method('process') + ->with($this->equalTo('resize')) + ->will($this->returnArgument(2)); // Resize with integer for width and height. $this->image->resize(30, 40); // Pass a float for width. - $width = $this->image->resize(30.4, 40); + $result = $this->image->resize(30.4, 40); // Ensure that the float was rounded to an integer first. - $this->assertEquals($width, 30); + $this->assertEquals($result['width'], 30); } /** @@ -350,7 +353,8 @@ public function testResize() { */ public function testDesaturate() { $this->toolkit->expects($this->once()) - ->method('desaturate'); + ->method('process') + ->with($this->equalTo('desaturate')); $this->image->desaturate(); } @@ -359,7 +363,8 @@ public function testDesaturate() { */ public function testRotate() { $this->toolkit->expects($this->once()) - ->method('rotate'); + ->method('process') + ->with($this->equalTo('rotate')); $this->image->rotate(90); } -- 1.8.3.1