You are here

function imagecrop_javascript_image in Image javascript crop 6

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

Action 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 154
Imagecache actions implementation.

Code

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

  // if a global presetname is been set, it meens the image is generated from the imagecrop module
  if (isset($GLOBALS['imagecrop_preset'])) {
    $presetname = $GLOBALS['imagecrop_preset'];
  }
  else {
    $args = explode('/', $_GET['q']);
    $key = array_search('imagecache', $args);
    if ($key != FALSE) {
      $key++;
      $presetname = $args[$key];
      $all_presets = imagecache_presets();
      foreach ($all_presets as $preset) {
        if ($preset['presetname'] == $presetname) {
          $presetname = $preset['presetname'];
          break;
        }
      }
    }
  }
  if (!empty($presetname)) {
    $row = FALSE;
    $hooks = variable_get('imagecrop_modules', array());
    if (in_array('profile_picture', $hooks)) {
      $is_profile_picture = imagecrop_match_requested_file_directory($image->source, variable_get('user_picture_path', 'pictures'));
      if ($is_profile_picture) {
        $row = db_fetch_object(db_query("\n          SELECT xoffset, yoffset, width, height, scale\n            FROM {imagecrop} ic\n            INNER JOIN {users} u on u.uid = ic.fid\n          WHERE u.picture = '%s' AND ic.presetname = '%s' AND reference = 'user'", $image->source, $presetname));
      }
    }
    if (!$row && in_array('taxonomy_image', $hooks)) {
      $is_taxonomy_image = imagecrop_match_requested_file_directory($image->source, variable_get('taxonomy_image_path', 'category_pictures'));
      if ($is_taxonomy_image) {
        $file_directory = variable_get('file_directory_path', 'sites/default/files');
        $file_directory .= '/' . variable_get('taxonomy_image_path', '');
        $source = str_replace($file_directory . '/', '', $image->source);
        $row = db_fetch_object(db_query("\n          SELECT xoffset, yoffset, width, height, scale\n            FROM {imagecrop} ic\n            INNER JOIN {term_image} t on t.tid = ic.fid\n          WHERE t.path = '%s' AND ic.presetname = '%s' ", $source, $presetname));
      }
    }

    // support for node images (this sucks in a way, because images are not stored in the files table)
    if (!$row && 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("\n          SELECT xoffset, yoffset, width, height, scale\n            FROM {imagecrop} ic\n            INNER JOIN {node_images} ni on ni.id = ic.fid\n          WHERE ni.filepath = '%s' AND ic.presetname = '%s' AND reference = 'node_images'", $image->source, $presetname));
      }
    }
    if (!$row) {
      $row = db_fetch_object(db_query("\n        SELECT xoffset, yoffset, width, height, scale\n          FROM {imagecrop} ic\n          INNER JOIN {files} f on f.fid = ic.fid\n        WHERE f.filepath = '%s' AND ic.presetname = '%s' AND reference = 'files'", $image->source, $presetname));
    }
    $firstscale = FALSE;

    // fill cropping info from database
    if (!empty($row)) {
      $data['xoffset'] = $row->xoffset;
      $data['yoffset'] = $row->yoffset;
      $data['width'] = $row->width;
      $data['height'] = $row->height;
      $firstscale = TRUE;
    }
    else {

      // If there is no data in DB, use default or exit
      if ($data['disable_if_no_data'] == 1) {
        return TRUE;
      }
    }

    // add scale if necessary
    if ($row->scale != 'original' && $firstscale == TRUE) {
      if (!imageapi_image_scale($image, $row->scale, '', FALSE)) {
        watchdog('imagecrop', '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', 'imagecrop_javascript failed. image: %image, data: %data.', array(
      '%path' => $image,
      '%data' => print_r($data, TRUE),
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  return TRUE;
}