You are here

function imagefield_crop_widget_process in Imagefield Crop 6

Same name and namespace in other branches
  1. 7.3 imagefield_crop.module \imagefield_crop_widget_process()
  2. 7 imagefield_crop.module \imagefield_crop_widget_process()
  3. 7.2 imagefield_crop.module \imagefield_crop_widget_process()
1 string reference to 'imagefield_crop_widget_process'
imagefield_crop_elements in ./imagefield_crop.module
Implementation of hook_elements().

File

./imagefield_crop_widget.inc, line 125

Code

function imagefield_crop_widget_process($element, $edit, &$form_state, $form) {
  static $added_js = array();
  $module_path = drupal_get_path('module', 'imagefield_crop');
  drupal_add_js($module_path . '/Jcrop/js/jquery.Jcrop.js');

  // We must define Drupal.behaviors for ahah to work, even if there is no file
  drupal_add_js($module_path . '/imagefield_crop.js');
  drupal_add_css($module_path . '/imagefield_crop.css');
  drupal_add_css($module_path . '/Jcrop/css/jquery.Jcrop.css');
  $file = $element['#value'];
  $field = content_fields($element['#field_name'], $element['#type_name']);

  // check remove buttons...
  $remove_name = $element['#field_name'] . '_' . $element['#delta'] . '_remove_btn';
  if (isset($form_state['clicked_button']) && $form_state['clicked_button']['#name'] == $remove_name) {
    return $element;
  }
  if ($field['widget']['resolution']) {
    list($w, $h) = explode('x', $field['widget']['resolution']);

    // ratio is zero when not enforced
    $ratio = $field['widget']['enforce_ratio'] * $w / $h;
  }
  else {

    // no output resolution requested
    $ratio = 0;
    $w = $h = 0;
  }
  if ($field['widget']['croparea']) {
    list($bwidth, $bheight) = explode('x', $field['widget']['croparea']);
  }
  else {
    $bwidth = $bheight = 0;
  }
  $defaults = array(
    'x' => 0,
    'y' => 0,
    'width' => $w ? $w : 50,
    'height' => $h ? $h : 50,
    'changed' => 0,
  );
  if (!empty($file) && is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath']))) {
    if ($field['widget']['enforce_ratio']) {
      $image_ratio = $width / $height;
      if ($ratio > $image_ratio) {
        $height = $width / $ratio;
      }
      else {
        $width = $height * $ratio;
      }

      // if the enforced ratio is different, force crop
      $defaults['changed'] = $ratio != $image_ratio;
    }
    $defaults['width'] = $width;
    $defaults['height'] = $height;
  }
  $crop_display = $file;
  $crop_display['filepath'] = imagefield_crop_file_admin_crop_display_path($file);
  if (!file_exists($crop_display['filepath'])) {

    // crop_display_path might not exist if we were imagefield_crop is added to existing images
    $crop_display['filepath'] = $file['filepath'];
  }
  $element['data']['cropbox'] = array(
    '#type' => 'markup',
    '#value' => theme('imagefield_crop_cropbox', $crop_display, isset($crop_display['alt']) ? $crop_display['alt'] : '', isset($crop_display['title']) ? $crop_display['title'] : '', NULL, FALSE, $element['#id'] . '-cropbox'),
  );
  $element['data']['crop'] = array(
    '#weight' => -10,
  );
  foreach ($defaults as $name => $default) {
    $element['data']['crop'][$name] = array(
      '#type' => 'hidden',
      '#title' => $name,
      '#attributes' => array(
        'class' => 'edit-image-crop-' . $name,
      ),
      '#value' => !empty($file['data']['crop'][$name]) ? $file['data']['crop'][$name] : $default,
    );
  }

  // show dynamic crop preview
  list($preview, $preview_js) = theme('imagefield_crop_dynamic_preview', $crop_display, $field['widget']['resolution'], $field['widget']['preview_width']);
  if (!isset($added_js[$element['#id']])) {

    // Add settings only once (for ahah).
    $settings = array(
      $element['#id'] => array(
        'box' => array(
          'ratio' => $ratio,
          'box_width' => (int) $bwidth,
          'box_height' => (int) $bheight,
        ),
        'minimum' => array(
          'width' => $field['widget']['enforce_minimum'] ? $w : NULL,
          'height' => $field['widget']['enforce_minimum'] ? $h : NULL,
        ),
        'preview' => $preview_js,
      ),
    );
    drupal_add_js(array(
      'imagefield_crop' => $settings,
    ), 'setting');
    $added_js[$element['#id']] = TRUE;
  }
  $element['preview'] = array(
    '#type' => 'markup',
    // the jQuery.extend is required for ahah("add another item" button) to work
    '#value' => $preview . '<script type="text/javascript">jQuery.extend(Drupal.settings.imagefield_crop, ' . drupal_to_js($settings) . ');</script>',
  );
  return $element;
}