diff --git a/core/lib/Drupal/Core/Image/Image.php b/core/lib/Drupal/Core/Image/Image.php index 215882a..228a718 100644 --- a/core/lib/Drupal/Core/Image/Image.php +++ b/core/lib/Drupal/Core/Image/Image.php @@ -59,7 +59,7 @@ class Image implements ImageInterface { public function __construct(ImageToolkitInterface $toolkit, $source = NULL) { $this->toolkit = $toolkit; if ($source) { - $this->loadFromFile($source); + $this->parseFile($source); } } @@ -152,7 +152,7 @@ public function save($destination = NULL) { } /** - * Loads the image from a file. + * Determines if a file contains a valid image. * * Drupal supports GIF, JPG and PNG file formats when used with the GD * toolkit, and may support others, depending on which toolkits are @@ -165,20 +165,15 @@ public function save($destination = NULL) { * FALSE, if the file could not be found or is not an image. Otherwise, the * image information is populated. */ - protected function loadFromFile($source) { - // Return immediately if the image has been loaded already. - if ($this->isValid()) { - return TRUE; - } - + protected function parseFile($source) { // Check that file is valid. if (!is_file($source) && !is_uploaded_file($source)) { return FALSE; } - // Defer image loading to toolkit. + // Defer file parsing to toolkit. $this->setSource($source); - if ($this->toolkit->loadFromFile($this)) { + if ($this->toolkit->parseFile($this)) { $this->fileSize = filesize($source); $this->valid = TRUE; return TRUE; diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php index 17ff549..78545ce 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php @@ -185,7 +185,7 @@ public function scale(ImageInterface $image, $width = NULL, $height = NULL, $ups public function scaleAndCrop(ImageInterface $image, $width, $height); /** - * Loads the image from a file. + * Determines if a file contains a valid image. * * @param \Drupal\Core\Image\ImageInterface $image * An image object. @@ -193,7 +193,7 @@ public function scaleAndCrop(ImageInterface $image, $width, $height); * @return bool * TRUE, if the file could be found and is an image. Otherwise, FALSE. */ - public function loadFromFile(ImageInterface $image); + public function parseFile(ImageInterface $image); /** * Returns the height of the image. diff --git a/core/modules/file/file.module b/core/modules/file/file.module index bc9af6f..607618e 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -396,7 +396,7 @@ function file_validate_size(File $file, $file_limit = 0, $user_limit = 0) { } /** - * Checks that the file is recognized by Image::loadFromFile() as an image. + * Checks that the file is recognized as a valid image. * * @param \Drupal\file\File $file * A file entity. 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 bd971a2..2b8159c 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php @@ -237,6 +237,36 @@ public function scaleAndCrop(ImageInterface $image, $width, $height) { } /** + * Loads a GD resource from a file. + * + * @param string $source + * String specifying the path of the image file. + * @param int $type + * Image type represented by a PHP IMAGETYPE_* constant (e.g. + * IMAGETYPE_JPEG). + * + * @return bool + * TRUE or FALSE, based on success. + */ + protected function load($source, $type) { + $function = 'imagecreatefrom' . image_type_to_extension($type, FALSE); + if (function_exists($function) && $resource = $function($source)) { + $this->setResource($resource); + if (!imageistruecolor($resource)) { + // Convert indexed images to true color, so that filters work + // correctly and don't result in unnecessary dither. + $new_image = $this->createTmp($type, imagesx($resource), imagesy($resource)); + imagecopy($new_image, $resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource)); + imagedestroy($resource); + $this->setResource($new_image); + } + return (bool) $this->getResource(); + } + + return FALSE; + } + + /** * {@inheritdoc} */ public function save(ImageInterface $image, $destination) { @@ -278,26 +308,15 @@ public function save(ImageInterface $image, $destination) { /** * {@inheritdoc} */ - public function loadFromFile(ImageInterface $image) { + public function parseFile(ImageInterface $image) { $source = $image->getSource(); $data = getimagesize($source); if (isset($data) && is_array($data) && in_array($data[2], static::supportedTypes())) { $this->setType($data[2]); - $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE); - if (function_exists($function) && $resource = $function($source)) { - $this->setResource($resource); - if (!imageistruecolor($resource)) { - // Convert indexed images to true color, so that filters work - // correctly and don't result in unnecessary dither. - $new_image = $this->createTmp($this->getType(), imagesx($resource), imagesy($resource)); - imagecopy($new_image, $resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource)); - imagedestroy($resource); - $this->setResource($new_image); - } - return (bool) $this->getResource(); - } + $this->load($source, $image->getType()); + return (bool) $this->getResource(); } return FALSE; diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php index 35b201a..dc60eca 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTest.php @@ -38,7 +38,7 @@ function testLoad() { $image = $this->getImage(); $this->assertTrue(is_object($image), 'Returned an object.'); $this->assertEqual($image->getToolkitId(), 'test', 'Image had toolkit set.'); - $this->assertToolkitOperationsCalled(array('load_from_file')); + $this->assertToolkitOperationsCalled(array('parseFile')); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php index d5da542..c8b04e1 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitTestBase.php @@ -109,7 +109,7 @@ function assertToolkitOperationsCalled(array $expected) { function imageTestReset() { // Keep track of calls to these operations $results = array( - 'load_from_file' => array(), + 'parseFile' => array(), 'save' => array(), 'settings' => array(), 'resize' => array(), @@ -124,7 +124,7 @@ function imageTestReset() { * Gets an array of calls to the test toolkit. * * @return array - * An array keyed by operation name ('load_from_file', 'save', 'settings', + * An array keyed by operation name ('parseFile', 'save', 'settings', * 'resize', 'rotate', 'crop', 'desaturate') with values being arrays of * parameters passed to each call. */ 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 47ea0c1..dceb812 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 @@ -70,8 +70,8 @@ public function settingsFormSubmit($form, &$form_state) { /** * {@inheritdoc} */ - public function loadFromFile(ImageInterface $image) { - $this->logCall('load_from_file', array($image)); + public function parseFile(ImageInterface $image) { + $this->logCall('parseFile', array($image)); $source = $image->getSource(); @@ -149,8 +149,8 @@ public function scaleAndCrop(ImageInterface $image, $width, $height) { * Stores the values passed to a toolkit call. * * @param string $op - * One of the image toolkit operations: 'load_from_file', 'save', - * 'settings', 'resize', 'rotate', 'crop', 'desaturate'. + * One of the image toolkit operations: 'parseFile', 'save', 'settings', + * 'resize', 'rotate', 'crop', 'desaturate'. * @param array $args * Values passed to hook. * diff --git a/core/tests/Drupal/Tests/Core/Image/ImageTest.php b/core/tests/Drupal/Tests/Core/Image/ImageTest.php index 3c6c6ad..36cdb3e 100644 --- a/core/tests/Drupal/Tests/Core/Image/ImageTest.php +++ b/core/tests/Drupal/Tests/Core/Image/ImageTest.php @@ -172,9 +172,9 @@ public function testChmodFails() { } /** - * Tests \Drupal\Core\Image\Image::loadFromFile(). + * Tests \Drupal\Core\Image\Image::parseFile(). */ - public function testLoadFromFileFails() { + public function testParseFileFails() { $toolkit = $this->getToolkitMock(); $image = new Image($toolkit, 'magic-foobars.png');