diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-09-01 17:49:11 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2009-09-01 17:49:11 +0000 |
commit | 18b7e4254bde471e4c2060a430b3f219f917c4b9 (patch) | |
tree | 893a4cdabfd4c3b5f43424a203ea4094797a6b7f | |
parent | c4a548f64d3c941a36edd03903ba388c6c32c1ae (diff) | |
download | brdo-18b7e4254bde471e4c2060a430b3f219f917c4b9.tar.gz brdo-18b7e4254bde471e4c2060a430b3f219f917c4b9.tar.bz2 |
#269337 by egfrith and wrunt: Added support for more image types (PDF, TIFF, EPS, etc.).
-rw-r--r-- | includes/image.inc | 56 | ||||
-rw-r--r-- | modules/simpletest/tests/image.test | 2 | ||||
-rw-r--r-- | modules/simpletest/tests/image_test.module | 12 | ||||
-rw-r--r-- | modules/system/image.gd.inc | 33 |
4 files changed, 75 insertions, 28 deletions
diff --git a/includes/image.inc b/includes/image.inc index dfc1d3002..d6075fe37 100644 --- a/includes/image.inc +++ b/includes/image.inc @@ -101,36 +101,40 @@ function image_toolkit_invoke($method, stdClass $image, array $params = array()) /** * Get details about an image. * - * Drupal only supports GIF, JPG and PNG file formats. + * 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 $filepath * String specifying the path of the image file. + * @param $toolkit + * An optional image toolkit name to override the default. * @return * FALSE, if the file could not be found or is not an image. Otherwise, a * keyed 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. + * - "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. */ -function image_get_info($filepath) { +function image_get_info($filepath, $toolkit = FALSE) { + $details = FALSE; if (!is_file($filepath)) { - return FALSE; + return $details; } - $details = FALSE; - $data = @getimagesize($filepath); - $file_size = @filesize($filepath); - - if (isset($data) && is_array($data)) { - $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png'); - $extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : ''; - $details = array('width' => $data[0], - 'height' => $data[1], - 'extension' => $extension, - 'file_size' => $file_size, - 'mime_type' => $data['mime']); + if (!$toolkit) { + $toolkit = image_get_toolkit(); + } + if ($toolkit) { + $image = new stdClass(); + $image->source = $filepath; + $image->toolkit = $toolkit; + $details = image_toolkit_invoke('get_info', $image); + if (isset($details) && is_array($details)) { + $details['file_size'] = filesize($filepath); + } } return $details; @@ -343,10 +347,12 @@ function image_load($file, $toolkit = FALSE) { if ($toolkit) { $image = new stdClass(); $image->source = $file; - $image->info = image_get_info($file); - $image->toolkit = $toolkit; - if (image_toolkit_invoke('load', $image)) { - return $image; + $image->info = image_get_info($file, $toolkit); + if (isset($image->info) && is_array($image->info)) { + $image->toolkit = $toolkit; + if (image_toolkit_invoke('load', $image)) { + return $image; + } } } return FALSE; @@ -374,7 +380,7 @@ function image_save(stdClass $image, $destination = NULL) { if ($return = image_toolkit_invoke('save', $image, array($destination))) { // Clear the cached file size and refresh the image information. clearstatcache(); - $image->info = image_get_info($destination); + $image->info = image_get_info($destination, $image->toolkit); if (drupal_chmod($destination)) { return $return; diff --git a/modules/simpletest/tests/image.test b/modules/simpletest/tests/image.test index 3eeb602a8..5ebd6bdc2 100644 --- a/modules/simpletest/tests/image.test +++ b/modules/simpletest/tests/image.test @@ -96,7 +96,7 @@ class ImageToolkitUnitTest extends ImageToolkitTestCase { $image = image_load($this->file, $this->toolkit); $this->assertTrue(is_object($image), t('Returned an object.')); $this->assertEqual($this->toolkit, $image->toolkit, t('Image had toolkit set.')); - $this->assertToolkitOperationsCalled(array('load')); + $this->assertToolkitOperationsCalled(array('load', 'get_info')); } /** diff --git a/modules/simpletest/tests/image_test.module b/modules/simpletest/tests/image_test.module index 1428d303c..695485730 100644 --- a/modules/simpletest/tests/image_test.module +++ b/modules/simpletest/tests/image_test.module @@ -58,8 +58,8 @@ function image_test_get_all_calls() { * Store the values passed to a toolkit call. * * @param $op - * One of the image toolkit operations: 'load', 'save', 'settings', 'resize', - * 'rotate', 'crop', 'desaturate'. + * One of the image toolkit operations: 'get_info', 'load', 'save', + * 'settings', 'resize', 'rotate', 'crop', 'desaturate'. * @param $args * Values passed to hook. * @see image_test_get_all_calls() @@ -80,6 +80,14 @@ function image_test_settings() { } /** + * Image toolkit's get_info operation. + */ +function image_test_get_info(stdClass $image) { + _image_test_log_call('get_info', array($image)); + return array(); +} + +/** * Image tookit's load operation. */ function image_test_load(stdClass $image) { diff --git a/modules/system/image.gd.inc b/modules/system/image.gd.inc index 23e34677b..e6c70e76b 100644 --- a/modules/system/image.gd.inc +++ b/modules/system/image.gd.inc @@ -321,5 +321,38 @@ function image_gd_create_tmp(stdClass $image, $width, $height) { } /** + * Get details about an image. + * + * @param $image + * An image object. + * @return + * FALSE, if the file could not be found or is not an image. Otherwise, a + * keyed 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'). + * + * @see image_get_info() + */ +function image_gd_get_info(stdClass $image) { + $details = FALSE; + $data = getimagesize($image->source); + + if (isset($data) && is_array($data)) { + $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png'); + $extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : ''; + $details = array( + 'width' => $data[0], + 'height' => $data[1], + 'extension' => $extension, + 'mime_type' => $data['mime'], + ); + } + + return $details; +} + +/** * @} End of "ingroup image". */ |