diff --git a/file_entity.file.inc b/file_entity.file.inc index 9921b37..4f3ccca 100644 --- a/file_entity.file.inc +++ b/file_entity.file.inc @@ -109,3 +109,17 @@ function file_entity_file_operation_info() { return $info; } + +/** + * Implements hook_file_load(). + */ +function file_entity_file_load($files) { + // Load cached image dimensions. + $result = db_query('SELECT * FROM {image_dimensions} id WHERE id.fid IN (:fids)', array(':fids' => array_keys($files))); + foreach ($result as $record) { + $files[$record->fid]->image_dimensions = array( + 'width' => $record->width, + 'height' => $record->height, + ); + } +} diff --git a/file_entity.install b/file_entity.install index 8fecc7b..305b80b 100644 --- a/file_entity.install +++ b/file_entity.install @@ -63,7 +63,38 @@ function file_entity_schema() { ), ), ); - + $schema['image_dimensions'] = array( + 'description' => 'Cache images dimensions.', + 'fields' => array( + 'fid' => array( + 'description' => 'File ID.', + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'height' => array( + 'description' => 'The height of the image in pixels.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ), + 'width' => array( + 'description' => 'The width of the image in pixels..', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ), + ), + 'primary key' => array('fid'), + 'foreign keys' => array( + 'file_managed' => array( + 'table' => 'file_managed', + 'columns' => array('fid' => 'fid'), + ), + ), + ); return $schema; } @@ -247,3 +278,42 @@ function file_entity_update_7104() { } } } + +/** + * Create the {image_dimensions} database table. + */ +function file_entity_update_7200() { + $schema['image_dimensions'] = array( + 'description' => 'Cache images dimensions.', + 'fields' => array( + 'fid' => array( + 'description' => 'File ID.', + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'height' => array( + 'description' => 'The height of the image in pixels.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ), + 'width' => array( + 'description' => 'The width of the image in pixels..', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + ), + ), + 'primary key' => array('fid'), + 'foreign keys' => array( + 'file_managed' => array( + 'table' => 'file_managed', + 'columns' => array('fid' => 'fid'), + ), + ), + ); + db_create_table('image_dimensions', $schema['image_dimensions']); +} diff --git a/file_entity.module b/file_entity.module index 979f413..492a871 100644 --- a/file_entity.module +++ b/file_entity.module @@ -531,22 +531,44 @@ function file_entity_file_formatter_file_image_view($file, $display, $langcode) $scheme = file_uri_scheme($file->uri); $local_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL); - if (isset($local_wrappers[$scheme]) && $image = image_load($file->uri)) { + if (isset($local_wrappers[$scheme])) { + if (!isset($file->image_dimensions)) { + // Get and cache image dimensions. + $image_info = image_get_info($file->uri); + if ($image_info) { + $file->image_dimensions = array( + 'width' => $image_info['width'], + 'height' => $image_info['height'], + ); + db_query('INSERT INTO {image_dimensions} (fid, width, height) VALUES (:fid, :width, :height);', array( + ':fid' => $file->fid, + ':width' => $file->image_dimensions['width'], + ':height' => $file->image_dimensions['height'], + )); + } + else { + // Fallback to NULL values. + $file->image_dimensions = array( + 'width' => NULL, + 'height' => NULL, + ); + } + } if (!empty($display['settings']['image_style'])) { $element = array( '#theme' => 'image_style', '#style_name' => $display['settings']['image_style'], '#path' => $file->uri, - '#width' => $image->info['width'], - '#height' => $image->info['height'], + '#width' => $file->image_dimensions['width'], + '#height' => $file->image_dimensions['height'], ); } else { $element = array( '#theme' => 'image', '#path' => $file->uri, - '#width' => $image->info['width'], - '#height' => $image->info['height'], + '#width' => $file->image_dimensions['width'], + '#height' => $file->image_dimensions['height'], ); } return $element;