You are here

function imagefield_crop_widget_process in Imagefield Crop 7.3

Same name and namespace in other branches
  1. 6 imagefield_crop_widget.inc \imagefield_crop_widget_process()
  2. 7 imagefield_crop.module \imagefield_crop_widget_process()
  3. 7.2 imagefield_crop.module \imagefield_crop_widget_process()

An element #process callback for the imagefield_crop field type.

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

File

./imagefield_crop.module, line 567

Code

function imagefield_crop_widget_process($element, &$form_state, $form) {
  $item = $element['#value'];
  $item['fid'] = $element['fid']['#value'];
  $instance = field_widget_instance($element, $form_state);
  $settings = $instance['settings'];
  $widget_settings = $instance['widget']['settings'];
  $presets = imagefield_crop_presets_load_multiple_by_name(array_keys($widget_settings['presets']));
  $default_preset = reset($presets);
  $element['#theme'] = 'imagefield_crop_widget';
  $element['#description'] = t('Click on the image and drag to mark how the image will be cropped');
  $path = drupal_get_path('module', 'imagefield_crop');
  $element['#attached']['js'][] = "{$path}/Jcrop/js/jquery.Jcrop.js";
  $element['#attached']['js'][] = "{$path}/imagefield_crop.js";
  $element['#attached']['css'][] = "{$path}/Jcrop/css/jquery.Jcrop.css";
  $entity_w = entity_metadata_wrapper($element['#entity_type'], $element['#entity']);
  if ($element['#file']) {
    unset($element['filename']);
    $file_to_crop = $element['#file'];
    $cropbox_class = array(
      'cropbox',
      'jcrop-processed',
    );
    $parent = drupal_array_get_nested_value($form, array_slice($element['#parents'], 0, -1));
    if (isset($item['_weight']) && $parent['#file_upload_delta'] - 1 == $item['_weight'] && ($form_state['submitted'] && is_numeric(strpos($form_state['triggering_element']['#name'], 'upload_button')))) {
      unset($cropbox_class[1]);
    }
    elseif (!isset($item['_weight']) && ($form_state['submitted'] && is_numeric(strpos($form_state['triggering_element']['#name'], 'upload_button')))) {
      unset($cropbox_class[1]);
    }
    $element['cropbox'] = array(
      '#markup' => theme('image', array(
        'path' => $file_to_crop->uri,
        'attributes' => array(
          'class' => $cropbox_class,
          'id' => $element['#id'] . '-cropbox',
        ),
      )),
    );
    $element['recrop'] = array(
      '#type' => 'button',
      '#attributes' => array(
        'class' => array(
          'recrop-btn',
        ),
      ),
      '#value' => t('Recrop'),
    );
    $element['presets']['#theme'] = 'item_list';

    // Get image info to find out dimensions.
    $image_info = image_get_info(drupal_realpath($file_to_crop->uri));
    $element['crop_config'] = array(
      '#tree' => TRUE,
      '#type' => 'container',
    );
    $element['preview'] = array(
      '#type' => 'markup',
      '#file' => $file_to_crop,
      // This is used by the #process function
      '#process' => array(
        'imagefield_crop_widget_preview_process',
      ),
      '#theme' => 'imagefield_crop_preview',
      '#path' => $file_to_crop->uri,
    );
    foreach ($presets as $preset) {
      $class = array();
      $machine_name = strtolower(str_replace(' ', '-', $preset->name));
      if ($preset->data['crop-type'] == 'free') {
        $crop_area_height = $image_info['height'];
        $crop_area_width = $image_info['width'];
        $ratio = 0;

        //$crop_area_width / $crop_area_height;
      }
      elseif ($preset->data['crop-type'] == 'resolution') {
        $min_resolution_height = $resolution_height = $preset->data['resolution']['height'];
        $min_resolution_width = $resolution_width = $preset->data['resolution']['width'];
        $ratio = $resolution_width / $resolution_height;
        $crop_area_height = floor($image_info['width'] / $ratio);
        $crop_area_width = $image_info['width'];
        if ($crop_area_height > $image_info['height']) {
          $crop_area_width = floor($image_info['height'] * $ratio);
          $crop_area_height = $image_info['height'];
        }
        if ($image_info['width'] < $min_resolution_width || $image_info['height'] < $min_resolution_height) {
          $class[] = 'error';
        }
      }
      else {
        $min_resolution_height = $preset->data['min-resolution']['height'];
        $min_resolution_width = $preset->data['min-resolution']['width'];
        $ratio_width = $preset->data['ratio']['width'];
        $ratio_height = $preset->data['ratio']['height'];
        $crop_area_height = floor($image_info['width'] / $ratio_width * $ratio_height);
        $crop_area_width = $image_info['width'];
        if ($crop_area_height > $image_info['height']) {
          $crop_area_height = $image_info['height'];
          $crop_area_width = floor($image_info['height'] / $ratio_height * $ratio_width);
        }
        $ratio = $ratio_width / $ratio_height;
        if ($image_info['width'] < $crop_area_width || $image_info['height'] < $crop_area_height) {
          $class[] = 'error';
        }
      }
      $crop_config = array();
      if (!empty($item['crop_config'][$preset->pid]->data)) {
        $crop_config = (array) $item['crop_config'][$preset->pid]->data;
      }
      elseif (!empty($item['crop_config'][$preset->pid])) {
        $crop_config = imagefield_crop_fill_cropconfig(!empty($item['crop_config'][$preset->pid]) ? $item['crop_config'][$preset->pid] : array(), $crop_area_height, $crop_area_width);
        $crop_config['new'] = TRUE;
      }
      if (empty($crop_config)) {
        $crop_config = imagefield_crop_fill_cropconfig($item, $crop_area_height, $crop_area_width);
      }
      $crop_config = json_encode($crop_config);

      //      if(!empty($element['#default_value']['crop_config'][$preset->pid])){
      //        $element['#default_value']['crop_config'][$preset->pid] = $crop_config;
      //      }
      $element['crop_config'][$preset->pid] = array(
        '#type' => 'hidden',
        '#attributes' => array(
          'class' => array(
            'crop-config',
            $machine_name,
          ),
        ),
        '#default_value' => isset($crop_config) ? $crop_config : 0,
      );
      $class[] = 'preset';
      if (!empty($item['crop_config'][$preset->pid]->data->active)) {
        $class[] = 'active';
      }
      $element['presets']['#items'][] = '<a id="' . $machine_name . '" href="#" class="' . implode(' ', $class) . '">' . $preset->name . '</a>';
      $settings = array(
        $element['#id'] => array(
          $machine_name => array(),
        ),
      );
      $settings[$element['#id']]['hide_preview'] = $widget_settings['hide_preview'];
      $settings[$element['#id']][$machine_name] = array(
        'box' => array(
          'ratio' => isset($ratio) ? $ratio : 0,
          'box_width' => $widget_settings['cropbox']['width'],
          'box_height' => $widget_settings['cropbox']['height'],
        ),
        'minimum' => array(
          'width' => $preset->data['crop-type'] != 'free' ? $min_resolution_width : NULL,
          'height' => $preset->data['crop-type'] != 'free' ? $min_resolution_height : NULL,
        ),
      );
      $element['#attached']['js'][] = array(
        'data' => array(
          'imagefield_crop' => $settings,
        ),
        'type' => 'setting',
        'scope' => 'header',
      );
    }
  }
  return $element;
}