diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index ff33717..8c8aea2 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 = NULL) { + 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, $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..02db478 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 = NULL); + + /** + * 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/image/src/Tests/ImageEffectsTest.php b/core/modules/image/src/Tests/ImageEffectsTest.php index 9cd289d..f2cb31a 100644 --- a/core/modules/image/src/Tests/ImageEffectsTest.php +++ b/core/modules/image/src/Tests/ImageEffectsTest.php @@ -96,12 +96,12 @@ function testScaleAndCropEffect() { 'width' => 5, 'height' => 10, )); - $this->assertToolkitOperationsCalled(array('scaleAndCrop')); + $this->assertToolkitOperationsCalled(array('scale_and_crop')); // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scaleAndCrop'][0][0], 5, 'Width was computed and passed correctly'); - $this->assertEqual($calls['scaleAndCrop'][0][1], 10, 'Height was computed and passed correctly'); + $this->assertEqual($calls['scale_and_crop'][0][0], 5, 'Width was computed and passed correctly'); + $this->assertEqual($calls['scale_and_crop'][0][1], 10, 'Height was computed and passed correctly'); } /** 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..ee08c56 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Scale.php @@ -6,6 +6,7 @@ */ namespace Drupal\system\Plugin\ImageToolkit\Operation\gd; + use Drupal\Component\Utility\String; /** @@ -50,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 '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 +73,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/modules/system/src/Tests/Image/ToolkitTestBase.php b/core/modules/system/src/Tests/Image/ToolkitTestBase.php index eabc88d..810dd9f 100644 --- a/core/modules/system/src/Tests/Image/ToolkitTestBase.php +++ b/core/modules/system/src/Tests/Image/ToolkitTestBase.php @@ -81,6 +81,21 @@ protected function getImage() { * 'save', 'crop', etc. */ function assertToolkitOperationsCalled(array $expected) { + // If one of the image operations is expected, apply should be expected as + // well. + $operations = array( + 'resize', + 'rotate', + 'crop', + 'desaturate', + 'scale', + 'scale_and_crop', + 'my_operation', + ); + if (count(array_intersect($expected, $operations)) > 0 && !in_array('apply', $expected)) { + $expected[] = 'apply'; + } + // Determine which operations were called. $actual = array_keys(array_filter($this->imageTestGetAllCalls())); @@ -94,8 +109,10 @@ function assertToolkitOperationsCalled(array $expected) { } // Determine if there were any unexpected calls. + // If all unexpected calls are operations and apply was expected, we do not + // count it as an error. $unexpected = array_diff($actual, $expected); - if (count($unexpected)) { + if (count($unexpected) && (!in_array('apply', $expected) || count(array_intersect($unexpected, $operations)) !== count($unexpected))) { $this->assertTrue(FALSE, String::format('Unexpected operations were called: %unexpected.', array('%unexpected' => implode(', ', $unexpected)))); } else { @@ -112,6 +129,7 @@ function imageTestReset() { 'parseFile' => array(), 'save' => array(), 'settings' => array(), + 'apply' => array(), 'resize' => array(), 'rotate' => array(), 'crop' => array(), diff --git a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php index ad25bb1..42c8830 100644 --- a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php +++ b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php @@ -105,6 +105,13 @@ public function save($destination) { protected function logCall($op, $args) { $results = \Drupal::state()->get('image_test.results') ?: array(); $results[$op][] = $args; + // A call to apply is also logged under its operation name whereby the + // array of arguments are logged as separate arguments, this because at the + // ImageInterface level we still have methods named after the operations. + if ($op === 'apply') { + $operation = array_shift($args); + $results[$operation][] = array_values(reset($args)); + } \Drupal::state()->set('image_test.results', $results); } diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index f86b4d7..828a1a1 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') @@ -260,7 +260,7 @@ public function testScaleWidth() { ->method('execute') ->will($this->returnArgument(0)); - $ret = $this->image->apply('scale', array('width' => 44, 'upscale' => FALSE)); + $ret = $this->image->scale(44, NULL, FALSE); $this->assertEquals(50, $ret['height']); } @@ -273,7 +273,7 @@ public function testScaleHeight() { ->method('execute') ->will($this->returnArgument(0)); - $ret = $this->image->apply('scale', array('height' => 50, 'upscale' => FALSE)); + $ret = $this->image->scale(NULL, 50, FALSE); $this->assertEquals(44, $ret['width']); } @@ -287,7 +287,7 @@ public function testScaleSame() { ->method('execute') ->will($this->returnArgument(0)); - $ret = $this->image->apply('scale', array ('width' => 88, 'height' => 100, 'upscale' => FALSE)); + $ret = $this->image->scale(88, 100, FALSE); $this->assertEquals(88, $ret['width']); $this->assertEquals(100, $ret['height']); } @@ -301,7 +301,7 @@ public function testScaleAndCropWidth() { ->method('execute') ->will($this->returnArgument(0)); - $ret = $this->image->apply('scale_and_crop', array ('width' => 34, 'height' => 50, 'upscale' => FALSE)); + $ret = $this->image->scaleAndCrop(34, 50, FALSE); $this->assertEquals(5, $ret['x']); } @@ -314,7 +314,7 @@ public function testScaleAndCropHeight() { ->method('execute') ->will($this->returnArgument(0)); - $ret = $this->image->apply('scale_and_crop', array ('width' => 44, 'height' => 40)); + $ret = $this->image->scaleAndCrop(44, 40); $this->assertEquals(5, $ret['y']); } @@ -327,7 +327,7 @@ public function testScaleAndCropFails() { ->method('execute') ->will($this->returnArgument(0)); - $ret = $this->image->apply('scale_and_crop', array('width' => 44, 'height' => 40)); + $ret = $this->image->scaleAndCrop(44, 40); $this->assertEquals(0, $ret['x']); $this->assertEquals(5, $ret['y']); $this->assertEquals(44, $ret['resize']['width']); @@ -344,7 +344,7 @@ public function testCropWidth() { ->will($this->returnArgument(0)); // Cropping with width only should preserve the aspect ratio. - $ret = $this->image->apply('crop', array ('x' => 0, 'y' => 0, 'width' => 44)); + $ret = $this->image->crop(0, 0, 44); $this->assertEquals(50, $ret['height']); } @@ -358,7 +358,7 @@ public function testCropHeight() { ->will($this->returnArgument(0)); // Cropping with height only should preserve the aspect ratio. - $ret = $this->image->apply('crop', array ('x' => 0, 'y' => 0, 'height' => 50)); + $ret = $this->image->crop(0, 0, NULL, 50); $this->assertEquals(44, $ret['width']); } @@ -371,7 +371,7 @@ public function testCrop() { ->method('execute') ->will($this->returnArgument(0)); - $ret = $this->image->apply('crop', array ('x' => 0, 'y' => 0, 'width' => 44, 'height' => 50)); + $ret = $this->image->crop(0, 0, 44, 50); $this->assertEquals(44, $ret['width']); } @@ -385,7 +385,7 @@ public function testResize() { ->will($this->returnArgument(0)); // Resize with integer for width and height. - $ret = $this->image->apply('resize', array('width' => 30, 'height' => 40)); + $ret = $this->image->resize(30, 40); $this->assertEquals(30, $ret['width']); $this->assertEquals(40, $ret['height']); } @@ -400,7 +400,7 @@ public function testFloatResize() { ->will($this->returnArgument(0)); // Pass a float for width. - $ret = $this->image->apply('resize', array('width' => 30.4, 'height' => 40)); + $ret = $this->image->resize(30.4, 40); // Ensure that the float was rounded to an integer first. $this->assertEquals(30, $ret['width']); } @@ -414,7 +414,7 @@ public function testDesaturate() { ->method('execute') ->will($this->returnArgument(0)); - $this->image->apply('desaturate'); + $this->image->desaturate(); } /** @@ -426,7 +426,7 @@ public function testRotate() { ->method('execute') ->will($this->returnArgument(0)); - $ret = $this->image->apply('rotate', array('degrees' => 90)); + $ret = $this->image->rotate(90); $this->assertEquals(90, $ret['degrees']); }