You are here

function imagecrop_javascript_image in Image javascript crop 5

Same name and namespace in other branches
  1. 6 imagecrop_actions.inc \imagecrop_javascript_image()

Callback to perform the crop on an image

Parameters

$image current image resource:

$data values associated with this action:

Return value

false or true

File

./imagecrop_actions.inc, line 63
Imagecache actions implementation.

Code

function imagecrop_javascript_image(&$image, $data) {
  $presetid = '';

  // if a global presetid is been set, it means the image is generated from the imagecrop module
  if (isset($GLOBALS['imagecrop_presetid'])) {
    $presetid = $GLOBALS['imagecrop_presetid'];
  }
  else {
    $args = explode('/', $_GET['q']);
    $key = array_search('imagecache', $args);
    if ($key != FALSE) {
      $key++;
      $presetname = $args[$key];
      $presetid = db_result(db_query("SELECT presetid FROM {imagecache_preset} WHERE presetname = '%s'", $presetname));
    }
  }
  if (!empty($presetid)) {
    $row = db_fetch_object(db_query("SELECT xoffset,yoffset,width,height,scale FROM {imagecrop} ic INNER JOIN {files} f on f.fid = ic.fid WHERE f.filepath = '%s' AND ic.presetid = %d AND reference = 'files'", $image->source, $presetid));

    // support for node images (this sucks in a way, because images are not stored in the files table)
    if (module_exists('node_images')) {
      $directory = variable_get('node_images_path', 'node_images');
      $pos = strpos($image->source, $directory);
      if ($pos !== FALSE) {
        $row = db_fetch_object(db_query("SELECT xoffset,yoffset,width,height,scale FROM {imagecrop} ic INNER JOIN {node_images} ni on ni.id = ic.fid WHERE ni.filepath = '%s' AND ic.presetid = %d AND reference = 'node_images'", $image->source, $presetid));
      }
    }
    $firstscale = FALSE;
    if (!empty($row)) {
      $data['xoffset'] = $row->xoffset;
      $data['yoffset'] = $row->yoffset;
      $data['width'] = $row->width;
      $data['height'] = $row->height;
      $firstscale = TRUE;
    }

    // add scale if necessary
    if ($row->scale != 'original' && $firstscale == TRUE) {
      if (!imageapi_image_scale($image, $row->scale, '', TRUE)) {
        watchdog('imagecrop', t('imagecache_scale_image failed before imagecrop'), WATCHDOG_ERROR);
        return FALSE;
      }
    }
  }
  if (!imageapi_image_crop($image, $data['xoffset'], $data['yoffset'], $data['width'], $data['height'])) {
    watchdog('imagecrop', t('imagecrop_javascript failed. image: %image, data: %data.', array(
      '%path' => $image,
      '%data' => print_r($data, TRUE),
    )), WATCHDOG_ERROR);
    return FALSE;
  }
  return TRUE;
}