diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 4cbc63a..d1edb6e 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -57,15 +57,15 @@ class Image implements ImageInterface { /** * Constructs a new Image object. * - * @param string $source - * The path to an image file, or an empty string to construct the object - * with no image source. * @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit * The image toolkit. + * @param string|null $source + * (optional) The path to an image file, or NULL to construct the object + * with no image source. */ - public function __construct($source, ImageToolkitInterface $toolkit) { + public function __construct(ImageToolkitInterface $toolkit, $source = NULL) { $this->toolkit = $toolkit; - if (!empty($source)) { + if ($source) { $this->source = $source; $this->processInfo(); } @@ -216,7 +216,7 @@ protected function processInfo() { * interface method ImageInterface::apply(). An image operation will be * performed as in the next example: * @code - * $image = new Image($path, $toolkit); + * $image = new Image($toolkit, $path); * $image->apply('scale', array('width' => 50, 'height' => 100)); * @endcode * Also in https://drupal.org/node/2110499 operation arguments sent to toolkit diff --git a/core/lib/Drupal/Core/Image/ImageFactory.php b/core/lib/Drupal/Core/Image/ImageFactory.php index a909ff4..c7adf4c 100644 --- a/core/lib/Drupal/Core/Image/ImageFactory.php +++ b/core/lib/Drupal/Core/Image/ImageFactory.php @@ -74,19 +74,20 @@ public function getToolkitId() { * current factory settings, by passing its plugin ID in the $toolkit_id * argument. * - * @param string $source - * The path to an image file, or an empty string to construct the object + * @param string|null $source + * (optional) The path to an image file, or NULL to construct the object * with no image source. - * @param string $toolkit_id - * (Optional) The image toolkit ID to use for this image. + * @param string|null $toolkit_id + * (optional) The ID of the image toolkit to use for this image, or NULL + * to use the current toolkit. * * @return \Drupal\Core\Image\ImageInterface * An Image object. * * @see ImageFactory::setToolkitId() */ - public function get($source, $toolkit_id = '') { - $toolkit_id = empty($toolkit_id) ? $this->toolkitId : $toolkit_id; - return new Image($source, $this->toolkitManager->createInstance($toolkit_id)); + public function get($source = NULL, $toolkit_id = NULL) { + $toolkit_id = $toolkit_id ?: $this->toolkitId; + return new Image($this->toolkitManager->createInstance($toolkit_id), $source); } } diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index 344b34e..7e0e1a5 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -31,16 +31,16 @@ public function isExisting(); /** * Returns the height of the image. * - * @return int - * The height of the image, or 0 if the image is invalid. + * @return int|null + * The height of the image, or NULL if the image is invalid. */ public function getHeight(); /** * Returns the width of the image. * - * @return int - * The width of the image, or 0 if the image is invalid. + * @return int|null + * The width of the image, or NULL if the image is invalid. */ public function getWidth(); diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php index 8ca05a2..177057b 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php @@ -190,9 +190,9 @@ public function scaleAndCrop(ImageInterface $image, $width, $height); * @param \Drupal\Core\Image\ImageInterface $image * An image object. * - * @return array|FALSE - * FALSE, if the file could not be found or is not an image. Otherwise, a - * keyed array containing information about the image: + * @return array + * If the file could not be found or is not an image, an empty array; + * otherwise, a keyed array containing information about the image: * - "type": Image type represented as an IMAGETYPE_* constant. * * @see \Drupal\Core\Image\ImageInterface::processInfo() @@ -205,8 +205,8 @@ public function getInfo(ImageInterface $image); * @param \Drupal\Core\Image\ImageInterface $image * An image object. * - * @return int - * The height of the image, or 0 if the image is invalid. + * @return int|null + * The height of the image, or NULL if the image is invalid. */ public function getHeight(ImageInterface $image); @@ -216,8 +216,8 @@ public function getHeight(ImageInterface $image); * @param \Drupal\Core\Image\ImageInterface $image * An image object. * - * @return int - * The width of the image, or 0 if the image is invalid. + * @return int|null + * The width of the image, or NULL if the image is invalid. */ public function getWidth(ImageInterface $image); diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php index b6ce30c..e067014 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php @@ -292,16 +292,11 @@ public function save(ImageInterface $image, $destination) { * {@inheritdoc} */ public function getInfo(ImageInterface $image) { - $details = FALSE; + $details = array(); $data = getimagesize($image->getSource()); if (isset($data) && is_array($data) && in_array($data[2], static::supportedTypes())) { - $details = array( - 'type' => $data[2], - ); - } - - if ($details) { + $details['type'] = $data[2]; $this->load($image->getSource(), $details); } return $details; @@ -356,14 +351,14 @@ public function createTmp($type, $width, $height) { * {@inheritdoc} */ public function getWidth(ImageInterface $image) { - return $this->getResource() ? imagesx($this->getResource()) : 0; + return $this->getResource() ? imagesx($this->getResource()) : NULL; } /** * {@inheritdoc} */ public function getHeight(ImageInterface $image) { - return $this->getResource() ? imagesy($this->getResource()) : 0; + return $this->getResource() ? imagesy($this->getResource()) : NULL; } /** diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php index 8ee44dd..11fa771 100644 --- a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php +++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php @@ -25,14 +25,14 @@ class TestToolkit extends ImageToolkitBase { * * @var int */ - protected $width = 0; + protected $width; /** * The height of the image. * * @var int */ - protected $height = 0; + protected $height; /** * {@inheritdoc} @@ -65,16 +65,11 @@ public function settingsFormSubmit($form, &$form_state) { public function getInfo(ImageInterface $image) { $this->logCall('get_info', array($image)); - $details = FALSE; + $details = array(); $data = getimagesize($image->getSource()); if (isset($data) && is_array($data) && in_array($data[2], static::supportedTypes())) { - $details = array( - 'type' => $data[2], - ); - } - - if ($details) { + $details['type'] = $data[2]; $this->width = $data[0]; $this->height = $data[1]; $this->load($image->getSource(), $details); diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index 43a8740..2eebde5 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -46,7 +46,7 @@ protected function setUp() { ->method('getPluginId') ->will($this->returnValue('gd')); - $this->image = new Image($this->source, $this->toolkit); + $this->image = new Image($this->toolkit, $this->source); } /** @@ -176,7 +176,7 @@ public function testChmodFails() { */ public function testProcessInfoFails() { $toolkit = $this->getToolkitMock(); - $image = new Image('magic-foobars.png', $toolkit); + $image = new Image($toolkit, 'magic-foobars.png'); $this->assertFalse($image->isExisting()); } @@ -186,7 +186,7 @@ public function testProcessInfoFails() { */ public function testScaleWidth() { $toolkit = $this->getToolkitMock(array('resize')); - $image = new Image($this->source, $toolkit); + $image = new Image($toolkit, $this->source); $toolkit->expects($this->any()) ->method('resize') @@ -200,7 +200,7 @@ public function testScaleWidth() { */ public function testScaleHeight() { $toolkit = $this->getToolkitMock(array('resize')); - $image = new Image($this->source, $toolkit); + $image = new Image($toolkit, $this->source); $toolkit->expects($this->any()) ->method('resize') @@ -214,7 +214,7 @@ public function testScaleHeight() { */ public function testScaleSame() { $toolkit = $this->getToolkitMock(array('resize')); - $image = new Image($this->source, $toolkit); + $image = new Image($toolkit, $this->source); // Dimensions are the same, resize should not be called. $toolkit->expects($this->never()) @@ -230,7 +230,7 @@ public function testScaleSame() { */ public function testScaleAndCropWidth() { $toolkit = $this->getToolkitMock(array('resize', 'crop')); - $image = new Image($this->source, $toolkit); + $image = new Image($toolkit, $this->source); $toolkit->expects($this->once()) ->method('resize') @@ -249,7 +249,7 @@ public function testScaleAndCropWidth() { */ public function testScaleAndCropHeight() { $toolkit = $this->getToolkitMock(array('resize', 'crop')); - $image = new Image($this->source, $toolkit); + $image = new Image($toolkit, $this->source); $toolkit->expects($this->once()) ->method('resize') @@ -268,7 +268,7 @@ public function testScaleAndCropHeight() { */ public function testScaleAndCropFails() { $toolkit = $this->getToolkitMock(array('resize', 'crop')); - $image = new Image($this->source, $toolkit); + $image = new Image($toolkit, $this->source); $toolkit->expects($this->once()) ->method('resize') @@ -304,7 +304,7 @@ public function testScaleAndCropFails() { */ public function testCrop() { $toolkit = $this->getToolkitMock(array('crop')); - $image = new Image($this->source, $toolkit); + $image = new Image($toolkit, $this->source); $toolkit->expects($this->once()) ->method('crop') @@ -328,7 +328,7 @@ public function testCrop() { */ public function testDesaturate() { $toolkit = $this->getToolkitMock(array('desaturate')); - $image = new Image($this->source, $toolkit); + $image = new Image($toolkit, $this->source); $toolkit->expects($this->once()) ->method('desaturate'); @@ -340,7 +340,7 @@ public function testDesaturate() { */ public function testRotate() { $toolkit = $this->getToolkitMock(array('rotate')); - $image = new Image($this->source, $toolkit); + $image = new Image($toolkit, $this->source); $toolkit->expects($this->once()) ->method('rotate');