You are here

function imagefield_crop_widget_value in Imagefield Crop 7

Same name and namespace in other branches
  1. 6 imagefield_crop_widget.inc \imagefield_crop_widget_value()

value callback

Registered by imagefield_crop_field_widget_form()

1 string reference to 'imagefield_crop_widget_value'
imagefield_crop_field_widget_form in ./imagefield_crop.module
Implements hook_field_widget_form().

File

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

Code

function imagefield_crop_widget_value(&$element, &$input, $form_state) {

  // set $input['fid'] and that will be the value of the element
  if (!empty($input['fid']) && $input['cropinfo']['changed']) {

    // Get crop and scale info.
    $crop = $input['cropinfo'];
    $instance = field_widget_instance($element, $form_state);
    $scale = NULL;
    if ($instance['widget']['settings']['resolution']) {
      list($scale['width'], $scale['height']) = explode('x', $instance['widget']['settings']['resolution']);
    }
    $src = file_load($input['fid']);
    $file_to_crop = _imagefield_crop_file_to_crop($src->fid);

    // Copy the original aside, for future cropping.
    if ($file_to_crop->fid == $src->fid && ($orig_uri = file_unmanaged_copy($src->uri, $src->uri))) {
      $orig = clone $src;
      $orig->fid = 0;
      $orig->uri = $orig_uri;
      $orig->filename = drupal_basename($orig_uri);
      $orig->status = 1;
      $orig = file_save($orig);
      file_usage_add($orig, 'imagefield_crop', 'file', $src->fid);
    }

    // do the crop. @todo check for errors
    // This worked in D6, doesn't work in D7
    // // Save crop data to the database
    // $src->imagefield_crop = array('crop' => $crop);
    // file_save($src);
    if (_imagefield_crop_resize(drupal_realpath($file_to_crop->uri), $crop, $scale, drupal_realpath($src->uri))) {

      // Insert crop info for this image in imagefield_crop_info variable.
      $crop['fid'] = $src->fid;
      unset($crop['changed']);
      imagefield_crop_file_settings_save($crop);

      // Remove cached versions of the cropped image.
      image_path_flush($src->uri);
    }
  }
}