diff --git a/core/lib/Drupal/Core/ImageToolkit/Annotation/ImageToolkitOperation.php b/core/lib/Drupal/Core/ImageToolkit/Annotation/ImageToolkitOperation.php index e1383cc..29d1070 100644 --- a/core/lib/Drupal/Core/ImageToolkit/Annotation/ImageToolkitOperation.php +++ b/core/lib/Drupal/Core/ImageToolkit/Annotation/ImageToolkitOperation.php @@ -60,14 +60,15 @@ class ImageToolkitOperation extends Plugin { * An array of arguments to be passed to the image toolkit operation. * * Keys are the names of the arguments (e.g. "width", "degrees") and each - * value is an associative array having next keys: + * 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 doesn't need to be + * 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) The default value of this argument when the argument - * is not required. + * - default: (optional) When the argument is set to "required" = FALSE, + * this must be set to a default value. Ignored for "required" = TRUE + * arguments. * * @var array */ diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php index f08d6da..4618827 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @@ -72,15 +72,22 @@ public function apply($operation, ImageInterface $image, array $arguments = arra } // Validate and prepare arguments before applying the operation. The - // operation might be cancelled if prepare() decides that the final image - // will not change. - if ($plugin->prepare($image, $arguments) !== FALSE) { - return $plugin->apply($image, $arguments); + // 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; } - // No operation is required because the target image will be the same as the - // original but the result is still successful. - 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 ba08048..50f38d7 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php @@ -20,7 +20,7 @@ protected $toolkit; /** - * Builds am image toolkit operation plugin. + * Constructs an image toolkit operation plugin. * * @param array $configuration * A configuration array containing information about the plugin instance. @@ -28,7 +28,7 @@ * The plugin_id for the plugin instance. * @param array $plugin_definition * The plugin implementation definition. - * @var ImageToolkitInterface $toolkit + * @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit * The image toolkit. */ public function __construct(array $configuration, $plugin_id, array $plugin_definition, ImageToolkitInterface $toolkit) { @@ -41,6 +41,7 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi */ public function prepare(ImageInterface $image, array &$arguments) { $arguments = $this->prepareArguments($arguments); + return TRUE; } /** diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php index e0b9714..9b0cacb 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php @@ -13,23 +13,24 @@ /** * Defines an interface for image toolkit operations. * - * An image toolkit operation provides image manipulations like scaling, - * cropping, rotating, etc. + * An image toolkit operation plugin provides a self-contained image + * manipulation routine, for a specific image toolkit. Examples of image + * toolkit operations are scaling, cropping, rotating, etc. */ interface ImageToolkitOperationInterface extends PluginInspectionInterface { /** - * Validates, normalizes and adds processing to arguments before sending them - * to image toolkit operation. + * 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|null - * Tells if the image still needs this operation to be applied. FALSE means - * that after applying the operations the image will remain unchanged. + * @return bool + * TRUE if the operation has to be applied to the image. FALSE if the + * image has to remain unchanged. * * @throws \InvalidArgumentException. */ 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 abbbdfb..53f94a6 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php @@ -42,25 +42,36 @@ class Crop extends ImageToolkitOperationBase { public function prepare(ImageInterface $image, array &$arguments) { parent::prepare($image, $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); $arguments['height'] = empty($arguments['height']) ? $arguments['width'] * $aspect : $arguments['height']; $arguments['width'] = empty($arguments['width']) ? $arguments['height'] / $aspect : $arguments['width']; + // Fail when width or height are 0 or negative. + if ($arguments['width'] <= 0) { + throw new \InvalidArgumentException("Invalid width specified for the image 'crop' operation."); + } + if ($arguments['height'] <= 0) { + 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; } /** * {@inheritdoc} */ public function apply(ImageInterface $image, array $arguments = array()) { - if ($arguments['width'] <= 0 || $arguments['height'] <= 0) { - return FALSE; - } - $res = $this->toolkit->createTmp($this->toolkit->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'])) { diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php index a6703a0..a6c2e75 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php @@ -36,20 +36,25 @@ class Resize extends ImageToolkitOperationBase { public function prepare(ImageInterface $image, array &$arguments) { parent::prepare($image, $arguments); + // Fail when width or height are 0 or negative. + if ($arguments['width'] <= 0) { + throw new \InvalidArgumentException("Invalid width specified for the image 'resize' operation."); + } + if ($arguments['height'] <= 0) { + 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; } /** * {@inheritdoc} */ public function apply(ImageInterface $image, array $arguments = array()) { - if ($arguments['width'] <= 0 || $arguments['height'] <= 0) { - return FALSE; - } - $res = $this->toolkit->createTmp($this->toolkit->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))) { 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 26dd310..477247b 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php @@ -43,13 +43,12 @@ class Scale extends Resize { * {@inheritdoc} */ public function prepare(ImageInterface $image, array &$arguments) { - $width =& $arguments['width']; - $height =& $arguments['height']; + $width = &$arguments['width']; + $height = &$arguments['height']; // Assure at least one dimension. if (empty($width) && empty($height)) { - $message = String::format("At least one dimension ('width' or 'height') must be provided to '@plugin' plugin.", array('@plugin' => $this->getPluginId())); - throw new \InvalidArgumentException($message); + throw new \InvalidArgumentException("At least one dimension ('width' or 'height') must be provided to the image 'scale' operation."); } // Calculate one of the dimensions from the other target dimension, @@ -65,13 +64,15 @@ public function prepare(ImageInterface $image, array &$arguments) { $width = (int) round($height / $scale); } - // Call parent validation + // Call parent (Resize) argument preparation. parent::prepare($image, $arguments); // Don't upscale if the option isn't enabled. if (!$arguments['upscale'] && ($width >= $this->toolkit->getWidth($image) || $height >= $this->toolkit->getHeight($image))) { return FALSE; } + + 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 da04b14..4214d8d 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php @@ -47,6 +47,8 @@ public function prepare(ImageInterface $image, array &$arguments) { 'width' => (int) round($width * $scale), 'height' => (int) round($height * $scale), ); + + return TRUE; } /**