diff --git a/core/includes/image.inc b/core/includes/image.inc index 4d95416..447500e 100644 --- a/core/includes/image.inc +++ b/core/includes/image.inc @@ -52,9 +52,13 @@ * - "extension": Commonly used file extension for the image. * - "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png'). * - "file_size": File size in bytes. + * + * @deprecated as of Drupal 8.0. Use + * \Drupal\Core\Image\ImageFile::getFileInfo(). */ function image_get_info($filepath) { - return ImageFile::getFileInfo($filepath); + $image = new ImageFile($filepath); + return $image->getFileInfo(); } /** diff --git a/core/lib/Drupal/Core/Image/ImageFile.php b/core/lib/Drupal/Core/Image/ImageFile.php index 5bc67ab..3e914f5 100644 --- a/core/lib/Drupal/Core/Image/ImageFile.php +++ b/core/lib/Drupal/Core/Image/ImageFile.php @@ -11,108 +11,202 @@ use Drupal\system\Plugin\ImageToolkitInterface; /** - * @todo. + * Defines an image object to represent an image file. + * + * @see \Drupal\system\Plugin\ImageToolkitInterface + * @see \Drupal\image\ImageEffectInterface + * + * @ingroup image */ class ImageFile { /** + * String specifying the path of the image file. + * * @var string */ protected $source; /** + * An associative array containing information about the image: + * - width: Width, in pixels. + * - height: Height, in pixels. + * - extension: Commonly used file extension for the image. + * - mime_type: MIME type ('image/jpeg', 'image/gif', 'image/png'). + * - file_size: File size in bytes. + * * @var array */ protected $info; /** + * An image toolkit object. + * * @var \Drupal\system\Plugin\ImageToolkitInterface */ protected $toolkit; /** - * @var string + * An image file handle. + * + * @var resource */ protected $resource; /** - * @param $source + * Constructs a new ImageFile object. + * + * @param string $source + * The path to an image file. */ public function __construct($source) { $this->source = $source; } /** + * Sets a value for a given key. + * * @param string $key + * The key to set the value for. * @param mixed $value + * The value to set for this key. + * + * @return self + * Returns this image file. */ public function set($key, $value) { $this->info[$key] = $value; + return $this; } /** + * Replaces the file info. + * * @param array $info + * Information about this file. + * + * @return self + * Returns this image file. */ public function setInfo(array $info) { $this->info = $info; + return $this; } /** + * Retrieves specific information about this image file. + * * @param string $key + * The key of the info to retrieve, e.g., 'height', 'width', 'extension', + * 'mime_type', 'file_size'. * * @return mixed|null + * The value requested, or NULL. */ public function get($key) { - if (isset($this->info[$key])) { - return $this->info[$key]; + $info = $this->getInfo(); + if (isset($info[$key])) { + return $info[$key]; } return NULL; } /** + * Retrives all information about this image file. + * * @return array + * An associative array of image file information. */ public function getInfo() { + if (!$this->info) { + $this->processInfo(); + } return $this->info; } /** + * Sets the image file resource. + * * @param resource $resource + * The image file handle. + * + * @return self + * Returns this image file. */ public function setResource($resource) { $this->resource = $resource; + return $this; } /** + * Determines if this image file has a resource set. + * + * @return bool + * TRUE if this image file has a resource set, FALSE otherwise. + */ + public function hasResource() { + return (bool) $this->resource; + } + + /** + * Retrieves the image file resource. + * * @return resource + * The image file handle. */ public function getResource() { + if (!$this->hasResource()) { + $this->processInfo(); + $this->getToolkit()->load($this); + } return $this->resource; } /** + * Sets the source path of the image file. + * * @param string $source + * A string specifying the path of the image file. + * + * @return self + * Returns this image file. */ public function setSource($source) { $this->source = $source; + return $this; } /** + * Retrieves the source path of the image file. + * * @return string + * The source path of the image file. */ public function getSource() { return $this->source; } /** + * Sets a custom image toolkit. + * * @param \Drupal\system\Plugin\ImageToolkitInterface $toolkit + * The image toolkit to use for this image file. + * + * @return self + * Returns this image file. */ public function setToolkit(ImageToolkitInterface $toolkit) { $this->toolkit = $toolkit; + return $this; } /** + * Returns the image toolkit being used for this image file. + * + * If a custom toolkit was not specified, this will fallback to the default. + * * @return \Drupal\system\Plugin\ImageToolkitInterface + * The image toolkit used for this image file. */ protected function getToolkit() { if (!$this->toolkit) { @@ -122,50 +216,22 @@ protected function getToolkit() { } /** + * Returns the ID of the image toolkit used for this image file. + * * @return string + * The ID of the image toolkit. */ public function getToolkitId() { return $this->getToolkit()->getPluginId(); } /** - * Loads an image file and returns an image object. - * - * Any changes to the file are not saved until self::save() is called. - * - * @param string $source - * Path to an image file. - * @param \Drupal\system\Plugin\ImageToolkitInterface|null $toolkit - * (optional) Image toolkit object to override the default. - * - * @return \Drupal\Core\Image\ImageFile|bool - * An image object or FALSE if there was a problem loading the file. - */ - public static function load($source, ImageToolkitInterface $toolkit = NULL) { - $image = new static($source); - if ($toolkit) { - $image->setToolkit($toolkit); - } - - $image->processInfo($source); - if ($image->getToolkit()->load($image)) { - return $image; - } - return FALSE; - } - - /** * Gets details about an image. * * Drupal supports GIF, JPG and PNG file formats when used with the GD * toolkit, and may support others, depending on which toolkits are * installed. * - * @param string $source - * String specifying the path of the image file. - * @param \Drupal\system\Plugin\ImageToolkitInterface|null $toolkit - * (optional) Image toolkit object to override the default. - * * @return array|bool * FALSE, if the file could not be found or is not an image. Otherwise, a * keyed array containing information about the image: @@ -175,14 +241,9 @@ public static function load($source, ImageToolkitInterface $toolkit = NULL) { * - "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png'). * - "file_size": File size in bytes. */ - public static function getFileInfo($source, ImageToolkitInterface $toolkit = NULL) { - $image = new static($source); - if ($toolkit) { - $image->setToolkit($toolkit); - } - - if ($image->processInfo()) { - return $image->getInfo(); + public function getFileInfo() { + if ($this->processInfo()) { + return $this->getInfo(); } return FALSE; } @@ -201,12 +262,13 @@ public static function getFileInfo($source, ImageToolkitInterface $toolkit = NUL */ public function save($destination = NULL) { if (empty($destination)) { - $destination = $this->source; + $destination = $this->getSource(); } if ($return = $this->getToolkit()->save($this, $destination)) { // Clear the cached file size and refresh the image information. clearstatcache(TRUE, $destination); - $this->processInfo($destination); + $this->setSource($destination); + $this->processInfo(); if (drupal_chmod($destination)) { return $return; @@ -216,26 +278,22 @@ public function save($destination = NULL) { } /** - * @param string|null $destination + * Prepares the image information. * * @return bool * FALSE, if the file could not be found or is not an image. Otherwise, the * image information is populated. */ - public function processInfo($destination = NULL) { - if (empty($destination)) { - $destination = $this->getSource(); - } - + protected function processInfo() { + $destination = $this->getSource(); if (!is_file($destination) && !is_uploaded_file($destination)) { return FALSE; } - $details = $this->getToolkit()->getInfo($this); - if (isset($details) && is_array($details)) { + if ($details = $this->getToolkit()->getInfo($this)) { $details['file_size'] = filesize($destination); + $this->setInfo($details); } - $this->setInfo($details); return TRUE; } diff --git a/core/modules/file/file.module b/core/modules/file/file.module index f437cae..ebd9467 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -463,7 +463,8 @@ function file_validate_image_resolution(File $file, $maximum_dimensions = 0, $mi list($width, $height) = explode('x', $maximum_dimensions); if ($info['width'] > $width || $info['height'] > $height) { // Try to resize the image to fit the dimensions. - if ($image = ImageFile::load($file->getFileUri())) { + $image = new ImageFile($file->getFileUri()); + if ($image->hasResource()) { $image->scale($width, $height); $image->save(); $file->filesize = $image->get('file_size'); diff --git a/core/modules/image/image.module b/core/modules/image/image.module index cbfd743..04f4fda 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -481,7 +481,7 @@ function image_style_deliver($style, $scheme) { } if ($success) { - $image = ImageFile::load($derivative_uri); + $image = new ImageFile($derivative_uri); $uri = $image->getSource(); $headers = array( 'Content-Type' => $image->get('mime_type'), @@ -525,7 +525,8 @@ function image_style_create_derivative($style, $source, $destination) { return FALSE; } - if (!$image = ImageFile::load($source)) { + $image = new ImageFile($source); + if (!$image->hasResource()) { return FALSE; } 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 b2adce4..70be018 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php @@ -159,12 +159,12 @@ public function load(ImageFile $image) { $function = 'imagecreatefrom' . $extension; if (function_exists($function) && $resource = $function($image->getSource())) { $image->setResource($resource); - if (!imageistruecolor($image->getResource())) { + 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($image, $image->get('width'), $image->get('height')); - imagecopy($new_image, $image->getResource(), 0, 0, 0, 0, $image->get('width'), $image->get('height')); - imagedestroy($image->getResource()); + imagecopy($new_image, $resource, 0, 0, 0, 0, $image->get('width'), $image->get('height')); + imagedestroy($resource); $image->setResource($new_image); } return (bool) $image->getResource(); diff --git a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php index e02a6e1..fb03c41 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Image/ToolkitGdTest.php @@ -220,7 +220,7 @@ function testManipulations() { foreach ($files as $file) { foreach ($operations as $op => $values) { // Load up a fresh image. - $image = ImageFile::load(drupal_get_path('module', 'simpletest') . '/files/' . $file, $manager->createInstance('gd')); + $image = new ImageFile(drupal_get_path('module', 'simpletest') . '/files/' . $file, $manager->createInstance('gd')); if (!$image) { $this->fail(t('Could not load image %file.', array('%file' => $file))); continue 2; 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 1021372..0713e3b 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,8 @@ function testGetAvailableToolkits() { * Test the image_load() function. */ function testLoad() { - $image = ImageFile::load($this->file, $this->toolkit); + $image = new ImageFile($this->file); + $image->setToolkit($this->toolkit)->getResource(); $this->assertTrue(is_object($image), 'Returned an object.'); $this->assertEqual($this->toolkit->getPluginId(), $image->getToolkitId(), t('Image had toolkit set.')); $this->assertToolkitOperationsCalled(array('load', 'get_info'));