diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index ff33717..a46a0a1 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -171,6 +171,48 @@ public function apply($operation, array $arguments = array()) { } /** + * {@inheritdoc} + */ + public function crop($x, $y, $width, $height) { + return $this->apply('crop', array('x' => $x, 'y' => $y, 'width' => $width, 'height' => $height)); + } + + /** + * {@inheritdoc} + */ + public function desaturate() { + return $this->apply('desaturate', array()); + } + + /** + * {@inheritdoc} + */ + public function resize($width, $height) { + return $this->apply('resize', array('width' => $width, 'height' => $height)); + } + + /** + * {@inheritdoc} + */ + public function rotate($degrees, $background = NULL) { + return $this->apply('rotate', array('degrees' => $degrees, 'background' => $background)); + } + + /** + * {@inheritdoc} + */ + public function scaleAndCrop($width, $height) { + return $this->apply('scale_and_crop', array('width' => $width, 'height' => $height)); + } + + /** + * {@inheritdoc} + */ + public function scale($width = NULL, $height = NULL, $upscale = FALSE) { + return $this->apply('scale', array('width' => $width, 'height' => $height, 'upscale' => $upscale)); + } + + /** * Provides a wrapper for drupal_chmod() to allow unit testing. * * @param string $uri diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index ed33313..1066ba0 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -109,4 +109,97 @@ public function apply($operation, array $arguments = array()); */ public function save($destination = NULL); + /** + * Scales an image while maintaining aspect ratio. + * + * The resulting image can be smaller for one or both target dimensions. + * + * @param int|null $width + * The target width, in pixels. If this value is null then the scaling will + * be based only on the height value. + * @param int|null $height + * (optional) The target height, in pixels. If this value is null then the + * scaling will be based only on the width value. + * @param bool $upscale + * (optional) Boolean indicating that files smaller than the dimensions will + * be scaled up. This generally results in a low quality image. + * + * @return bool + * TRUE on success, FALSE on failure. + */ + public function scale($width, $height = NULL, $upscale = FALSE); + + /** + * Scales an image to the exact width and height given. + * + * This function 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. + * + * The resulting image always has the exact target dimensions. + * + * @param int $width + * The target width, in pixels. + * @param int $height + * The target height, in pixels. + * + * @return bool + * TRUE on success, FALSE on failure. + */ + public function scaleAndCrop($width, $height); + + /** + * Crops an image to a rectangle specified by the given dimensions. + * + * @param int $x + * The top left coordinate, in pixels, of the crop area (x axis value). + * @param int $y + * The top left coordinate, in pixels, of the crop area (y axis value). + * @param int $width + * The target width, in pixels. + * @param int $height + * The target height, in pixels. + * + * @return bool + * TRUE on success, FALSE on failure. + */ + public function crop($x, $y, $width, $height); + + /** + * Resizes an image to the given dimensions (ignoring aspect ratio). + * + * @param int $width + * The target width, in pixels. + * @param int $height + * The target height, in pixels. + * + * @return bool + * TRUE on success, FALSE on failure. + */ + public function resize($width, $height); + + /** + * Converts an image to grayscale. + * + * @return bool + * TRUE on success, FALSE on failure. + */ + public function desaturate(); + + /** + * Rotates an image by the given number of degrees. + * + * @param float $degrees + * The number of (clockwise) degrees to rotate the image. + * @param string|null $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 on success, FALSE on failure. + */ + public function rotate($degrees, $background = NULL); } diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php index fbeb4a0..b3ae30d 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @@ -49,7 +49,7 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi */ public function setImage(ImageInterface $image) { if ($this->image) { - throw new \BadMethodCallException(__METHOD__ . '() may only be called once.'); + throw new \BadMethodCallException(__METHOD__ . '() may only be called once'); } $this->image = $image; } diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php index 1b127a3..6b66c29 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php @@ -95,7 +95,7 @@ protected function prepareArguments(array $arguments) { if ($argument['required']) { if (!array_key_exists($id, $arguments)) { // If the argument is required throw an exception. - throw new \InvalidArgumentException(String::format("Argument '@argument' expected by plugin '@plugin' but not passed.", array('@argument' => $id, '@plugin' => $this->getPluginId()))); + throw new \InvalidArgumentException(String::format("Argument '@argument' expected by plugin '@plugin' but not passed", array('@argument' => $id, '@plugin' => $this->getPluginId()))); } } else { @@ -105,7 +105,7 @@ protected function prepareArguments(array $arguments) { 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(String::format("Default for argument '@argument' expected by plugin '@plugin' but not defined.", array('@argument' => $id, '@plugin' => $this->getPluginId()))); + throw new InvalidPluginDefinitionException(String::format("Default for argument '@argument' expected by plugin '@plugin' but not defined", array('@argument' => $id, '@plugin' => $this->getPluginId()))); } // Use the default value if the argument is not passed in. diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 389d99b..52a255a 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -458,7 +458,7 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions list($width, $height) = explode('x', $maximum_dimensions); if ($image->getWidth() > $width || $image->getHeight() > $height) { // Try to resize the image to fit the dimensions. - if ($image->apply('scale', array('width' => $width, 'height' => $height))) { + if ($image->scale($width, $height)) { $image->save(); $file->filesize = $image->getFileSize(); drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions))); diff --git a/core/modules/image/src/Form/ImageEffectFormBase.php b/core/modules/image/src/Form/ImageEffectFormBase.php index 6cc8d1d..f517e5e 100644 --- a/core/modules/image/src/Form/ImageEffectFormBase.php +++ b/core/modules/image/src/Form/ImageEffectFormBase.php @@ -59,7 +59,7 @@ public function buildForm(array $form, array &$form_state, ImageStyleInterface $ $this->imageEffect = $this->prepareImageEffect($image_effect); } catch (PluginNotFoundException $e) { - throw new NotFoundHttpException(String::format("Invalid effect id: '@id'.", array('@id' => $image_effect))); + throw new NotFoundHttpException(String::format("Invalid effect id: '@id'", array('@id' => $image_effect))); } $request = $this->getRequest(); diff --git a/core/modules/image/src/Plugin/ImageEffect/CropImageEffect.php b/core/modules/image/src/Plugin/ImageEffect/CropImageEffect.php index b242d4c..29ae87d 100644 --- a/core/modules/image/src/Plugin/ImageEffect/CropImageEffect.php +++ b/core/modules/image/src/Plugin/ImageEffect/CropImageEffect.php @@ -24,10 +24,10 @@ class CropImageEffect extends ResizeImageEffect { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - list($x, $y) = explode('-', $this->configuration['anchor']); - $x = image_filter_keyword($x, $image->getWidth(), $this->configuration['width']); - $y = image_filter_keyword($y, $image->getHeight(), $this->configuration['height']); - $data = array('x' => $x, 'y' => $y, 'width' => $this->configuration['width'], 'height' => $this->configuration['height']); + $data = $this->configuration; + list($data['x'], $data['y']) = explode('-', $this->configuration['anchor']); + $data['x'] = image_filter_keyword($data['x'], $image->getWidth(), $this->configuration['width']); + $data['y'] = image_filter_keyword($data['y'], $image->getHeight(), $this->configuration['height']); if (!$image->apply('crop', $data)) { \Drupal::logger('image')->error('Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight())); return FALSE; diff --git a/core/modules/image/src/Plugin/ImageEffect/DesaturateImageEffect.php b/core/modules/image/src/Plugin/ImageEffect/DesaturateImageEffect.php index 8d323b9..b3aedd3 100644 --- a/core/modules/image/src/Plugin/ImageEffect/DesaturateImageEffect.php +++ b/core/modules/image/src/Plugin/ImageEffect/DesaturateImageEffect.php @@ -31,7 +31,7 @@ public function transformDimensions(array &$dimensions) { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - if (!$image->apply('desaturate')) { + if (!$image->desaturate()) { \Drupal::logger('image')->error('Image desaturate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight())); return FALSE; } diff --git a/core/modules/image/src/Plugin/ImageEffect/ResizeImageEffect.php b/core/modules/image/src/Plugin/ImageEffect/ResizeImageEffect.php index e39f609..afa9210 100644 --- a/core/modules/image/src/Plugin/ImageEffect/ResizeImageEffect.php +++ b/core/modules/image/src/Plugin/ImageEffect/ResizeImageEffect.php @@ -25,8 +25,7 @@ class ResizeImageEffect extends ConfigurableImageEffectBase { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - $data = array('width' => $this->configuration['width'], 'height' => $this->configuration['height']); - if (!$image->apply('resize', $data)) { + if (!$image->apply('resize', $this->configuration)) { \Drupal::logger('image')->error('Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight())); return FALSE; } diff --git a/core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.php b/core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.php index 6538303..89d226e 100644 --- a/core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.php +++ b/core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.php @@ -45,8 +45,7 @@ public function applyEffect(ImageInterface $image) { $this->configuration['degrees'] = rand(-1 * $degrees, $degrees); } - $data = array('degrees' => $this->configuration['degrees'], 'background' => $this->configuration['bgcolor']); - if (!$image->apply('rotate', $data)) { + if (!$image->rotate($this->configuration['degrees'], $this->configuration['bgcolor'])) { \Drupal::logger('image')->error('Image rotate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight())); return FALSE; } diff --git a/core/modules/image/src/Plugin/ImageEffect/ScaleAndCropImageEffect.php b/core/modules/image/src/Plugin/ImageEffect/ScaleAndCropImageEffect.php index ab24c5d..57a0a47 100644 --- a/core/modules/image/src/Plugin/ImageEffect/ScaleAndCropImageEffect.php +++ b/core/modules/image/src/Plugin/ImageEffect/ScaleAndCropImageEffect.php @@ -24,8 +24,7 @@ class ScaleAndCropImageEffect extends ResizeImageEffect { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - $data = array('width' => $this->configuration['width'], 'height' => $this->configuration['height']); - if (!$image->apply('scale_and_crop', $data)) { + if (!$image->apply('scale_and_crop', $this->configuration)) { \Drupal::logger('image')->error('Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight())); return FALSE; } diff --git a/core/modules/image/src/Plugin/ImageEffect/ScaleImageEffect.php b/core/modules/image/src/Plugin/ImageEffect/ScaleImageEffect.php index 325c2a9..de906fe 100644 --- a/core/modules/image/src/Plugin/ImageEffect/ScaleImageEffect.php +++ b/core/modules/image/src/Plugin/ImageEffect/ScaleImageEffect.php @@ -25,8 +25,7 @@ class ScaleImageEffect extends ResizeImageEffect { * {@inheritdoc} */ public function applyEffect(ImageInterface $image) { - $data = array('width' => $this->configuration['width'], 'height' => $this->configuration['height'], 'upscale' => $this->configuration['upscale']); - if (!$image->apply('scale', $data)) { + if (!$image->apply('scale', $this->configuration)) { \Drupal::logger('image')->error('Image scale failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight())); return FALSE; } 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 6564242..67c435c 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php @@ -51,7 +51,7 @@ protected function arguments() { protected function validateArguments(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."); + throw new \InvalidArgumentException("At least one dimension ('width' or 'height') must be provided to the image 'crop' operation"); } // Preserve aspect. @@ -66,10 +66,10 @@ protected function validateArguments(array $arguments) { // Fail when width or height are 0 or negative. if ($arguments['width'] <= 0) { - throw new \InvalidArgumentException(String::format("Invalid width (@value) specified for the image 'crop' operation.", array('@value' => $arguments['width']))); + throw new \InvalidArgumentException(String::format("Invalid width (@value) specified for the image 'crop' operation", array('@value' => $arguments['width']))); } if ($arguments['height'] <= 0) { - throw new \InvalidArgumentException(String::format("Invalid height (@value) specified for the image 'crop' operation.", array('@value' => $arguments['height']))); + throw new \InvalidArgumentException(String::format("Invalid height (@value) specified for the image 'crop' operation", array('@value' => $arguments['height']))); } return $arguments; 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 aea6617..84258f8 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php @@ -45,10 +45,10 @@ protected function validateArguments(array $arguments) { // Fail when width or height are 0 or negative. if ($arguments['width'] <= 0) { - throw new \InvalidArgumentException(String::format("Invalid width (@value) specified for the image 'resize' operation.", array('@value' => $arguments['width']))); + throw new \InvalidArgumentException(String::format("Invalid width (@value) specified for the image 'resize' operation", array('@value' => $arguments['width']))); } if ($arguments['height'] <= 0) { - throw new \InvalidArgumentException(String::format("Invalid height (@value) specified for the image 'resize' operation.", array('@value' => $arguments['height']))); + throw new \InvalidArgumentException(String::format("Invalid height (@value) specified for the image 'resize' operation", array('@value' => $arguments['height']))); } return $arguments; 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 c7af040..ab38d04 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php @@ -50,7 +50,7 @@ protected function arguments() { protected function validateArguments(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 'scale' operation."); + 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, @@ -72,10 +72,10 @@ protected function validateArguments(array $arguments) { // Fail when width or height are 0 or negative. if ($arguments['width'] <= 0) { - throw new \InvalidArgumentException(String::format("Invalid width (@value) specified for the image 'scale' operation.", array('@value' => $arguments['width']))); + throw new \InvalidArgumentException(String::format("Invalid width (@value) specified for the image 'scale' operation", array('@value' => $arguments['width']))); } if ($arguments['height'] <= 0) { - throw new \InvalidArgumentException(String::format("Invalid height (@value) specified for the image 'scale' operation.", array('@value' => $arguments['height']))); + throw new \InvalidArgumentException(String::format("Invalid height (@value) specified for the image 'scale' operation", array('@value' => $arguments['height']))); } return $arguments; 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 203d2b6..f5848a1 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/ScaleAndCrop.php @@ -53,10 +53,10 @@ protected function validateArguments(array $arguments) { // Fail when width or height are 0 or negative. if ($arguments['width'] <= 0) { - throw new \InvalidArgumentException(String::format("Invalid width (@value) specified for the image 'scale_and_crop' operation.", array('@value' => $arguments['width']))); + throw new \InvalidArgumentException(String::format("Invalid width (@value) specified for the image 'scale_and_crop' operation", array('@value' => $arguments['width']))); } if ($arguments['height'] <= 0) { - throw new \InvalidArgumentException(String::format("Invalid height (@value) specified for the image 'scale_and_crop' operation.", array('@value' => $arguments['height']))); + throw new \InvalidArgumentException(String::format("Invalid height (@value) specified for the image 'scale_and_crop' operation", array('@value' => $arguments['height']))); } return $arguments; diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index f86b4d7..24692b7 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -99,7 +99,7 @@ protected function getToolkitOperationMock($class_name, ImageToolkitInterface $t * An image object. */ protected function getTestImage(array $stubs = array()) { - $this->toolkit = $this->getToolkitMock(); + $this->toolkit = $this->getToolkitMock($stubs); $this->toolkit->expects($this->any()) ->method('getPluginId')