function file_entity_image_dimensions in D7 Media 7
Retrieve the dimensions of an image file and store them in the {image dimensions} table.
Parameters
$file: A file object.
$force: TRUE if the image dimensions should always be loaded from the actual file even if $file->image_dimensions is already set.
Return value
The image dimensions as an array with the 'width' and 'height' properties. The array is also added to $file as its image_dimensions property. If the image dimensions cannot be read, the 'width' and 'height' properties will be NULL. If $file is either empty or not an image file, FALSE is returned.
3 calls to file_entity_image_dimensions()
- file_entity_file_insert in file_entity/
file_entity.module - Implements hook_file_insert().
- file_entity_file_load in file_entity/
file_entity.module - Implements hook_file_load().
- file_entity_file_update in file_entity/
file_entity.module - Implement hook_file_update().
File
- file_entity/
file_entity.module, line 255 - Extends Drupal file entities to be fieldable and viewable.
Code
function file_entity_image_dimensions($file, $force = FALSE) {
// 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 (strpos($file->filemime, 'image/') !== 0) {
return;
}
// Return the existing $file->image_dimensions unless a reload is forced.
if (!$force && isset($file->image_dimensions)) {
return $file->image_dimensions;
}
// We have a non-empty image file.
$image_info = image_get_info($file->uri);
if ($image_info) {
$file->image_dimensions = array(
'width' => $image_info['width'],
'height' => $image_info['height'],
);
db_merge('image_dimensions')
->key(array(
'fid' => $file->fid,
))
->fields(array(
'width' => $file->image_dimensions['width'],
'height' => $file->image_dimensions['height'],
))
->execute();
}
else {
// Fallback to NULL values.
$file->image_dimensions = array(
'width' => NULL,
'height' => NULL,
);
}
}