You are here

function _file_entity_update_image_field_dimensions in File Entity (fieldable files) 7.2

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

Update the image dimensions stored in any image fields for a file.

Parameters

object $file: A file object that is an image.

See also

http://drupal.org/node/1448124

1 call to _file_entity_update_image_field_dimensions()
file_entity_file_update in ./file_entity.file.inc
Implements hook_file_update().

File

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

Code

function _file_entity_update_image_field_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;
  }

  // Find all image field enabled on the site.
  $image_fields = _file_entity_get_fields_by_type('image');
  foreach ($image_fields as $image_field) {
    $query = new EntityFieldQuery();
    $query
      ->fieldCondition($image_field, 'fid', $file->fid);
    $results = $query
      ->execute();
    foreach ($results as $entity_type => $entities) {
      $entities = entity_load($entity_type, array_keys($entities));
      foreach ($entities as $entity) {
        foreach ($entity->{$image_field} as $langcode => $items) {
          foreach ($items as $delta => $item) {
            if ($item['fid'] == $file->fid) {
              $entity->{$image_field}[$langcode][$delta]['width'] = $file->metadata['width'];
              $entity->{$image_field}[$langcode][$delta]['height'] = $file->metadata['height'];
            }
          }
        }

        // Save the updated field column values.
        _file_entity_entity_fields_update($entity_type, $entity);
      }
    }
  }
}