You are here

function _imagefield_crop_entity_presave in Imagefield Crop 7

Helper function to reset the width and height of the imagefield to those of the cropped image.

1 call to _imagefield_crop_entity_presave()
imagefield_crop_entity_presave in ./imagefield_crop.module
Implements hook_entity_presave().

File

./imagefield_crop.module, line 468
Provide a widget to crop uploaded image.

Code

function _imagefield_crop_entity_presave($entity, $fields) {
  foreach ($fields as $field_name => $field) {
    if ($field['widget']['type'] !== 'imagefield_crop_widget') {
      continue;
    }
    if (empty($entity->{$field_name})) {
      continue;
    }

    // Get the language key for the field, if fields language key is not
    // $entity->language set $lang to LANGUAGE_NONE.
    $lang = LANGUAGE_NONE;
    if (isset($entity->language, $entity->{$field_name}[$entity->language])) {
      $lang = $entity->language;
    }
    elseif (empty($entity->{$field_name}[$lang])) {
      continue;
    }
    foreach ($entity->{$field_name}[$lang] as $delta => $imagefield) {
      $file = file_load($imagefield['fid']);
      $info = image_get_info($file->uri);
      if (is_array($info)) {
        $entity->{$field_name}[$lang][$delta]['width'] = $info['width'];
        $entity->{$field_name}[$lang][$delta]['height'] = $info['height'];
      }
    }
  }
}