diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php index 4618827..6dcbd36 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @@ -61,33 +61,18 @@ protected function getToolkitOperation($operation) { * {@inheritdoc} */ public function apply($operation, ImageInterface $image, array $arguments = array()) { - // Get the plugin to use for the operation. If there are no plugins - // available, log an error message and return. try { - $plugin = $this->getToolkitOperation($operation); + // Get the plugin to use for the operation and apply the operation. + return $this->getToolkitOperation($operation)->apply($image, $arguments); } catch (PluginNotFoundException $e) { watchdog('image', 'The selected image handling toolkit %toolkit can not process operation %operation.', array('%toolkit' => $this->getPluginId(), '%operation' => $operation), WATCHDOG_ERROR); return FALSE; } - - // Validate and prepare arguments before applying the operation. The - // apply operation will not be executed if prepare() decides that the - // final image will not change. If there are errors in the arguments - // passed, log a warning message and return. - try { - $execute_apply = $plugin->prepare($image, $arguments); - } catch (\InvalidArgumentException $e) { watchdog('image', $e->getMessage(), array(), WATCHDOG_WARNING); return FALSE; } - if (!$execute_apply) { - return TRUE; - } - - // Apply the image operation and return its result. - return $plugin->apply($image, $arguments); } } diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php index b4f6310..5d3a815 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php @@ -7,6 +7,7 @@ namespace Drupal\Core\ImageToolkit; +use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Core\Image\ImageInterface; use Drupal\Core\Plugin\PluginBase; @@ -37,35 +38,146 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi } /** - * {@inheritdoc} + * Returns the image toolkit instance for this operation. + * + * Image toolkit implementers should provide a trait that overrides this + * method to correctly document the return type of this getter. This provides + * better DX (code checking and code completion) for image toolkit operation + * developers. + * + * @return \Drupal\Core\ImageToolkit\ImageToolkitInterface */ - public function arguments() { - return array(); + protected function getToolkit() { + return $this->toolkit; } /** - * {@inheritdoc} + * Returns the definition of the operation arguments. + * + * Image toolkit operation implementers must implement this method to + * "document" their operation, thus also if no arguments are expected. + * + * An array whose keys are the names of the arguments (e.g. "width", + * "degrees") and each value is an associative array having the following + * keys: + * - description: A string with the argument description. This is used only + * internally for documentation purposes, so it does not need to be + * translatable. + * - required: (optional) A boolean indicating if this argument should be + * provided or not. Defaults to TRUE. + * - default: (optional) When the argument is set to "required" = FALSE, + * this must be set to a default value. Ignored for "required" = TRUE + * arguments. + * + * @return array + */ + abstract protected function arguments(); + + /** + * Checks if required arguments are passed in and adds defaults for non passed + * in optional arguments. + * + * Image toolkit operation implementers should not normally need to override + * this method as they should place their own validation in validateArguments. + * + * @param array $arguments + * An associative array of arguments to be used by the toolkit operation. + * + * @return array + * The prepared arguments array. + * + * @throws \InvalidArgumentException. + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException. */ - public function prepare(ImageInterface $image, array &$arguments) { + protected function prepareArguments(array $arguments) { foreach ($this->arguments() as $id => $argument) { $argument += array('required' => TRUE); - // Check if the defined argument has been provided. - if (!array_key_exists($id, $arguments)) { - if ($argument['required']) { + // Check if the argument is required and, if so, has been provided. + if ($argument['required']) { + if (!array_key_exists($id, $arguments)) { // If the argument is required throw an exception. throw new \InvalidArgumentException("Argument '$id' expected by plugin '" . $this->getPluginId() . "' but not passed."); } - else { - // Optional argument? Assign the default value. - if (!array_key_exists('default', $argument)) { - // If the default is missing throw an exception. - throw new \InvalidArgumentException("Default for argument '$id' expected by plugin '" . $this->getPluginId() . "' not defined."); - } - $arguments[$id] = $argument['default']; + } + else { + // Optional arguments require a 'default' value. + // We check this even if the argument is provided by the caller, as we + // want to fail fast here, i.e. at development time. + if (!array_key_exists('default', $argument)) { + // The plugin did not define a default, so throw a plugin exception, + // not an invalid argument exception. + throw new InvalidPluginDefinitionException("Default for argument '$id' expected by plugin '" . $this->getPluginId() . "' but not defined."); + } + + // Use the default value if the argument is not passed in. + if (!array_key_exists($id, $arguments)) { + $arguments[$id] = $argument['default']; } } } - return TRUE; + return $arguments; } + /** + * Validates the arguments. + * + * Image toolkit operation implementers should place any argument validation + * in this method, throwing an InvalidArgumentException when an error is + * encountered. + * + * Validation typically includes things like: + * - Checking that width and height are not negative. + * - Checking that a color value is indeed a color. + * + * But validation may also include correcting the arguments, e.g: + * - Casting arguments to the correct type. + * - Rounding pixel values to an integer. + * + * This base implementation just returns the array of arguments and thus does + * not need to be called by overriding methods. + * + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. + * @param array $arguments + * An associative array of arguments to be used by the toolkit operation. + * + * @return array + * The validated and corrected arguments array. + * + * @throws \InvalidArgumentException. + * If one or more of the arguments are not valid. + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException. + * If the plugin does not define a default for an optional argument. + */ + protected function validateArguments(ImageInterface $image, array $arguments) { + return $arguments; + } + + /** + * {@inheritdoc} + */ + public final function apply(ImageInterface $image, array $arguments) { + $arguments = $this->prepareArguments($arguments); + $arguments = $this->validateArguments($image, $arguments); + return $this->execute($image, $arguments); + } + + /** + * Performs the actual manipulation on the image. + * + * Image toolkit operation implementers must implement this method. This + * method is responsible for actually performing the operation on the image. + * When this method gets called, the implementer may assume all arguments, + * also the optional ones, to be available, validated and corrected. + * + * @param \Drupal\Core\Image\ImageInterface $image + * An image object. + * @param array $arguments + * An associative array of arguments to be used by the toolkit operation. + * + * @return bool + * TRUE if the operation was performed successfully, FALSE otherwise. + */ + abstract protected function execute(ImageInterface $image, array $arguments); + } diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php index 7e1a2cc..d0ee39d 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php @@ -20,53 +20,19 @@ interface ImageToolkitOperationInterface extends PluginInspectionInterface { /** - * Returns the definition of the operation arguments. - * - * An array whose keys are the names of the arguments (e.g. "width", - * "degrees") and each value is an associative array having the following - * keys: - * - description: A string with the argument description. This is used only - * internally for documentation purposes, so it does not need to be - * translatable. - * - required: (optional) A boolean indicating if this argument should be - * provided or not. Defaults to TRUE. - * - default: (optional) When the argument is set to "required" = FALSE, - * this must be set to a default value. Ignored for "required" = TRUE - * arguments. - * - * @return array - */ - public function arguments(); - - /** - * Validates, normalizes and adds defaults to arguments before sending them - * to the apply method. - * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * @param array $arguments - * An associative array of arguments to be used by the toolkit operation. - * - * @return bool - * TRUE if the operation has to be applied to the image. FALSE if the - * image has to remain unchanged. - * - * @throws \InvalidArgumentException. - */ - public function prepare(ImageInterface $image, array &$arguments); - - /** * Applies a toolkit specific operation to an image. * * @param \Drupal\Core\Image\ImageInterface $image * An image object. * @param array $arguments - * (optional) An associative array of data to be used by the toolkit - * operation. + * An associative array of data to be used by the toolkit operation. * * @return bool * TRUE if the operation was performed successfully, FALSE otherwise. + * + * @throws \InvalidArgumentException. + * If one or more of the arguments are not valid. */ - public function apply(ImageInterface $image, array $arguments = array()); + public function apply(ImageInterface $image, array $arguments); } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php index 40056b8..7751868 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php @@ -8,7 +8,6 @@ namespace Drupal\system\Plugin\ImageToolkit\Operation\gd; use Drupal\Core\Image\ImageInterface; -use Drupal\Core\ImageToolkit\ImageToolkitOperationBase; /** * Defines GD2 Crop operation. @@ -20,12 +19,12 @@ * description = @Translation("Crops an image to a rectangle specified by the given dimensions.") * ) */ -class Crop extends ImageToolkitOperationBase { +class Crop extends GDImageToolkitOperationBase { /** * {@inheritdoc} */ - public function arguments() { + protected function arguments() { return array( 'x' => array( 'description' => 'The starting x offset at which to start the crop, in pixels', @@ -49,19 +48,22 @@ public function arguments() { /** * {@inheritdoc} */ - public function prepare(ImageInterface $image, array &$arguments) { - parent::prepare($image, $arguments); - + protected function validateArguments(ImageInterface $image, array $arguments) { // Assure at least one dimension. if (empty($arguments['width']) && empty($arguments['height'])) { throw new \InvalidArgumentException("At least one dimension ('width' or 'height') must be provided to the image 'crop' operation."); } // Preserve aspect. - $aspect = $this->toolkit->getHeight($image) / $this->toolkit->getWidth($image); + $aspect = $this->getToolkit()->getHeight($image) / $this->getToolkit()->getWidth($image); $arguments['height'] = empty($arguments['height']) ? $arguments['width'] * $aspect : $arguments['height']; $arguments['width'] = empty($arguments['width']) ? $arguments['height'] / $aspect : $arguments['width']; + // Assure integers for all arguments. + foreach (array('x', 'y', 'width', 'height') as $key) { + $arguments[$key] = (int) round($arguments[$key]); + } + // Fail when width or height are 0 or negative. if ($arguments['width'] <= 0) { throw new \InvalidArgumentException("Invalid width specified for the image 'crop' operation."); @@ -70,27 +72,22 @@ public function prepare(ImageInterface $image, array &$arguments) { throw new \InvalidArgumentException("Invalid height specified for the image 'crop' operation."); } - // Assure integers for all arguments. - foreach (array('x', 'y', 'width', 'height') as $key) { - $arguments[$key] = (int) round($arguments[$key]); - } - - return TRUE; + return $arguments; } /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { - $res = $this->toolkit->createTmp($this->toolkit->getType(), $arguments['width'], $arguments['height']); + protected function execute(ImageInterface $image, array $arguments) { + $res = $this->getToolkit()->createTmp($this->getToolkit()->getType(), $arguments['width'], $arguments['height']); - if (!imagecopyresampled($res, $this->toolkit->getResource(), 0, 0, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'], $arguments['width'], $arguments['height'])) { + if (!imagecopyresampled($res, $this->getToolkit()->getResource(), 0, 0, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'], $arguments['width'], $arguments['height'])) { return FALSE; } // Destroy the original image and return the modified image. - imagedestroy($this->toolkit->getResource()); - $this->toolkit->setResource($res); + imagedestroy($this->getToolkit()->getResource()); + $this->getToolkit()->setResource($res); return TRUE; } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Desaturate.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Desaturate.php index a46fb5d..e2bf45e 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Desaturate.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Desaturate.php @@ -8,7 +8,6 @@ namespace Drupal\system\Plugin\ImageToolkit\Operation\gd; use Drupal\Core\Image\ImageInterface; -use Drupal\Core\ImageToolkit\ImageToolkitOperationBase; /** * Defines GD2 Desaturate operation. @@ -20,19 +19,27 @@ * description = @Translation("Converts an image to grayscale.") * ) */ -class Desaturate extends ImageToolkitOperationBase { +class Desaturate extends GDImageToolkitOperationBase { /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { + protected function arguments() { + // This operation does not use any parameters. + return array(); + } + + /** + * {@inheritdoc} + */ + protected function execute(ImageInterface $image, array $arguments) { // 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($this->toolkit->getResource(), IMG_FILTER_GRAYSCALE); + return imagefilter($this->getToolkit()->getResource(), IMG_FILTER_GRAYSCALE); } } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/GDImageToolkitOperationBase.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/GDImageToolkitOperationBase.php new file mode 100644 index 0000000..b1a9b79 --- /dev/null +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/GDImageToolkitOperationBase.php @@ -0,0 +1,23 @@ + array( 'description' => 'The new width of the resized image, in pixels', @@ -39,8 +38,10 @@ public function arguments() { /** * {@inheritdoc} */ - public function prepare(ImageInterface $image, array &$arguments) { - parent::prepare($image, $arguments); + protected function validateArguments(ImageInterface $image, array $arguments) { + // Assure integers for all arguments. + $arguments['width'] = (int) round($arguments['width']); + $arguments['height'] = (int) round($arguments['height']); // Fail when width or height are 0 or negative. if ($arguments['width'] <= 0) { @@ -50,26 +51,22 @@ public function prepare(ImageInterface $image, array &$arguments) { throw new \InvalidArgumentException("Invalid height specified for the image 'resize' operation."); } - // Assure integers for all arguments. - $arguments['width'] = (int) round($arguments['width']); - $arguments['height'] = (int) round($arguments['height']); - - return TRUE; + return $arguments; } /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { - $res = $this->toolkit->createTmp($this->toolkit->getType(), $arguments['width'], $arguments['height']); + protected function execute(ImageInterface $image, array $arguments = array()) { + $res = $this->getToolkit()->createTmp($this->getToolkit()->getType(), $arguments['width'], $arguments['height']); - if (!imagecopyresampled($res, $this->toolkit->getResource(), 0, 0, 0, 0, $arguments['width'], $arguments['height'], $this->toolkit->getWidth($image), $this->toolkit->getHeight($image))) { + if (!imagecopyresampled($res, $this->getToolkit()->getResource(), 0, 0, 0, 0, $arguments['width'], $arguments['height'], $this->getToolkit()->getWidth($image), $this->getToolkit()->getHeight($image))) { return FALSE; } - imagedestroy($this->toolkit->getResource()); + imagedestroy($this->getToolkit()->getResource()); // Update image object. - $this->toolkit->setResource($res); + $this->getToolkit()->setResource($res); return TRUE; } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php index e7d0f18..5679d65 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php @@ -8,7 +8,6 @@ namespace Drupal\system\Plugin\ImageToolkit\Operation\gd; use Drupal\Core\Image\ImageInterface; -use Drupal\Core\ImageToolkit\ImageToolkitOperationBase; /** * Defines GD2 rotate operation. @@ -20,12 +19,12 @@ * description = @Translation("Rotates an image by the given number of degrees.") * ) */ -class Rotate extends ImageToolkitOperationBase { +class Rotate extends GDImageToolkitOperationBase { /** * {@inheritdoc} */ - public function arguments() { + protected function arguments() { return array( 'degrees' => array( 'description' => 'The number of (clockwise) degrees to rotate the image', @@ -41,7 +40,7 @@ public function arguments() { /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { + protected function execute(ImageInterface $image, array $arguments) { // 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())); @@ -54,34 +53,34 @@ public function apply(ImageInterface $image, array $arguments = array()) { for ($i = 16; $i >= 0; $i -= 8) { $rgb[] = (($arguments['background'] >> $i) & 0xFF); } - $arguments['background'] = imagecolorallocatealpha($this->toolkit->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); + $arguments['background'] = imagecolorallocatealpha($this->getToolkit()->getResource(), $rgb[0], $rgb[1], $rgb[2], 0); } // Set background color as transparent if $arguments['background'] is NULL. else { // Get the current transparent color. - $arguments['background'] = imagecolortransparent($this->toolkit->getResource()); + $arguments['background'] = imagecolortransparent($this->getToolkit()->getResource()); // If no transparent colors, use white. if ($arguments['background'] == 0) { - $arguments['background'] = imagecolorallocatealpha($this->toolkit->getResource(), 255, 255, 255, 0); + $arguments['background'] = imagecolorallocatealpha($this->getToolkit()->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 ($this->toolkit->getType() == IMAGETYPE_GIF) { - $transparent_index = imagecolortransparent($this->toolkit->getResource()); + if ($this->getToolkit()->getType() == IMAGETYPE_GIF) { + $transparent_index = imagecolortransparent($this->getToolkit()->getResource()); if ($transparent_index != 0) { - $transparent_gif_color = imagecolorsforindex($this->toolkit->getResource(), $transparent_index); + $transparent_gif_color = imagecolorsforindex($this->getToolkit()->getResource(), $transparent_index); } } - $this->toolkit->setResource(imagerotate($this->toolkit->getResource(), 360 - $arguments['degrees'], $arguments['background'])); + $this->getToolkit()->setResource(imagerotate($this->getToolkit()->getResource(), 360 - $arguments['degrees'], $arguments['background'])); // GIFs need to reassign the transparent color after performing the rotate. if (isset($transparent_gif_color)) { - $arguments['background'] = imagecolorexactalpha($this->toolkit->getResource(), $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']); - imagecolortransparent($this->toolkit->getResource(), $arguments['background']); + $arguments['background'] = imagecolorexactalpha($this->getToolkit()->getResource(), $transparent_gif_color['red'], $transparent_gif_color['green'], $transparent_gif_color['blue'], $transparent_gif_color['alpha']); + imagecolortransparent($this->getToolkit()->getResource(), $arguments['background']); } return TRUE; diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php index b43dc4b..4a70429 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php @@ -8,7 +8,6 @@ namespace Drupal\system\Plugin\ImageToolkit\Operation\gd; use Drupal\Core\Image\ImageInterface; -use Drupal\Component\Utility\String; /** * Defines GD2 Scale operation. @@ -25,7 +24,7 @@ class Scale extends Resize { /** * {@inheritdoc} */ - public function arguments() { + protected function arguments() { return array( 'width' => array( 'description' => 'The target width, in pixels. This value is omitted then the scaling will based only on the height value', @@ -48,12 +47,9 @@ public function arguments() { /** * {@inheritdoc} */ - public function prepare(ImageInterface $image, array &$arguments) { - $width = &$arguments['width']; - $height = &$arguments['height']; - + protected function validateArguments(ImageInterface $image, array $arguments) { // Assure at least one dimension. - if (empty($width) && empty($height)) { + if (empty($arguments['width']) && empty($arguments['height'])) { throw new \InvalidArgumentException("At least one dimension ('width' or 'height') must be provided to the image 'scale' operation."); } @@ -62,22 +58,40 @@ public function prepare(ImageInterface $image, array &$arguments) { // target dimensions is missing, that is the one that is calculated. If both // are specified then the dimension calculated is the one that would not be // calculated to be bigger than its target. - $scale = $this->toolkit->getHeight($image) / $this->toolkit->getWidth($image); - if (($width && !$height) || ($width && $height && $scale < $height / $width)) { - $height = (int) round($width * $scale); + $aspect = $this->getToolkit()->getHeight($image) / $this->getToolkit()->getWidth($image); + if (($arguments['width'] && !$arguments['height']) || ($arguments['width'] && $arguments['height'] && $aspect < $arguments['height'] / $arguments['width'])) { + $arguments['height'] = (int) round($arguments['width'] * $aspect); } else { - $width = (int) round($height / $scale); + $arguments['width'] = (int) round($arguments['height'] / $aspect); } - // Call parent (Resize) argument preparation. - parent::prepare($image, $arguments); + // Assure integers for all arguments. + $arguments['width'] = (int) round($arguments['width']); + $arguments['height'] = (int) round($arguments['height']); - // Don't upscale if the option isn't enabled. - if (!$arguments['upscale'] && ($width >= $this->toolkit->getWidth($image) || $height >= $this->toolkit->getHeight($image))) { - return FALSE; + // Fail when width or height are 0 or negative. + if ($arguments['width'] <= 0) { + throw new \InvalidArgumentException("Invalid width specified for the image 'scale' operation."); + } + if ($arguments['height'] <= 0) { + throw new \InvalidArgumentException("Invalid height specified for the image 'scale' operation."); } + return $arguments; + } + + /** + * {@inheritdoc} + */ + protected function execute(ImageInterface $image, array $arguments = array()) { + // Don't scale if we don't change the dimensions at all. + if ($arguments['width'] !== $this->getToolkit()->getWidth($image) || $arguments['height'] !== $this->getToolkit()->getHeight($image)) { + // Don't upscale if the option isn't enabled. + if ($arguments['upscale'] || ($arguments['width'] <= $this->getToolkit()->getWidth($image) && $arguments['height'] <= $this->getToolkit()->getHeight($image))) { + return parent::execute($image, $arguments); + } + } return TRUE; } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php index 5ea9166..13304a4 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php @@ -8,7 +8,6 @@ namespace Drupal\system\Plugin\ImageToolkit\Operation\gd; use Drupal\Core\Image\ImageInterface; -use Drupal\Core\ImageToolkit\ImageToolkitOperationBase; /** * Defines GD2 Scale and crop operation. @@ -20,12 +19,12 @@ * description = @Translation("Scales an image to the exact width and height given. This plugin achieves the target aspect ratio by cropping the original image equally on both sides, or equally on the top and bottom. This function is useful to create uniform sized avatars from larger images.") * ) */ -class ScaleAndCrop extends ImageToolkitOperationBase { +class ScaleAndCrop extends GDImageToolkitOperationBase { /** * {@inheritdoc} */ - public function arguments() { + protected function arguments() { return array( 'width' => array( 'description' => 'The target width, in pixels', @@ -39,33 +38,28 @@ public function arguments() { /** * {@inheritdoc} */ - public function prepare(ImageInterface $image, array &$arguments) { - parent::prepare($image, $arguments); + protected function validateArguments(ImageInterface $image, array $arguments) { + $actualWidth = $this->getToolkit()->getWidth($image); + $actualHeight = $this->getToolkit()->getHeight($image); - $width = $this->toolkit->getWidth($image); - $height = $this->toolkit->getHeight($image); + $scaleFactor = max($arguments['width'] / $actualWidth, $arguments['height'] / $actualHeight); - $scale = max($arguments['width'] / $width, $arguments['height'] / $height); - - $arguments['x'] = (int) round(($width * $scale - $arguments['width']) / 2); - $arguments['y'] = (int) round(($height * $scale - $arguments['height']) / 2); + $arguments['x'] = (int) round(($actualWidth * $scaleFactor - $arguments['width']) / 2); + $arguments['y'] = (int) round(($actualHeight * $scaleFactor - $arguments['height']) / 2); $arguments['resize'] = array( - 'width' => (int) round($width * $scale), - 'height' => (int) round($height * $scale), + 'width' => (int) round($actualWidth * $scaleFactor), + 'height' => (int) round($actualHeight * $scaleFactor), ); - return TRUE; + return $arguments; } /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { - if ($this->toolkit->apply('resize', $image, $arguments['resize'])) { - return $this->toolkit->apply('crop', $image, $arguments); - } - - return FALSE; + protected function execute(ImageInterface $image, array $arguments = array()) { + return $this->getToolkit()->apply('resize', $image, $arguments['resize']) + && $this->getToolkit()->apply('crop', $image, $arguments); } } diff --git a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Crop.php b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Crop.php index 7075dc6..3928ff5 100644 --- a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Crop.php +++ b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Crop.php @@ -25,7 +25,7 @@ class Crop extends ImageToolkitOperationBase { /** * {@inheritdoc} */ - public function arguments() { + protected function arguments() { return array( 'x' => array( 'description' => 'The starting x offset at which to start the crop, in pixels', @@ -49,8 +49,8 @@ public function arguments() { /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { - $this->toolkit->logCall('crop', array($image, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'])); + protected function execute(ImageInterface $image, array $arguments) { + $this->getToolkit()->logCall('crop', array($image, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'])); return TRUE; } diff --git a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Desaturate.php b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Desaturate.php index 04b7542..1c87c5c 100644 --- a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Desaturate.php +++ b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Desaturate.php @@ -25,8 +25,16 @@ class Desaturate extends ImageToolkitOperationBase { /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { - $this->toolkit->logCall('desaturate', array($image)); + protected function arguments() { + // This operation does not use any parameters. + return array(); + } + + /** + * {@inheritdoc} + */ + protected function execute(ImageInterface $image, array $arguments) { + $this->getToolkit()->logCall('desaturate', array($image)); return TRUE; } diff --git a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Resize.php b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Resize.php index bb5d741..6d2441e 100644 --- a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Resize.php +++ b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Resize.php @@ -25,7 +25,7 @@ class Resize extends ImageToolkitOperationBase { /** * {@inheritdoc} */ - public function arguments() { + protected function arguments() { return array( 'width' => array( 'description' => 'The new width of the resized image, in pixels', @@ -39,8 +39,8 @@ public function arguments() { /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { - $this->toolkit->logCall('resize', array($image, $arguments['width'], $arguments['height'])); + protected function execute(ImageInterface $image, array $arguments) { + $this->getToolkit()->logCall('resize', array($image, $arguments['width'], $arguments['height'])); return TRUE; } diff --git a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Rotate.php b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Rotate.php index dac6c68..7d75121 100644 --- a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Rotate.php +++ b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Rotate.php @@ -25,7 +25,7 @@ class Rotate extends ImageToolkitOperationBase { /** * {@inheritdoc} */ - public function arguments() { + protected function arguments() { return array( 'degrees' => array( 'description' => 'The number of (clockwise) degrees to rotate the image', @@ -41,8 +41,8 @@ public function arguments() { /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { - $this->toolkit->logCall('rotate', array($image, $arguments['degrees'], $arguments['background'])); + protected function execute(ImageInterface $image, array $arguments) { + $this->getToolkit()->logCall('rotate', array($image, $arguments['degrees'], $arguments['background'])); return TRUE; } diff --git a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Scale.php b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Scale.php index 05b356a..198a4ea 100644 --- a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Scale.php +++ b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/Scale.php @@ -24,7 +24,7 @@ class Scale extends Resize { /** * {@inheritdoc} */ - public function arguments() { + protected function arguments() { return array( 'width' => array( 'description' => 'The target width, in pixels. This value is omitted then the scaling will based only on the height value', @@ -47,8 +47,8 @@ public function arguments() { /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { - $this->toolkit->logCall('scale', array($image, $arguments['width'], $arguments['height'], $arguments['upscale'])); + protected function execute(ImageInterface $image, array $arguments) { + $this->getToolkit()->logCall('scale', array($image, $arguments['width'], $arguments['height'], $arguments['upscale'])); return TRUE; } diff --git a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/ScaleAndCrop.php b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/ScaleAndCrop.php index fa32d8a..f857e69 100644 --- a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/ScaleAndCrop.php +++ b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/Operation/test/ScaleAndCrop.php @@ -24,7 +24,7 @@ class ScaleAndCrop extends Crop { /** * {@inheritdoc} */ - public function arguments() { + protected function arguments() { return array( 'width' => array( 'description' => 'The target width, in pixels', @@ -38,8 +38,8 @@ public function arguments() { /** * {@inheritdoc} */ - public function apply(ImageInterface $image, array $arguments = array()) { - $this->toolkit->logCall('scale_and_crop', array($image, $arguments['width'], $arguments['height'])); + protected function execute(ImageInterface $image, array $arguments) { + $this->getToolkit()->logCall('scale_and_crop', array($image, $arguments['width'], $arguments['height'])); return TRUE; } diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index 49a0159..058e222 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -91,9 +91,9 @@ protected function getToolkitMock(array $stubs = array()) { * @return \PHPUnit_Framework_MockObject_MockObject */ protected function getToolkitOperationMock($class_name, ImageToolkitInterface $toolkit) { - $mock_builder = $this->getMockBuilder('Drupal\\system\\Plugin\\ImageToolkit\\Operation\\gd\\' . $class_name); + $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\Operation\gd\\' . $class_name); return $mock_builder - ->setMethods(array('apply')) + ->setMethods(array('execute')) ->setConstructorArgs(array(array(), '', array(), $toolkit)) ->getMock(); } @@ -264,7 +264,7 @@ public function testParseFileFails() { public function testScaleWidth() { $this->getTestImageForOperation('Scale'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); $ret = $this->image->apply('scale', array('width' => 44, 'upscale' => FALSE)); @@ -277,7 +277,7 @@ public function testScaleWidth() { public function testScaleHeight() { $this->getTestImageForOperation('Scale'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); $ret = $this->image->apply('scale', array('height' => 50, 'upscale' => FALSE)); @@ -290,10 +290,13 @@ public function testScaleHeight() { public function testScaleSame() { $this->getTestImageForOperation('Scale'); // Dimensions are the same, resize should not be called. - $this->toolkitOperation->expects($this->never()) - ->method('apply'); + $this->toolkitOperation->expects($this->once()) + ->method('execute') + ->will($this->returnArgument(1)); $ret = $this->image->apply('scale', array ('width' => 88, 'height' => 100, 'upscale' => FALSE)); + $this->assertEquals(88, $ret['width']); + $this->assertEquals(100, $ret['height']); } /** @@ -302,7 +305,7 @@ public function testScaleSame() { public function testScaleAndCropWidth() { $this->getTestImageForOperation('ScaleAndCrop'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); $ret = $this->image->apply('scale_and_crop', array ('width' => 34, 'height' => 50, 'upscale' => FALSE)); @@ -315,7 +318,7 @@ public function testScaleAndCropWidth() { public function testScaleAndCropHeight() { $this->getTestImageForOperation('ScaleAndCrop'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); $ret = $this->image->apply('scale_and_crop', array ('width' => 44, 'height' => 40)); @@ -328,7 +331,7 @@ public function testScaleAndCropHeight() { public function testScaleAndCropFails() { $this->getTestImageForOperation('ScaleAndCrop'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); $ret = $this->image->apply('scale_and_crop', array('width' => 44, 'height' => 40)); @@ -344,7 +347,7 @@ public function testScaleAndCropFails() { public function testCropWidth() { $this->getTestImageForOperation('Crop'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); // Cropping with width only should preserve the aspect ratio. @@ -358,7 +361,7 @@ public function testCropWidth() { public function testCropHeight() { $this->getTestImageForOperation('Crop'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); // Cropping with height only should preserve the aspect ratio. @@ -372,7 +375,7 @@ public function testCropHeight() { public function testCrop() { $this->getTestImageForOperation('Crop'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); $ret = $this->image->apply('crop', array ('x' => 0, 'y' => 0, 'width' => 44, 'height' => 50)); @@ -385,7 +388,7 @@ public function testCrop() { public function testResize() { $this->getTestImageForOperation('Resize'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); // Resize with integer for width and height. @@ -400,7 +403,7 @@ public function testResize() { public function testFloatResize() { $this->getTestImageForOperation('Resize'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); // Pass a float for width. @@ -415,7 +418,7 @@ public function testFloatResize() { public function testDesaturate() { $this->getTestImageForOperation('Desaturate'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); $this->image->apply('desaturate'); @@ -427,7 +430,7 @@ public function testDesaturate() { public function testRotate() { $this->getTestImageForOperation('Rotate'); $this->toolkitOperation->expects($this->once()) - ->method('apply') + ->method('execute') ->will($this->returnArgument(1)); $ret = $this->image->apply('rotate', array('degrees' => 90));