You are here

imagecrop_actions.inc in Image javascript crop 6

Same filename and directory in other branches
  1. 5 imagecrop_actions.inc

Imagecache actions implementation.

File

imagecrop_actions.inc
View source
<?php

/**
 * @file
 * Imagecache actions implementation.
 *
 * @param $data values passed on by imagecache
 */
function imagecrop_javascript_form($data) {
  $form['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Width'),
    '#required' => TRUE,
    '#default_value' => $data['width'],
    '#description' => t('Enter a width in pixels or as a percentage. i.e. 500 or 80%.'),
  );
  $form['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Height'),
    '#required' => TRUE,
    '#default_value' => $data['height'],
    '#description' => t('Enter a height in pixels or as a percentage. i.e. 500 or 80%.'),
  );
  $form['xoffset'] = array(
    '#type' => 'textfield',
    '#title' => t('X offset'),
    '#default_value' => $data['xoffset'],
    '#description' => t('Enter an offset in pixels or use a keyword: <em>left</em>, <em>center</em>, or <em>right</em>.'),
  );
  $form['yoffset'] = array(
    '#type' => 'textfield',
    '#title' => t('Y offset'),
    '#default_value' => $data['yoffset'],
    '#description' => t('Enter an offset in pixels or use a keyword: <em>top</em>, <em>center</em>, or <em>bottom</em>.'),
  );
  $form['resizable'] = array(
    '#type' => 'checkbox',
    '#title' => t('Is the toolbox resizable or not?'),
    '#default_value' => $data['resizable'],
    '#description' => t('If the toolbox is resized, the crop values won\'t be respected, so you should add a Scale action after the ImageCrop.'),
  );
  $form['downscaling'] = array(
    '#type' => 'checkbox',
    '#title' => t('Do not allow down scaling'),
    '#default_value' => $data['downscaling'],
    '#description' => t('If checked, you can\'t resize the toolbox smaller than width and height.'),
  );
  $description = t('Enter an aspect ratio to preserve during resizing. This can take one of the following formats:');
  $description .= '<ul><li>' . t('A float (like 0.5 or 2).') . '</li>';
  $description .= '<li>' . t('The string \'KEEP\'. This will constrain the aspect ratio to that of the original image.') . '</li>';
  $description .= '<li>' . t('The string \'CROP\'. This will constrain the aspect ratio to the dimensions set above.') . '</li></ul>';
  $description .= t('Leave blank for no aspect ratio constraints.');
  $form['aspect'] = array(
    '#type' => 'textfield',
    '#title' => t('Aspect ratio'),
    '#default_value' => $data['aspect'],
    '#description' => $description,
    '#element_validate' => array(
      'imagecrop_validate_aspect',
    ),
  );
  $form['disable_if_no_data'] = array(
    '#type' => 'checkbox',
    '#title' => t('Don\'t crop if cropping region wasn\'t set.'),
    '#default_value' => $data['disable_if_no_data'],
  );
  return $form;
}

/**
 * Extra submit on the imagecrop action settings form.
 * Reset already cropped images to new width or height, when asked.
 */
function imagecrop_javascript_submit($form, $form_state) {
  if ($form_state['values']['reset-crops'] === 1) {
    $preset_name = db_result(db_query('SELECT presetname FROM {imagecache_preset} WHERE presetid = %d', $form_state['values']['presetid']));
    if ($preset_name) {
      db_query("UPDATE {imagecrop} SET width = %d, height = %d WHERE presetname = '%s' AND width = %d AND height = %d", $form_state['values']['data']['width'], $form_state['values']['data']['height'], $preset_name, $form_state['values']['old-width'], $form_state['values']['old-height']);
    }
  }
}

/**
 * Validation of the aspect ratio entry
 */
function imagecrop_validate_aspect(&$element, &$form_state) {
  if (!is_numeric($element['#value'])) {
    if (strtolower($element['#value']) != 'keep' && strtolower($element['#value']) != 'crop') {
      if (!empty($element['#value'])) {
        drupal_set_message(t('Aspect ratio must be a number, the string KEEP, CROP, or empty if you don\'t want to fix it. Defaulting to empty.'));
      }
      form_set_value($element, FALSE, $form_state);
    }
    else {
      form_set_value($element, strtoupper($element['#value']), $form_state);
    }
  }
  else {
    form_set_value($element, abs((double) $element['#value']), $form_state);
  }
}

