You are here

function file_entity_metadata_fetch_image_dimensions in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 7.3 file_entity.file.inc \file_entity_metadata_fetch_image_dimensions()

Fetch the dimensions of an image and store them in the file metadata array.

1 call to file_entity_metadata_fetch_image_dimensions()
file_entity_file_presave in ./file_entity.file.inc
Implements hook_file_presave().

File

./file_entity.file.inc, line 297
File hooks implemented by the File entity module.

Code

function file_entity_metadata_fetch_image_dimensions($file) {

  // Prevent PHP notices when trying to read empty files.
  // @see http://drupal.org/node/681042
  if (!$file->filesize) {
    return;
  }

  // Do not bother proceeding if this file does not have an image mime type.
  if (file_entity_file_get_mimetype_type($file) != 'image') {
    return;
  }

  // We have a non-empty image file.
  $image_info = image_get_info($file->uri);
  if ($image_info) {
    $file->metadata['width'] = $image_info['width'];
    $file->metadata['height'] = $image_info['height'];
  }
  else {

    // Fallback to NULL values.
    $file->metadata['width'] = NULL;
    $file->metadata['height'] = NULL;
  }
}