You are here

function imagefield_crop_widget_process in Imagefield Crop 7.2

Same name and namespace in other branches
  1. 6 imagefield_crop_widget.inc \imagefield_crop_widget_process()
  2. 7.3 imagefield_crop.module \imagefield_crop_widget_process()
  3. 7 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 682
Functionality and Drupal hook implementations for the Imagefield Crop module.

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);
  $context = array(
    'form' => $form,
  );
  drupal_alter('imagefield_crop_instance', $instance, $context);
  $widget_settings = $instance['widget']['settings'];
  $element['#theme'] = 'imagefield_crop_widget';
  $description = !empty($instance['description']) ? $instance['description'] : t('Click on the image and drag to mark how the image will be cropped');
  $element['#description'] = theme('file_upload_help', array(
    'description' => $description,
    'upload_validators' => $element['#upload_validators'],
  ));
  $path = drupal_get_path('module', 'imagefield_crop');
  $element['#attached']['js'][] = "{$path}/Jcrop/js/jquery.Jcrop.js";

  // We must define Drupal.behaviors for ahah to work, even if there is no file.
  $element['#attached']['js'][] = "{$path}/imagefield_crop.js";
  $element['#attached']['css'][] = "{$path}/Jcrop/css/jquery.Jcrop.css";
  if ($element['#file']) {
    $file_to_crop = _imagefield_crop_file_to_crop($element['#file']->fid);
    if (!empty($widget_settings['resolution'])) {
      $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',
        '#markup' => theme('image', array(
          'path' => $element['#file']->uri,
          'getsize' => FALSE,
          'attributes' => array(
            'class' => 'preview-existing',
          ),
        )),
      );
    }
    else {
      unset($element['preview']);
    }
    $element['cropbox'] = array(
      '#markup' => theme('image', array(
        'path' => $file_to_crop->uri,
        'attributes' => array(
          'class' => 'cropbox',
          'id' => $element['#id'] . '-cropbox',
        ),
      )),
    );

    // Get image info to find out dimensions.
    $image_info = image_get_info(drupal_realpath($file_to_crop->uri));

    // If ratio is set, then set default cropping area to this size.
    if ($widget_settings['enforce_ratio']) {
      if (!empty($widget_settings['resolution'])) {

        // If select maximum area property is selected, then try to find biggest possible area on the image.
        if ($widget_settings['select_maximum_area']) {
          list($output_width, $output_height) = explode('x', $widget_settings['resolution']);
          $output_ratio = $output_width / $output_height;
          $crop_area_height = floor($image_info['width'] / $output_ratio);
          $crop_area_width = $image_info['width'];
          if ($crop_area_height > $image_info['height']) {
            $crop_area_width = floor($image_info['height'] * $output_ratio);
            $crop_area_height = $image_info['height'];
          }
        }
        else {
          list($crop_area_width, $crop_area_height) = explode('x', $widget_settings['resolution']);
        }
      }
      elseif (!empty($widget_settings['custom_ratio'])) {
        list($ratio_width, $ratio_height) = explode('x', $widget_settings['custom_ratio']);
        $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);
        }
      }
    }
    else {
      list($crop_area_width, $crop_area_height) = array(
        $image_info['width'],
        $image_info['height'],
      );
    }
    $element['cropbox_x'] = array(
      '#type' => 'hidden',
      '#attributes' => array(
        'class' => array(
          'edit-image-crop-x',
        ),
      ),
      '#default_value' => isset($item['cropbox_x']) ? $item['cropbox_x'] : 0,
    );
    $element['cropbox_y'] = array(
      '#type' => 'hidden',
      '#attributes' => array(
        'class' => array(
          'edit-image-crop-y',
        ),
      ),
      '#default_value' => isset($item['cropbox_y']) ? $item['cropbox_y'] : 0,
    );
    $element['cropbox_height'] = array(
      '#type' => 'hidden',
      '#attributes' => array(
        'class' => array(
          'edit-image-crop-height',
        ),
      ),
      '#default_value' => isset($item['cropbox_height']) ? $item['cropbox_height'] : $crop_area_height,
    );
    $element['cropbox_width'] = array(
      '#type' => 'hidden',
      '#attributes' => array(
        'class' => array(
          'edit-image-crop-width',
        ),
      ),
      '#default_value' => isset($item['cropbox_width']) ? $item['cropbox_width'] : $crop_area_width,
    );
    $element['cropbox_changed'] = array(
      '#type' => 'hidden',
      '#attributes' => array(
        'class' => array(
          'edit-image-crop-changed',
        ),
      ),
      '#default_value' => isset($item['cropbox_changed']) ? $item['cropbox_changed'] : 0,
    );
    list($res_w, $res_h) = !empty($widget_settings['resolution']) ? explode('x', $widget_settings['resolution']) : array(
      0,
      0,
    );
    list($crop_w, $crop_h) = explode('x', $widget_settings['croparea']);
    $ratio = NULL;

    // Enforce ratio is set.
    if ($widget_settings['enforce_ratio']) {

      // If resolution is set, then calculate ratio from output resolution
      // else use values set in custom ratio fields.
      list($custom_ration_w, $custom_ration_h) = !empty($widget_settings['resolution']) ? explode('x', $widget_settings['resolution']) : explode('x', $widget_settings['custom_ratio']);
      $ratio = $custom_ration_w / $custom_ration_h;
    }
    $settings = array(
      $element['#id'] => array(
        'box' => array(
          'ratio' => isset($ratio) ? $ratio : 0,
          'box_width' => $crop_w,
          'box_height' => $crop_h,
        ),
        'minimum' => array(
          'width' => $widget_settings['enforce_minimum'] ? $res_w : NULL,
          'height' => $widget_settings['enforce_minimum'] ? $res_h : NULL,
        ),
      ),
    );
    $element['#attached']['js'][] = array(
      'data' => array(
        'imagefield_crop' => $settings,
      ),
      'type' => 'setting',
      'scope' => 'header',
    );
  }
  return $element;
}