/**
 * Display properties of a single action
 *
 * @param $element passed on by imagecache
 * @return string
 */
function theme_imagecrop_javascript($element) {
  $data = $element['#value'];
  $description = t('width') . ': ' . $data['width'] . ', ' . t('height') . ': ' . $data['height'];
  if (!empty($data['xoffset'])) {
    $description .= ', ' . t('xoffset') . ': ' . $data['xoffset'];
  }
  if (!empty($data['yoffset'])) {
    $description .= ', ' . t('yoffset') . ': ' . $data['yoffset'];
  }
  if ($data['resizable']) {
    $description .= ', ' . t('resizable');
  }
  if ($data['aspect']) {
    $description .= ', aspect ratio: ' . $data['aspect'];
  }
  if ($data['disable_if_no_data']) {
    $description .= ', ' . t("don't crop if region is not set");
  }
  if ($data['downscaling']) {
    $description .= ', ' . t('no downscaling allowed');
  }
  return $description;
}

/**
 * Action callback to perform the crop on an image
 *
 * @param $image current image resource
 * @param $data values associated with this action
 * @return false or true
 */
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;
}

/**
 * Check if a the current requested file is located in the given directory.
 */
function imagecrop_match_requested_file_directory($filepath, $search_directory) {
  $files_directory = variable_get('file_directory_path', 'sites/default/files');
  $image_dir = str_replace($files_directory . '/', '', $filepath);
  $args = explode('/', $image_dir);
  unset($args[count($args) - 1]);
  $image_dir = implode('/', $args);
  return $image_dir == $search_directory;
}

/**
 * Settings form for reüsing a crop selection.
 */
function imagecrop_reuse_form($data) {
  $presets = imagecrop_presets_list();
  if (count($presets) > 0) {
    $form['imagecrop_presetname'] = array(
      '#title' => t('Enabled imagecrop presets'),
      '#type' => 'select',
      '#options' => $presets,
      '#default_value' => (array) $data['imagecrop_presetname'],
    );
  }
  else {
    $form['imagecrop_warning'] = array(
      '#value' => t('No preset is found with the javascript_crop action so far. If you want to take advantage of this action, you will need to create at least one preset with javascript_crop action.'),
    );
  }
  return $form;
}

/**
 * Theme the description of current reüse settings.
 */
function theme_imagecrop_reuse($element) {
  $data = $element['#value'];
  $description = $data['imagecrop_presetname'];
  return $description;
}

/**
 * Action callback: Crop the image based on the image preset.
 *
 * @param $image current image resource
 * @param $data values associated with this action
 * @return false or true
 *
 */
function imagecrop_reuse_image(&$image, $data) {
  if ($preset_name = $data['imagecrop_presetname']) {
    $preset = imagecache_preset_by_name($preset_name);
    foreach ($preset['actions'] as $action) {
      if ($action['action'] == 'imagecrop_javascript') {
        $GLOBALS['imagecrop_preset'] = $preset_name;
        _imagecache_apply_action($action, $image);
        return TRUE;
      }
    }
    return FALSE;
  }
  return FALSE;
}

Functions

Namesort descending Description
imagecrop_javascript_form @file Imagecache actions implementation.
imagecrop_javascript_image Action callback to perform the crop on an image
imagecrop_javascript_submit Extra submit on the imagecrop action settings form. Reset already cropped images to new width or height, when asked.
imagecrop_match_requested_file_directory Check if a the current requested file is located in the given directory.
imagecrop_reuse_form Settings form for reüsing a crop selection.
imagecrop_reuse_image Action callback: Crop the image based on the image preset.
imagecrop_validate_aspect Validation of the aspect ratio entry
theme_imagecrop_javascript Display properties of a single action
theme_imagecrop_reuse Theme the description of current reüse settings.