#269337 by egfrith and wrunt: Added support for more image types (PDF, TIFF, EPS, etc.).
							parent
							
								
									c4a548f64d
								
							
						
					
					
						commit
						18b7e4254b
					
				| 
						 | 
					@ -101,36 +101,40 @@ function image_toolkit_invoke($method, stdClass $image, array $params = array())
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Get details about an image.
 | 
					 * 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
 | 
					 * @param $filepath
 | 
				
			||||||
 *   String specifying the path of the image file.
 | 
					 *   String specifying the path of the image file.
 | 
				
			||||||
 | 
					 * @param $toolkit
 | 
				
			||||||
 | 
					 *   An optional image toolkit name to override the default.
 | 
				
			||||||
 * @return
 | 
					 * @return
 | 
				
			||||||
 *   FALSE, if the file could not be found or is not an image. Otherwise, a
 | 
					 *   FALSE, if the file could not be found or is not an image. Otherwise, a
 | 
				
			||||||
 *   keyed array containing information about the image:
 | 
					 *   keyed array containing information about the image:
 | 
				
			||||||
 *    'width'     - Width, in pixels.
 | 
					 *   - "width": Width, in pixels.
 | 
				
			||||||
 *    'height'    - Height, in pixels.
 | 
					 *   - "height": Height, in pixels.
 | 
				
			||||||
 *    'extension' - Commonly used file extension for the image.
 | 
					 *   - "extension": Commonly used file extension for the image.
 | 
				
			||||||
 *    'mime_type' - MIME type ('image/jpeg', 'image/gif', 'image/png').
 | 
					 *   - "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png').
 | 
				
			||||||
 *    'file_size' - File size in bytes.
 | 
					 *   - "file_size": File size in bytes.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function image_get_info($filepath) {
 | 
					function image_get_info($filepath, $toolkit = FALSE) {
 | 
				
			||||||
 | 
					  $details = FALSE;
 | 
				
			||||||
  if (!is_file($filepath)) {
 | 
					  if (!is_file($filepath)) {
 | 
				
			||||||
    return FALSE;
 | 
					    return $details;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  $details = FALSE;
 | 
					  if (!$toolkit) {
 | 
				
			||||||
  $data = @getimagesize($filepath);
 | 
					    $toolkit = image_get_toolkit();
 | 
				
			||||||
  $file_size = @filesize($filepath);
 | 
					  }
 | 
				
			||||||
 | 
					  if ($toolkit) {
 | 
				
			||||||
  if (isset($data) && is_array($data)) {
 | 
					    $image = new stdClass();
 | 
				
			||||||
    $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
 | 
					    $image->source = $filepath;
 | 
				
			||||||
    $extension = array_key_exists($data[2], $extensions) ?  $extensions[$data[2]] : '';
 | 
					    $image->toolkit = $toolkit;
 | 
				
			||||||
    $details = array('width'     => $data[0],
 | 
					    $details = image_toolkit_invoke('get_info', $image);
 | 
				
			||||||
                     'height'    => $data[1],
 | 
					    if (isset($details) && is_array($details)) {
 | 
				
			||||||
                     'extension' => $extension,
 | 
					      $details['file_size'] = filesize($filepath);
 | 
				
			||||||
                     'file_size' => $file_size,
 | 
					    }
 | 
				
			||||||
                     'mime_type' => $data['mime']);
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return $details;
 | 
					  return $details;
 | 
				
			||||||
| 
						 | 
					@ -343,10 +347,12 @@ function image_load($file, $toolkit = FALSE) {
 | 
				
			||||||
  if ($toolkit) {
 | 
					  if ($toolkit) {
 | 
				
			||||||
    $image = new stdClass();
 | 
					    $image = new stdClass();
 | 
				
			||||||
    $image->source = $file;
 | 
					    $image->source = $file;
 | 
				
			||||||
    $image->info = image_get_info($file);
 | 
					    $image->info = image_get_info($file, $toolkit);
 | 
				
			||||||
    $image->toolkit = $toolkit;
 | 
					    if (isset($image->info) && is_array($image->info)) {
 | 
				
			||||||
    if (image_toolkit_invoke('load', $image)) {
 | 
					      $image->toolkit = $toolkit;
 | 
				
			||||||
      return $image;
 | 
					      if (image_toolkit_invoke('load', $image)) {
 | 
				
			||||||
 | 
					        return $image;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return FALSE;
 | 
					  return FALSE;
 | 
				
			||||||
| 
						 | 
					@ -374,7 +380,7 @@ function image_save(stdClass $image, $destination = NULL) {
 | 
				
			||||||
  if ($return = image_toolkit_invoke('save', $image, array($destination))) {
 | 
					  if ($return = image_toolkit_invoke('save', $image, array($destination))) {
 | 
				
			||||||
    // Clear the cached file size and refresh the image information.
 | 
					    // Clear the cached file size and refresh the image information.
 | 
				
			||||||
    clearstatcache();
 | 
					    clearstatcache();
 | 
				
			||||||
    $image->info = image_get_info($destination);
 | 
					    $image->info = image_get_info($destination, $image->toolkit);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (drupal_chmod($destination)) {
 | 
					    if (drupal_chmod($destination)) {
 | 
				
			||||||
      return $return;
 | 
					      return $return;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -96,7 +96,7 @@ class ImageToolkitUnitTest extends ImageToolkitTestCase {
 | 
				
			||||||
    $image = image_load($this->file, $this->toolkit);
 | 
					    $image = image_load($this->file, $this->toolkit);
 | 
				
			||||||
    $this->assertTrue(is_object($image), t('Returned an object.'));
 | 
					    $this->assertTrue(is_object($image), t('Returned an object.'));
 | 
				
			||||||
    $this->assertEqual($this->toolkit, $image->toolkit, t('Image had toolkit set.'));
 | 
					    $this->assertEqual($this->toolkit, $image->toolkit, t('Image had toolkit set.'));
 | 
				
			||||||
    $this->assertToolkitOperationsCalled(array('load'));
 | 
					    $this->assertToolkitOperationsCalled(array('load', 'get_info'));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -58,8 +58,8 @@ function image_test_get_all_calls() {
 | 
				
			||||||
 * Store the values passed to a toolkit call.
 | 
					 * Store the values passed to a toolkit call.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @param $op
 | 
					 * @param $op
 | 
				
			||||||
 *   One of the image toolkit operations: 'load', 'save', 'settings', 'resize',
 | 
					 *   One of the image toolkit operations: 'get_info', 'load', 'save',
 | 
				
			||||||
 *   'rotate', 'crop', 'desaturate'.
 | 
					 *   'settings', 'resize', 'rotate', 'crop', 'desaturate'.
 | 
				
			||||||
 * @param $args
 | 
					 * @param $args
 | 
				
			||||||
 *   Values passed to hook.
 | 
					 *   Values passed to hook.
 | 
				
			||||||
 * @see image_test_get_all_calls()
 | 
					 * @see image_test_get_all_calls()
 | 
				
			||||||
| 
						 | 
					@ -79,6 +79,14 @@ function image_test_settings() {
 | 
				
			||||||
  return array();
 | 
					  return array();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 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.
 | 
					 * Image tookit's load operation.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -320,6 +320,39 @@ function image_gd_create_tmp(stdClass $image, $width, $height) {
 | 
				
			||||||
  return $res;
 | 
					  return $res;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 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".
 | 
					 * @} End of "ingroup image".
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue