diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 6350cf1..fafb395 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -41,13 +41,6 @@ class Image implements ImageInterface { protected $fileSize; /** - * If this image object is valid. - * - * @var bool - */ - protected $valid = FALSE; - - /** * Constructs a new Image object. * * @param \Drupal\Core\ImageToolkit\ImageToolkitInterface $toolkit @@ -69,15 +62,7 @@ public function __construct(ImageToolkitInterface $toolkit, $source = NULL) { * {@inheritdoc} */ public function isValid() { - return $this->valid; - } - - /** - * {@inheritdoc} - */ - public function setValid($valid) { - $this->valid = $valid; - return $this; + return $this->getToolkit()->isValid(); } /** diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index 43e0950..02db478 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -21,16 +21,6 @@ public function isValid(); /** - * Sets the image validity flag. - * - * @param bool $valid - * TRUE if the image object contains a valid image, FALSE otherwise. - * - * @return $this - */ - public function setValid($valid); - - /** * Returns the height of the image. * * @return int|null diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php index b50331a..a4a1f6f 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php @@ -80,6 +80,15 @@ public function setImage(ImageInterface $image); public function getImage(); /** + * Checks if the image is valid. + * + * @return bool + * TRUE if the image toolkit is currently handling a valid image, FALSE + * otherwise. + */ + public function isValid(); + + /** * Writes an image resource to a destination file. * * @param string $destination diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 32c90b4..b88a220 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -51,6 +51,9 @@ class GDToolkit extends ImageToolkitBase { * @return $this */ public function setResource($resource) { + if (isset($this->preLoadInfo)) { + unset($this->preLoadInfo); + } $this->resource = $resource; return $this; } @@ -62,14 +65,10 @@ public function setResource($resource) { * The GD image resource, or NULL if not available. */ public function getResource() { - if (isset($this->resource)) { - return $this->resource; - } - elseif ($this->getImage()->isValid() && isset($this->preLoadInfo) && $this->load()) { - unset($this->preLoadInfo); - return $this->resource; + if (!isset($this->resource)) { + $this->load(); } - return NULL; + return $this->resource; } /** @@ -104,10 +103,15 @@ public function settingsFormSubmit($form, FormStateInterface $form_state) { * TRUE or FALSE, based on success. */ protected function load() { + // Return immediately if the image file is not valid. + if (!$this->isValid()) { + return FALSE; + } + $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE); if (function_exists($function) && $resource = $function($this->getImage()->getSource())) { - $this->setResource($resource); if (imageistruecolor($resource)) { + $this->setResource($resource); return TRUE; } else { @@ -119,17 +123,22 @@ protected function load() { imagedestroy($resource); $this->setResource($new_image); } - $this->getImage()->setValid($ret); return $ret; } } - $this->getImage()->setValid(FALSE); return FALSE; } /** * {@inheritdoc} */ + public function isValid() { + return isset($this->preLoadInfo) || isset($this->resource); + } + + /** + * {@inheritdoc} + */ public function save($destination) { $scheme = file_uri_scheme($destination); // Work around lack of stream wrapper support in imagejpeg() and imagepng(). @@ -174,10 +183,8 @@ public function parseFile() { if ($data && in_array($data[2], static::supportedTypes())) { $this->setType($data[2]); $this->preLoadInfo = $data; - $this->getImage()->setValid(TRUE); return TRUE; } - $this->getImage()->setValid(FALSE); return FALSE; } @@ -242,7 +249,12 @@ public function getWidth() { if (isset($this->preLoadInfo)) { return $this->preLoadInfo[0]; } - return $this->getResource() ? imagesx($this->getResource()) : NULL; + elseif ($res = $this->getResource()) { + return imagesx($res); + } + else { + return NULL; + } } /** @@ -252,7 +264,12 @@ public function getHeight() { if (isset($this->preLoadInfo)) { return $this->preLoadInfo[1]; } - return $this->getResource() ? imagesy($this->getResource()) : NULL; + elseif ($res = $this->getResource()) { + return imagesy($res); + } + else { + return NULL; + } } /** 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 ac62447..509ec9c 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 @@ -70,6 +70,13 @@ public function settingsFormSubmit($form, FormStateInterface $form_state) { /** * {@inheritdoc} */ + public function isValid() { + return isset($this->type); + } + + /** + * {@inheritdoc} + */ public function parseFile() { $this->logCall('parseFile', func_get_args()); $data = @getimagesize($this->getImage()->getSource()); @@ -77,10 +84,8 @@ public function parseFile() { $this->setType($data[2]); $this->width = $data[0]; $this->height = $data[1]; - $this->getImage()->setValid(TRUE); return TRUE; } - $this->getImage()->setValid(FALSE); return FALSE; } diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index b5afb3e..fa91f28 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -94,7 +94,7 @@ protected function getToolkitOperationMock($class_name, ImageToolkitInterface $t * Get an image with a mocked toolkit, for testing. * * @param bool $load_expected - * (optional) Whether the load() method is expected to ba called. Defaults + * (optional) Whether the load() method is expected to be called. Defaults * to TRUE. * @param array $stubs * (optional) Array containing toolkit methods to be replaced with stubs.