You are here

function imagecrop_docrop in Image javascript crop 6

Same name and namespace in other branches
  1. 5 imagecrop.module \imagecrop_docrop()

Callback with javascript crop.

Parameters

$fid id of file:

$presetname Name of preset to be cropped:

1 string reference to 'imagecrop_docrop'
imagecrop_menu in ./imagecrop.module
Implementation of hook_menu().

File

./imagecrop.admin.inc, line 164
Administration functions for Imagecrop

Code

function imagecrop_docrop($fid, $presetname, $module = '', $field = '', $node_type = '') {
  $presets = return_presets($presetname, $module, $field, $node_type);

  // no presets found for imagecrop
  if (count($presets) == 0) {
    return '<div id="imagecrop_info" class="imagecrop_error">' . t('No preset is found with the javascript_crop action so far. If you want to take advantage of this module, you will need to create at least one preset with that action.') . '</div>';
  }
  module_invoke_all('imagecrop_settings_update', $fid, $presetname, $module, $field, $node_type);
  $file = create_image_object($fid, $presetname, $module);
  imagecrop_markup(TRUE, TRUE);

  // no image found
  if (!$file) {
    return '<div id="imagecrop_info" class="imagecrop_error">' . t('Image to crop was not found.') . '</div>';
  }
  $size_warning = FALSE;

  // get size of temporary image
  $size = getimagesize($file->dst);
  $width = $size[0];
  $height = $size[1];

  // return warning message if crop toolbox is too big and not resizable.
  if (($width < $file->crop_width || $height < $file->crop_height) && $file->resizable == 0) {
    $size_warning = FALSE;
  }

  // add jquery ui only if jcrop is disabled
  if ($file->resizable) {
    $aspect = FALSE;

    // set aspect ration if needed
    if ($file->aspect) {
      if (is_numeric($file->aspect)) {
        $aspect = $file->aspect;
      }
    }

    // add imagecrop aspect setting
    drupal_add_js(array(
      'imagecrop' => array(
        'resize' => 1,
        'aspectRatio' => $aspect,
        'minWidth' => $file->min_width,
        'minHeight' => $file->min_height,
        'width' => $file->preset_width,
        'height' => $file->preset_height,
      ),
    ), 'setting');
  }
  else {
    drupal_add_js(array(
      'imagecrop' => array(
        'resize' => '0',
      ),
    ), 'setting');
  }

  // output
  if ($size_warning == FALSE) {
    $url = file_create_url($file->dst);
    $url .= strstr($url, '?') ? '&time=' . time() : '?time=' . time();
    $output = theme('presettabs', $presets, $fid, $presetname, $module, $field, $node_type);
    $output .= '<div id="imagecrop_help">' . t("Resize image if needed, then select a crop area. Click 'Crop image thumbnail' to save your selection.") . '</div>';
    $output .= theme('imagecrop', $url, $width, $height, $file->resizable);
    $output .= drupal_get_form('imageoffsets_form', $file, $presetname, $fid, $module, $field, $node_type);
  }
  else {
    $output .= '<div id="imagecrop_info" class="imagecrop_error">' . t('The crop toolbox is too big for this image.') . ' <a href="javascript:history.back();"><span class="white">' . t('Back') . '</span></a></div>';
  }
  return $output;
}