You are here

function imagefield_crop_widget_preview_process in Imagefield Crop 7.3

Same name and namespace in other branches
  1. 7 imagefield_crop.module \imagefield_crop_widget_preview_process()
  2. 7.2 imagefield_crop.module \imagefield_crop_widget_preview_process()

Impletement imagefield_crop_widget_preview_process().

Parameters

$element:

$form_state:

$form:

Return value

mixed

1 string reference to 'imagefield_crop_widget_preview_process'
imagefield_crop_widget_process in ./imagefield_crop.module
An element #process callback for the imagefield_crop field type.

File

./imagefield_crop.module, line 718

Code

function imagefield_crop_widget_preview_process($element, &$form_state, $form) {
  $file = $element['#file'];
  if ($file->fid == 0) {
    return $element;
  }

  // The widget belongs to the parent, so we got to find it first
  $parents = array_slice($element['#array_parents'], 0, -1);
  $parent = drupal_array_get_nested_value($form, $parents);
  $instance = field_widget_instance($parent, $form_state);
  $presets = imagefield_crop_presets_load_multiple_by_name(array_keys($instance['widget']['settings']['presets']));
  $image_info = image_get_info(drupal_realpath($file->uri));
  $crop_config = !empty($parent['#value']['crop_config']) ? $parent['#value']['crop_config'] : array();
  foreach ($presets as $pid => $preset) {
    if ($preset->data['crop-type'] == 'free') {
      $img = $image_info;
      imagefield_crop_presize($img, $instance['widget']['settings']['preview']['width'], $instance['widget']['settings']['preview']['height']);
      $width = $img['width'];
      $height = $img['height'];
    }
    elseif ($preset->data['crop-type'] == 'resolution') {
      $img = $preset->data['resolution'];
      imagefield_crop_presize($img, $instance['widget']['settings']['preview']['width'], $instance['widget']['settings']['preview']['height']);
      $width = $img['width'];
      $height = $img['height'];
    }
    else {
      if ($preset->data['ratio']['width'] > $preset->data['ratio']['height']) {
        $width = $instance['widget']['settings']['preview']['width'];
        $height = $width / $preset->data['ratio']['width'] * $preset->data['ratio']['height'];
      }
      else {
        $height = $instance['widget']['settings']['preview']['width'];
        $width = $height / $preset->data['ratio']['height'] * $preset->data['ratio']['width'];
      }
    }

    //TODO: Ivestigate about preview size
    $machine_name = strtolower(str_replace(' ', '-', $preset->name));
    $settings[$parent['#id']][$machine_name] = array();
    $settings[$parent['#id']][$machine_name] = array(
      'preview' => array(
        'orig_width' => $image_info['width'],
        'orig_height' => $image_info['height'],
        'width' => (int) $width,
        'height' => (int) $height,
      ),
    );
    $element['#imagefield_crop']['#' . $machine_name] = array(
      '#file' => $element['#file'],
      '#width' => $width,
      '#height' => $height,
      '#name' => $machine_name,
      '#crop_config' => !empty($crop_config[$pid]) ? $crop_config[$pid] : FALSE,
    );
  }
  if (!empty($settings)) {
    $element['#attached']['js'][] = array(
      'data' => array(
        'imagefield_crop' => $settings,
      ),
      'type' => 'setting',
      'scope' => 'header',
    );
  }
  return $element;
}