diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 47ee5aa..4e5a2b2 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -58,6 +58,7 @@ class Image implements ImageInterface { */ public function __construct(ImageToolkitInterface $toolkit, $source = NULL) { $this->toolkit = $toolkit; + $this->toolkit->setImage($this); if ($source) { $this->source = $source; $this->parseFile(); @@ -75,14 +76,14 @@ public function isValid() { * {@inheritdoc} */ public function getHeight() { - return $this->toolkit->getHeight($this); + return $this->toolkit->getHeight(); } /** * {@inheritdoc} */ public function getWidth() { - return $this->toolkit->getWidth($this); + return $this->toolkit->getWidth(); } /** @@ -96,7 +97,7 @@ public function getFileSize() { * {@inheritdoc} */ public function getMimeType() { - return $this->toolkit->getMimeType($this); + return $this->toolkit->getMimeType(); } /** @@ -130,7 +131,7 @@ public function save($destination = NULL) { } $destination = $destination ?: $this->getSource(); - if ($return = $this->toolkit->save($this, $destination)) { + if ($return = $this->toolkit->save($destination)) { // Clear the cached file size and refresh the image information. clearstatcache(TRUE, $destination); $this->fileSize = filesize($destination); @@ -156,7 +157,7 @@ public function save($destination = NULL) { * image information is populated. */ protected function parseFile() { - if ($this->valid = $this->toolkit->parseFile($this)) { + if ($this->valid = $this->toolkit->parseFile()) { $this->fileSize = filesize($this->source); } return $this->valid; @@ -166,7 +167,7 @@ protected function parseFile() { * {@inheritdoc} */ public function apply($operation, array $arguments = array()) { - return $this->toolkit->apply($operation, $this, $arguments); + return $this->toolkit->apply($operation, $arguments); } /** diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php index 5045eb5..e880b8f 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @@ -14,6 +14,13 @@ abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface { /** + * Image object this toolkit instance is tied to. + * + * @var \Drupal\Core\Image\ImageInterface + */ + protected $image; + + /** * The image toolkit operation manager. * * @var \Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface @@ -40,6 +47,23 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi /** * {@inheritdoc} */ + public function setImage(ImageInterface $image) { + if ($this->image) { + throw new \BadMethodCallException(__METHOD__ . '() may only be called once.'); + } + $this->image = $image; + } + + /** + * {@inheritdoc} + */ + public function getImage() { + return $this->image; + } + + /** + * {@inheritdoc} + */ public function getRequirements() { return array(); } @@ -60,10 +84,10 @@ protected function getToolkitOperation($operation) { /** * {@inheritdoc} */ - public function apply($operation, ImageInterface $image, array $arguments = array()) { + public function apply($operation, array $arguments = array()) { try { // Get the plugin to use for the operation and apply the operation. - return $this->getToolkitOperation($operation)->apply($image, $arguments); + return $this->getToolkitOperation($operation)->apply($this->getImage(), $arguments); } catch (PluginNotFoundException $e) { \Drupal::logger('image')->error('The selected image handling toolkit %toolkit can not process operation %operation.', array('%toolkit' => $this->getPluginId(), '%operation' => $operation)); diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php index 04d32f3..30d424e 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php @@ -46,7 +46,7 @@ interface ImageToolkitInterface extends PluginInspectionInterface { /** - * Retrieves toolkit's settings form. + * Retrieves the toolkit's settings form. * * @see system_image_toolkit_settings() */ @@ -60,62 +60,67 @@ public function settingsForm(); public function settingsFormSubmit($form, &$form_state); /** - * Writes an image resource to a destination file. + * Sets the image object that this toolkit instance is tied to. * * @param \Drupal\Core\Image\ImageInterface $image - * An image object. + * The image that this toolkit instance will be tied to. + * + * @throws \BadMethodCallException + * When called twice. + */ + public function setImage(ImageInterface $image); + + /** + * Gets the image object that this toolkit instance is tied to. + * + * @return \Drupal\Core\Image\ImageInterface + * The image object that this toolkit instance is tied to. + */ + public function getImage(); + + /** + * Writes an image resource to a destination file. + * * @param string $destination * A string file URI or path where the image should be saved. * * @return bool - * TRUE or FALSE, based on success. + * TRUE on success, FALSE on failure. */ - public function save(ImageInterface $image, $destination); + public function save($destination); /** * Determines if a file contains a valid image. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * * @return bool * TRUE if the file could be found and is an image, FALSE otherwise. */ - public function parseFile(ImageInterface $image); + public function parseFile(); /** * Returns the height of the image. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * * @return int|null * The height of the image, or NULL if the image is invalid. */ - public function getHeight(ImageInterface $image); + public function getHeight(); /** * Returns the width of the image. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * * @return int|null * The width of the image, or NULL if the image is invalid. */ - public function getWidth(ImageInterface $image); + public function getWidth(); /** * Returns the MIME type of the image file. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * * @return string * The MIME type of the image file, or an empty string if the image is * invalid. */ - public function getMimeType(ImageInterface $image); + public function getMimeType(); /** * Gets toolkit requirements in a format suitable for hook_requirements(). @@ -132,10 +137,10 @@ public function getMimeType(ImageInterface $image); public function getRequirements(); /** - * Verifies Image Toolkit is set up correctly. + * Verifies that the Image Toolkit is set up correctly. * * @return bool - * True if the GD toolkit is available on this machine. + * TRUE if the toolkit is available on this machine, FALSE otherwise. */ public static function isAvailable(); @@ -152,8 +157,6 @@ public static function getSupportedExtensions(); * * @param string $operation * The toolkit operation to be processed. - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. * @param array $arguments * An associative array of arguments to be passed to the toolkit * operation, e.g. array('width' => 50, 'height' => 100, @@ -162,6 +165,6 @@ public static function getSupportedExtensions(); * @return bool * TRUE if the operation was performed successfully, FALSE otherwise. */ - public function apply($operation, ImageInterface $image, array $arguments = array()); + public function apply($operation, array $arguments = array()); } diff --git a/core/modules/image/src/Tests/ImageEffectsTest.php b/core/modules/image/src/Tests/ImageEffectsTest.php index 4c692fe..a0aaf77 100644 --- a/core/modules/image/src/Tests/ImageEffectsTest.php +++ b/core/modules/image/src/Tests/ImageEffectsTest.php @@ -54,8 +54,8 @@ function testResizeEffect() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly'); - $this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly'); + $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly'); + $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly'); } /** @@ -71,8 +71,8 @@ function testScaleEffect() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scale'][0][1], 10, 'Width was passed correctly'); - $this->assertEqual($calls['scale'][0][2], 10, 'Height was based off aspect ratio and passed correctly'); + $this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly'); + $this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly'); } /** @@ -89,10 +89,10 @@ function testCropEffect() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['crop'][0][1], 0, 'X was passed correctly'); - $this->assertEqual($calls['crop'][0][2], 1, 'Y was passed correctly'); - $this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly'); - $this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly'); + $this->assertEqual($calls['crop'][0][0], 0, 'X was passed correctly'); + $this->assertEqual($calls['crop'][0][1], 1, 'Y was passed correctly'); + $this->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly'); + $this->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly'); } /** @@ -107,8 +107,8 @@ function testScaleAndCropEffect() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scale_and_crop'][0][1], 5, 'Width was computed and passed correctly'); - $this->assertEqual($calls['scale_and_crop'][0][2], 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'); } /** @@ -120,7 +120,7 @@ function testDesaturateEffect() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.'); + $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed'); } /** diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 7fbca7e..4ff0d9f 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -86,15 +86,12 @@ public function settingsFormSubmit($form, &$form_state) { /** * Loads a GD resource from a file. * - * @param \Drupal\Core\Image\ImageInterface $image - * An image object. - * * @return bool * TRUE or FALSE, based on success. */ - protected function load($image) { + protected function load() { $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE); - if (function_exists($function) && $resource = $function($image->getSource())) { + if (function_exists($function) && $resource = $function($this->getImage()->getSource())) { $this->setResource($resource); if (!imageistruecolor($resource)) { // Convert indexed images to true color, so that filters work @@ -112,7 +109,7 @@ protected function load($image) { /** * {@inheritdoc} */ - public function save(ImageInterface $image, $destination) { + public function save($destination) { $scheme = file_uri_scheme($destination); // Work around lack of stream wrapper support in imagejpeg() and imagepng(). if ($scheme && file_stream_wrapper_valid_scheme($scheme)) { @@ -151,11 +148,11 @@ public function save(ImageInterface $image, $destination) { /** * {@inheritdoc} */ - public function parseFile(ImageInterface $image) { - $data = @getimagesize($image->getSource()); + public function parseFile() { + $data = @getimagesize($this->getImage()->getSource()); if ($data && in_array($data[2], static::supportedTypes())) { $this->setType($data[2]); - $this->load($image); + $this->load(); return (bool) $this->getResource(); } return FALSE; @@ -218,14 +215,14 @@ public function createTmp($type, $width, $height) { /** * {@inheritdoc} */ - public function getWidth(ImageInterface $image) { + public function getWidth() { return $this->getResource() ? imagesx($this->getResource()) : NULL; } /** * {@inheritdoc} */ - public function getHeight(ImageInterface $image) { + public function getHeight() { return $this->getResource() ? imagesy($this->getResource()) : NULL; } @@ -247,7 +244,7 @@ public function getType() { * The image type represented by a PHP IMAGETYPE_* constant (e.g. * IMAGETYPE_JPEG). * - * @return this + * @return $this */ public function setType($type) { if (in_array($type, static::supportedTypes())) { @@ -259,7 +256,7 @@ public function setType($type) { /** * {@inheritdoc} */ - public function getMimeType(ImageInterface $image) { + public function getMimeType() { return $this->getType() ? image_type_to_mime_type($this->getType()) : ''; } diff --git a/core/modules/system/src/Tests/Image/ToolkitTest.php b/core/modules/system/src/Tests/Image/ToolkitTest.php index 5588c96..7c19bf6 100644 --- a/core/modules/system/src/Tests/Image/ToolkitTest.php +++ b/core/modules/system/src/Tests/Image/ToolkitTest.php @@ -59,8 +59,8 @@ function testResize() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['resize'][0][1], 1, 'Width was passed correctly'); - $this->assertEqual($calls['resize'][0][2], 2, 'Height was passed correctly'); + $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly'); + $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly'); } /** @@ -74,8 +74,8 @@ function testScale() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scale'][0][1], 10, 'Width was passed correctly'); - $this->assertEqual($calls['scale'][0][2], 10, 'Height was based off aspect ratio and passed correctly'); + $this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly'); + $this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly'); } /** @@ -89,8 +89,8 @@ function testScaleAndCrop() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['scale_and_crop'][0][1], 5, 'Width was computed and passed correctly'); - $this->assertEqual($calls['scale_and_crop'][0][2], 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'); } /** @@ -103,8 +103,8 @@ function testRotate() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['rotate'][0][1], 90, 'Degrees were passed correctly'); - $this->assertEqual($calls['rotate'][0][2], 1, 'Background color was passed correctly'); + $this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly'); + $this->assertEqual($calls['rotate'][0][1], 1, 'Background color was passed correctly'); } /** @@ -117,10 +117,10 @@ function testCrop() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual($calls['crop'][0][1], 1, 'X was passed correctly'); - $this->assertEqual($calls['crop'][0][2], 2, 'Y was passed correctly'); - $this->assertEqual($calls['crop'][0][3], 3, 'Width was passed correctly'); - $this->assertEqual($calls['crop'][0][4], 4, 'Height was passed correctly'); + $this->assertEqual($calls['crop'][0][0], 1, 'X was passed correctly'); + $this->assertEqual($calls['crop'][0][1], 2, 'Y was passed correctly'); + $this->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly'); + $this->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly'); } /** @@ -132,6 +132,6 @@ function testDesaturate() { // Check the parameters. $calls = $this->imageTestGetAllCalls(); - $this->assertEqual(count($calls['desaturate'][0]), 1, 'Only the image was passed.'); + $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.'); } } 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 7c24947..5b56734 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 @@ -8,7 +8,6 @@ namespace Drupal\image_test\Plugin\ImageToolkit; use Drupal\Component\Utility\Unicode; -use Drupal\Core\Image\ImageInterface; use Drupal\Core\ImageToolkit\ImageToolkitBase; /** @@ -70,9 +69,9 @@ public function settingsFormSubmit($form, &$form_state) { /** * {@inheritdoc} */ - public function parseFile(ImageInterface $image) { - $this->logCall('parseFile', array($image)); - $data = @getimagesize($image->getSource()); + public function parseFile() { + $this->logCall('parseFile', array()); + $data = @getimagesize($this->getImage()->getSource()); if ($data && in_array($data[2], static::supportedTypes())) { $this->setType($data[2]); $this->width = $data[0]; @@ -85,8 +84,8 @@ public function parseFile(ImageInterface $image) { /** * {@inheritdoc} */ - public function save(ImageInterface $image, $destination) { - $this->logCall('save', array($image, $destination)); + public function save($destination) { + $this->logCall('save', array($destination)); // Return false so that image_save() doesn't try to chmod the destination // file that we didn't bother to create. return FALSE; @@ -113,14 +112,14 @@ public function logCall($op, $args) { /** * {@inheritdoc} */ - public function getWidth(ImageInterface $image) { + public function getWidth() { return $this->width; } /** * {@inheritdoc} */ - public function getHeight(ImageInterface $image) { + public function getHeight() { return $this->height; } @@ -142,7 +141,7 @@ public function getType() { * The image type represented by a PHP IMAGETYPE_* constant (e.g. * IMAGETYPE_JPEG). * - * @return this + * @return $this */ public function setType($type) { if (in_array($type, static::supportedTypes())) { diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index 058e222..e3f64ab 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -204,11 +204,12 @@ public function testGetToolkitId() { public function testSave() { $this->getTestImage(); // This will fail if save() method isn't called on the toolkit. - $this->toolkit->expects($this->once()) + $toolkit = $this->getToolkitMock(); + $toolkit->expects($this->once()) ->method('save') ->will($this->returnValue(TRUE)); - $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($this->toolkit, $this->image->getSource())); + $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($toolkit, $this->image->getSource())); $image->expects($this->any()) ->method('chmod') ->will($this->returnValue(TRUE)); @@ -235,11 +236,12 @@ public function testSaveFails() { public function testChmodFails() { $this->getTestImage(); // This will fail if save() method isn't called on the toolkit. - $this->toolkit->expects($this->once()) + $toolkit = $this->getToolkitMock(); + $toolkit->expects($this->once()) ->method('save') ->will($this->returnValue(TRUE)); - $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($this->toolkit, $this->image->getSource())); + $image = $this->getMock('Drupal\Core\Image\Image', array('chmod'), array($toolkit, $this->image->getSource())); $image->expects($this->any()) ->method('chmod') ->will($this->returnValue(FALSE));