You are here

function imagefield_crop_field_insert in Imagefield Crop 7.3

Same name and namespace in other branches
  1. 7.2 imagefield_crop.module \imagefield_crop_field_insert()

Implements hook_field_insert().

File

./imagefield_crop.module, line 122

Code

function imagefield_crop_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  file_field_insert($entity_type, $entity, $field, $instance, $langcode, $items);
  $widget_settings = $instance['widget']['settings'];
  $presets = imagefield_crop_presets_load_multiple_by_name(array_keys($widget_settings['presets']));
  $entity_w = entity_metadata_wrapper($entity_type, $entity);
  $key = $entity_w
    ->entityKey('revision') ? $entity_w
    ->entityKey('revision') : $entity_w
    ->entityKey('id');
  foreach ($items as &$item) {
    foreach ($item['crop_config'] as $pid => &$preset) {
      if (!is_object($preset)) {
        $preset = json_decode($preset);
        $keys = array_keys($item['crop_config']);
        $first = reset($keys);
        $item['crop_config'][$first]->active = TRUE;
      }
      $scale = $presets[$pid]->data['crop-type'] == 'resolution' ? $presets[$pid]->data['resolution'] : NULL;
      $src = file_load($item['fid']);
      $orig = imagefield_crop_create_copy($src);
      file_usage_add($orig, 'imagefield_crop', 'file', $src->fid);
      _imagefield_crop_resize(drupal_realpath($orig->uri), $preset, $scale, $orig);
      file_save($src);
      $preset->uri = $orig->uri;
      unset($preset->cropbox_changed);
      $record['data'] = $preset;
      $record['source_fid'] = $item['fid'];
      $record['pid'] = $pid;
      $record['fid'] = $orig->fid;
      $record['vid'] = $entity->{$key};
      drupal_write_record('imagefield_crop_variants', $record);
      $record = array();
    }
  }
}