You are here

imagecrop.module in Image javascript crop 6

Same filename and directory in other branches
  1. 5 imagecrop.module
  2. 7 imagecrop.module

Provides a javascript toolbox through an imagecache action.

@author Zuuperman - http://drupal.org/user/361625 - http://www.menhir.be @version this is the drupal 6.x version

File

imagecrop.module
View source
<?php

/**
 * @file
 * Provides a javascript toolbox through an imagecache action.
 *
 * @author Zuuperman - http://drupal.org/user/361625 - http://www.menhir.be
 * @version this is the drupal 6.x version
 */

/**
 * Implementation of hook_perm().
 */
function imagecrop_perm() {
  return array(
    'crop any image with toolbox',
    'crop images with toolbox',
    'administer imagecrop',
  );
}

/**
 * Implementation of hook_menu().
 */
function imagecrop_menu() {
  $items = array();
  $items['admin/settings/imagecrop'] = array(
    'title' => 'Imagecache javascript crop',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'imagecrop_settings',
    ),
    'access arguments' => array(
      'administer imagecrop',
    ),
    'file' => 'imagecrop.admin.inc',
  );
  $items['imagecrop/showcrop'] = array(
    'page callback' => 'imagecrop_showcrop',
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'crop images with toolbox',
    ),
    'file' => 'imagecrop.admin.inc',
  );
  $items['imagecrop/docrop'] = array(
    'page callback' => 'imagecrop_docrop',
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'crop images with toolbox',
    ),
    'file' => 'imagecrop.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function imagecrop_theme() {
  return array(
    'imagecrop_javascript' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'imagecrop_reuse' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'imagecrop' => array(
      'arguments' => array(
        'url' => NULL,
        'width' => NULL,
        'height' => NULL,
        'resize' => NULL,
      ),
    ),
    'imagecrop_result' => array(
      'arguments' => array(
        'presetname' => NULL,
        'filepath' => NULL,
        'alt' => NULL,
        'attributes' => NULL,
      ),
    ),
    'presettabs' => array(
      'arguments' => array(
        'presets' => array(),
        'fid' => NULL,
        'active_preset' => NULL,
        'module' => NULL,
        'field' => NULL,
        'node_type' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_theme_registry_alter().
 */
function imagecrop_theme_registry_alter(&$theme_registry) {
  array_unshift($theme_registry['page']['theme paths'], drupal_get_path('module', 'imagecrop'));
}

/**
 * Implementation of hook_imagecrop_popups().
 */
function imagecrop_imagecrop_popups() {
  $popups = array();
  if (module_exists('thickbox')) {
    $popups['imagecrop_thickbox'] = t('Thickbox');
  }
  if (module_exists('modalframe')) {
    $popups['imagecrop_modalframe'] = t('Modalframe');
  }
  if (module_exists('colorbox')) {
    $popups['imagecrop_colorbox'] = t('Colorbox');
  }
  if (module_exists('shadowbox')) {
    $popups['imagecrop_shadowbox'] = t('Shadowbox');
  }
  return $popups;
}

/**
 * Implementation of hook_cron().
 * Delete all references in imagecrop table when
 *   a) file doesn't exist anymore.
 *   b) when preset has been deleted.
 *   c) when javascrip_crop action is removed from a preset.
 */
function imagecrop_cron() {

  // get all files which do not exist anymore from the files table
  $result = db_query("SELECT ic.fid, ic.presetname FROM {imagecrop} ic WHERE NOT EXISTS (SELECT fid FROM {files} f WHERE ic.fid = f.fid) AND ic.reference = 'files'");
  while ($row = db_fetch_object($result)) {
    $records[] = array(
      'fid' => $row->fid,
      'presetname' => $row->presetname,
      'reference' => 'files',
    );
  }

  // get all files which do not exist anymore from the node_images table
  if (module_exists('node_images')) {
    $result = db_query("SELECT ic.fid, presetname FROM {imagecrop} ic WHERE NOT EXISTS (SELECT id FROM {node_images} ni WHERE ic.fid = ni.id) AND ic.reference = 'node_images'");
    while ($row = db_fetch_object($result)) {
      $records[] = array(
        'fid' => $row->fid,
        'presetid' => $row->presetname,
        'reference' => 'node_images',
      );
    }
  }

  // Delete the records
  if (!empty($records)) {
    while (list($key, $val) = each($records)) {
      db_query("DELETE FROM {imagecrop} WHERE fid= %d AND presetname = '%s' AND reference = '%s'", $val['fid'], $val['presetname'], $val['reference']);
    }
  }

  /*
   * Deleted all records from deleted presets
   */
  $presets = return_presets();
  if (!isset($presets['tabs'])) {
    return;
  }
  $in = '';
  foreach ($presets['tabs'] as $preset) {
    $in .= "'" . db_escape_string($preset) . "',";
  }
  $in = substr($in, 0, -1);
  $result = db_query("SELECT presetname FROM {imagecrop} WHERE presetname NOT IN (" . $in . ")");
  while ($row = db_fetch_object($result)) {
    db_query("DELETE FROM {imagecrop} WHERE presetname = '%s'", $row->presetname);
  }
}

/**
 * Implementation of filefield.module's hook_file_delete().
 *
 * Remove imagecrop settings + temp files after a file has been deleted.
 */
function imagecrop_file_delete($file) {
  db_query("DELETE FROM {imagecrop} WHERE fid = %d", $file->fid);
  file_delete(imagecache_create_path('_imagecrop_temp', $file->filepath));
}

/**
 * Implementation of hook_imagecache_actions().
 */
function imagecrop_imagecache_actions() {
  $actions = array(
    'imagecrop_javascript' => array(
      'name' => 'Javascript crop',
      'description' => 'Create a crop with a javascript toolbox.',
      'file' => 'imagecrop_actions.inc',
    ),
    'imagecrop_reuse' => array(
      'name' => 'Javascript crop reuse selection',
      'description' => 'Reuse crop selection from another preset.',
      'file' => 'imagecrop_actions.inc',
    ),
  );
  return $actions;
}

/**
 * Implementation of hook_widget_settings_alter().
 */
function imagecrop_widget_settings_alter(&$settings, $op, $widget) {

  // Only support modules that implement hook_insert_widgets().
  $widget_type = isset($widget['widget_type']) ? $widget['widget_type'] : $widget['type'];
  if ($widget_type != 'imagefield_widget' && $widget_type != 'image_fupload_imagefield_widget' && $widget_type != 'linkimagefield_widget') {
    return;
  }

  // Add our new options to the list of settings to be saved.
  if ($op == 'save') {
    $settings = array_merge($settings, imagecrop_widget_settings());
  }

  // Add the additional settings to the form.
  if ($op == 'form') {
    $settings = array_merge($settings, imagecrop_widget_form($widget));
  }
}

/**
 * A list of settings needed by Imagecrop module on widgets.
 */
function imagecrop_widget_settings() {
  return array(
    'imagecrop',
    'imagecrop_presets',
  );
}

/**
 * Configuration form for editing Imagecrop settings for a field instance.
 */
function imagecrop_widget_form($widget) {
  $form['imagecrop'] = array(
    '#type' => 'fieldset',
    '#title' => t('Imagecrop'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('These options allow the user to alter JavaScript crops for specific ImageCache presets.'),
    '#weight' => 15,
  );
  $presets = imagecrop_presets_list();
  if (count($presets) > 0) {
    $form['imagecrop']['imagecrop'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable JavaScript crop'),
      '#default_value' => (bool) $widget['imagecrop'],
      '#description' => t('Enable JavaScript image crop tool for this widget.'),
    );
    $form['imagecrop']['imagecrop_presets'] = array(
      '#title' => t('Enabled imagecrop presets'),
      '#type' => 'checkboxes',
      '#options' => $presets,
      '#default_value' => (array) $widget['imagecrop_presets'],
      '#description' => t('Select which Imagecache presets should be available for cropping. If no presets are selected, the option to crop the image is not displayed.'),
    );
  }
  else {
    $form['imagecrop']['imagecrop_warning'] = array(
      '#value' => 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.'),
    );
  }
  return $form;
}

/**
 * Get a list of styles suitable for an #options array.
 */
function imagecrop_presets_list() {
  $presets = array();
  foreach (imagecache_presets() as $preset) {
    foreach ($preset['actions'] as $action) {
      if ($action['action'] == 'imagecrop_javascript') {
        $presets[$preset['presetname']] = $preset['presetname'];
      }
    }
  }
  return $presets;
}

/**
 * Implementation of hook_elements().
 */
function imagecrop_elements() {

  // Add our function to the form element declared by imagefield.
  $elements = array();
  $elements['imagefield_widget'] = array(
    '#after_build' => array(
      'imagecrop_process_cck_field',
    ),
  );
  $elements['image_fupload_imagefield_widget'] = $elements['imagefield_widget'];
  $elements['linkimagefield_widget'] = $elements['imagefield_widget'];
  return $elements;
}

/**
 * Process function for imagecrop-enabled cck fields.
 */
function imagecrop_process_cck_field($element) {
  $field = content_fields($element['#field_name'], $element['#type_name']);

  // Bail out if user does not have permission to crop images.
  if (!user_access('crop images with toolbox')) {
    return $element;
  }

  // Bail out of imagecrop is not enabled on this field.
  if (!$field['widget']['imagecrop']) {
    return $element;
  }
  $imagecache_presets = array_filter((array) $field['widget']['imagecrop_presets']);
  if (empty($imagecache_presets)) {
    return $element;
  }
  $element['imagecrop'] = array(
    '#type' => 'markup',
    '#widget' => $field['widget'],
    '#weight' => 10,
    '#suffix' => '</div>',
  );
  if ($element['fid']['#value']) {
    $element['imagecrop']['#prefix'] = '<div class="imagecrop form-item container-inline">';
    $element['imagecrop']['#value'] = imagecrop_linkitem($element['fid']['#value'], 'imagefield', $element);
  }
  else {
    $element['imagecrop']['#prefix'] = '<div class="description">';
    $element['imagecrop']['#value'] = t('After uploading an image you\'ll be able to crop it.');
  }
  return $element;
}

/**
 * Implementation of hook_form_alter().
 * Hook into several existing image modules/fields.
 */
function imagecrop_form_alter(&$form, $form_state, $form_id) {

  // No need to do checks on forms, other then node form.
  if ($form['#id'] != 'node-form' && $form_id != '_node_images_edit_page' && $form_id != 'user_profile_form' && $form_id != 'taxonomy_form_term') {
    return;
  }

  // Only show cropping link when editing a node.
  if ($form['#id'] == 'node-form' && $form['nid']['#value'] == NULL) {
    return;
  }
  $access = user_access('crop images with toolbox');
  if (!$access) {
    return;
  }

  // get array of available modules
  $hooks = variable_get('imagecrop_modules', array());

  // hook into image module
  if (isset($form['images']['thumbnail']) && module_exists('image') && in_array('image', $hooks)) {

    // do we have presets with javascript_crop ?
    if (count(imagecrop_presets_list()) == 0) {
      return;
    }

    // it's anonying we have to make a database call to get the right fid.
    $fid = db_result(db_query("SELECT i.fid FROM {image} i LEFT JOIN {files} f on f.fid = i.fid WHERE i.nid=%d AND filepath='%s' AND filename = '_original'", $form['nid']['#value'], $form['images']['_original']['#value']));
    $form['croptab'] = array(
      '#type' => 'item',
      '#value' => imagecrop_linkitem($fid),
      '#weight' => -10,
    );
  }
  elseif ($form_id == '_node_images_edit_page' && module_exists('node_images')) {

    // do we have presets with javascript_crop ?
    if (count(return_presets()) == 0) {
      return;
    }
    $type = $form['#parameters'][2]->type;
    if (variable_get('node_images_position_' . $type, 'hide') != 'hide' && in_array('node_images_position_' . $type, $hooks)) {
      $form['node_images']['wrapper']['node_images']['imagecrop'] = array(
        '#type' => 'hidden',
        '#value' => '1',
      );
    }
  }
  elseif ($form_id == 'user_profile_form' && isset($form['picture']['current_picture']) && in_array('profile_picture', $hooks)) {
    $form['picture']['current_picture']['#value'] .= imagecrop_linkitem($form['_account']['#value']->uid, 'user');
  }
  elseif ($form_id == 'taxonomy_form_term' && isset($form['taxonomy_image']['current_image']['image']['#value']) && in_array('taxonomy_image', $hooks)) {
    $form['taxonomy_image']['current_image']['taxonomy_image_current_image_delete']['#prefix'] .= imagecrop_linkitem($form['tid']['#value'], 'taxonomy_image');
  }
}

/**
 * Implementation of hook_form_{form_id}_alter().
 */
function imagecrop_form_imagecache_ui_action_form_alter($form, $form_state) {
  if ($form['action']['#value'] == 'imagecrop_javascript') {
    $form['reset-crops'] = array(
      '#type' => 'checkbox',
      '#title' => t('Reset the already cropped images to the new width and height'),
      '#description' => t('All crop selections that have the same width / height as old settings, will be updated to the new width and height.'),
      '#weight' => 0,
    );
    $form['old-height'] = array(
      '#type' => 'hidden',
      '#value' => $form['data']['height']['#default_value'],
    );
    $form['old-width'] = array(
      '#type' => 'hidden',
      '#value' => $form['data']['width']['#default_value'],
    );
    $form['#submit'][] = 'imagecrop_javascript_submit';
  }
}

/**
 * Helper function to add click link
 *
 * @return $form form markup
 */
function imagecrop_linkitem($fid, $module = '', $element = '') {
  $popup_link_function = variable_get('imagecrop_popup', 'basic');
  $width = variable_get('imagecrop_popup_width', 700);
  $height = variable_get('imagecrop_popup_height', 600);
  $module = $module ? '/0/' . $module : '';
  $field = $element['#field_name'] ? '/' . $element['#field_name'] : '';
  $node_type = $element['#type_name'] ? '/' . $element['#type_name'] : '';
  $url = url('imagecrop/showcrop/' . $fid . $module . $field . $node_type, array(
    'absolute' => TRUE,
  ));
  if ($popup_link_function != 'basic' && function_exists($popup_link_function) && ($link = $popup_link_function($url, $width, $height))) {
    return $link;
  }
  else {
    return '[<a href="javascript:;" onclick="window.open(\'' . $url . '\',\'imagecrop\',\'menubar=0,scrollbars=1,resizable=1,width=' . $width . ',height=' . $height . '\');">' . t('Crop this image') . '</a>]';
  }
}

/**
 * Helper function to determine which preset exists and which to load
 *
 * @param $presetname Name of active preset.
 * @return $presets array with presetid to load and list of all other possible presets
 */
function return_presets($presetname = '', $module = '', $field = '', $node_type = '') {
  $filter = array();

  // get possible presets for current imagefield
  if ($module == 'imagefield' && $field) {
    $element = content_fields($field, $node_type);
    if ($element['widget']['imagecrop']) {
      $filter = $element['widget']['imagecrop_presets'];
    }
  }
  $all_presets = imagecache_presets();
  $presets = array();
  foreach ($all_presets as $preset) {
    foreach ($preset['actions'] as $action) {
      if ($action['action'] == 'imagecrop_javascript') {
        if (!count($filter) || !empty($filter[$preset['presetname']])) {
          $presets['tabs'][] = $preset['presetname'];
          if ($presetname == $preset['presetname']) {
            $presets['active_preset'] = $preset['presetname'];
          }
        }
      }
    }
  }
  if (!isset($presets['active_preset']) && count($presets) > 0) {
    $presets['active_preset'] = $presets['tabs'][0];
  }
  return $presets;
}

/**
 * Render the imagecrop links for thickbox.
 */
function imagecrop_thickbox($url, $width, $height) {
  if (!module_exists('thickbox')) {
    return FALSE;
  }
  return '[<a class="thickbox" href="' . $url . '?KeepThis=true&TB_iframe=true&height=' . $height . '&width=' . $width . '">' . t('Crop this image') . '</a>]';
}

/**
 * Render the imagecrop links for colorbox.
 */
function imagecrop_colorbox($url, $width, $height) {
  if (!module_exists('colorbox')) {
    return FALSE;
  }
  return '[<a class="colorbox-load" href="' . $url . '?iframe=true&height=' . $height . '&width=' . $width . '">' . t('Crop this image') . '</a>]';
}

/**
 * Render the imagecrop links for modalframe.
 */
function imagecrop_modalframe($url, $width, $height) {
  if (!module_exists('modalframe')) {
    return FALSE;
  }

  // Send the Modal Frame javascript for parent windows to the page.
  modalframe_parent_js();
  $modalFrame = "Drupal.modalFrame.open({url:'{$url}',width:'{$width}',height:'{$height}'});";
  return '[<a href="javascript:;" onclick="' . $modalFrame . '">' . t('Crop this image') . '</a>]';
}

/**
 * Render the imagecrop links for shadowbox.
 */
function imagecrop_shadowbox($url, $width, $height) {
  if (!module_exists('shadowbox')) {
    return FALSE;
  }
  return '[<a rel="shadowbox;height=' . $height . ';width=' . $width . ';player=iframe" href="' . $url . '">' . t('Crop this image') . '</a>]';
}

/**
 * Helper function to create image
 *
 * @param $fid file id in files table
 * @param $presetname Name of preset beïng cropped
 * @param $cutoff delete the javascript crop action when user wants to define the offsets
 * @return $file with file, javascript crop and preset properties
 */
function create_image_object($fid, $presetname, $module = '', $cutoff = FALSE) {
  $file = _imagecrop_file_load($fid, $module);
  if ($file != FALSE) {
    $preset = imagecache_preset_by_name($presetname);
    if ($cutoff == FALSE) {

      // get the actions from the preset and and throw out the javascript_crop action
      // and every other action which comes after it.
      $break = FALSE;
      while (list($key, $val) = each($preset['actions'])) {
        if ($val['action'] == 'imagecrop_javascript') {
          $crop_width = $preset['actions'][$key]['data']['width'];
          $crop_height = $preset['actions'][$key]['data']['height'];
          $resizable = $preset['actions'][$key]['data']['resizable'];
          $initial_xoffset = $preset['actions'][$key]['data']['xoffset'];
          $initial_yoffset = $preset['actions'][$key]['data']['yoffset'];
          $aspect = $preset['actions'][$key]['data']['aspect'];
          $downscaling = $preset['actions'][$key]['data']['downscaling'];
          $break = TRUE;
        }
        if ($break == TRUE) {
          unset($preset['actions'][$key]);
        }
      }
      $file->initial_xoffset = $initial_xoffset;
      $file->initial_yoffset = $initial_yoffset;

      // see if we have stored values already for this file
      //      $file->xoffset = 0;
      //      $file->yoffset = 0;
      // start with original image size for upcoming actions.
      $src = $file->filepath;
      $download_method = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC);
      $dst = imagecache_create_path($cutoff || $download_method == FILE_DOWNLOADS_PRIVATE ? $preset['presetname'] : '_imagecrop_temp', $file->filepath);
      file_delete($dst);
      imagecache_build_derivative($preset['actions'], $src, $dst);
      $orig = getimagesize($dst);
      $file->orig_width = $orig[0];
      $file->orig_height = $orig[1];
      if (!empty($initial_xoffset) && is_numeric($file->initial_xoffset)) {

        //offset is not empty and is numeric so use #
        $file->xoffset = $file->initial_xoffset;
      }
      else {
        if (!is_numeric($file->initial_xoffset)) {
          switch ($initial_xoffset) {
            case "center":
              if (strpos($crop_width, '%')) {
                $crop_width_corrected = (int) (str_replace("%", "", $crop_width) / 100 * $file->orig_width);
                $file->xoffset = (int) ($file->orig_width / 2 - $crop_width_corrected / 2);
              }
              else {
                $file->xoffset = (int) ($file->orig_width / 2 - $crop_width / 2);
              }
              break;
            case "left":
              $file->xoffset = 0;
              break;
            case "right":
              if (strpos($crop_width, '%')) {
                $crop_width_corrected = (int) (str_replace("%", "", $crop_width) / 100 * $file->orig_width);
                $file->xoffset = (int) $file->orig_width - $crop_width_corrected;
              }
              else {
                $file->xoffset = (int) $file->orig_width - $crop_width;
              }
              break;
          }
        }
        else {
          $file->xoffset = 0;
        }
      }
      if (!empty($initial_yoffset) && is_numeric($file->initial_yoffset)) {

        //offset is not empty and is numeric so use #
        $file->yoffset = $file->initial_yoffset;
      }
      else {
        if (!is_numeric($file->initial_yoffset)) {
          switch ($initial_yoffset) {
            case "center":
              if (strpos($crop_height, '%')) {
                $crop_height_corrected = (int) (str_replace("%", "", $crop_height) / 100 * $file->orig_height);
                $file->yoffset = (int) ($file->orig_height / 2 - $crop_height_corrected / 2);
              }
              else {
                $file->yoffset = (int) ($file->orig_height / 2 - $crop_height / 2);
              }
              break;
            case "top":
              $file->yoffset = 0;
              break;
            case "bottom":
              if (strpos($crop_height, '%')) {
                $crop_height_corrected = (int) (str_replace("%", "", $crop_height) / 100 * $file->orig_height);
                $file->yoffset = (int) $file->orig_height - $crop_height_corrected;
              }
              else {
                $file->yoffset = (int) $file->orig_height - $crop_height;
              }
              break;
          }
        }
        else {
          $file->yoffset = 0;
        }
      }
      $file->crop_width = $crop_width;
      $file->crop_height = $crop_height;
      if ($downscaling) {
        $file->min_width = $crop_width;
        $file->min_height = $crop_height;
      }
      else {
        $file->min_width = 1;
        $file->min_height = 1;
      }
      $file->preset_width = $crop_width;
      $file->preset_height = $crop_height;
      $reference = $module == 'node_images' || $module == 'user' ? $module : 'files';
      $firstscale = FALSE;
      $row = db_fetch_object(db_query("SELECT xoffset, yoffset, width, height, scale\n            FROM {imagecrop} ic\n         WHERE ic.fid = %d AND ic.presetname = '%s' AND ic.reference = '%s'", $fid, $preset['presetname'], $reference));

      // var_dump($row);
      if (!empty($row)) {
        $file->xoffset = $row->xoffset + $file->initial_xoffset;
        $file->yoffset = $row->yoffset + $file->initial_xoffset;

        // When not resizable, force the imagecache action settings.
        if ($resizable) {
          $file->crop_width = $row->width;
          $file->crop_height = $row->height;
        }
        $file->scale = $row->scale;
        $firstscale = TRUE;
      }

      // resizable or not
      $file->resizable = $resizable;

      /*
            // start with original image size for upcoming actions.
            $src = $file->filepath;
            $download_method = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC);
            $dst = imagecache_create_path(($cutoff || ($download_method == FILE_DOWNLOADS_PRIVATE)) ? $preset['presetname'] : '_imagecrop_temp', $file->filepath);
            file_delete($dst);
            imagecache_build_derivative($preset['actions'], $src, $dst);

            $orig = getimagesize($dst);
            $file->orig_width = $orig[0];
            $file->orig_height = $orig[1];
      */

      // aspect ratio
      if ($aspect == 'CROP') {
        $aspect = $crop_width / $crop_height;
      }
      elseif ($aspect == 'KEEP') {
        $aspect = $file->orig_width / $file->orig_height;
      }
      $file->aspect = $aspect;

      // add scale action if necessary
      if ($row->scale != 'original' && $firstscale == TRUE) {
        $preset['actions'][] = array(
          'action' => 'imagecache_scale',
          'data' => array(
            'width' => $row->scale,
            'height' => '',
            'upscale' => 'false',
          ),
        );
      }
    }
    $file->presetname = $preset['presetname'];
    $file->dst = $dst;
    $file->preset_destination = imagecache_create_path($file->presetname, $file->filepath);

    // create the file to display for the crop,
    // we also set a global presetid variable, so I can use this in
    // the javascript_crop action
    $GLOBALS['imagecrop_preset'] = $preset;

    // if we have a dst, flush the image with all the needed imagecache actions.
    if (!empty($dst)) {
      file_delete($dst);
      imagecache_build_derivative($preset['actions'], $src, $dst);
    }
    return $file;
  }
  else {
    return FALSE;
  }
}

/**
 * Helper function to load a file into an object
 *
 * @param $fid file id
 * @param $module specific module which does not use the files table
 * @return $file with properties of the file or false
 */
function _imagecrop_file_load($fid, $module) {
  global $user;
  if (empty($module) || $module == 'imagefield') {
    $file = db_fetch_object(db_query('SELECT * FROM {files} WHERE fid = %d', $fid));
  }
  elseif ($module == 'node_images') {
    $file = db_fetch_object(db_query('SELECT * FROM {node_images} WHERE id = %d', $fid));
  }
  elseif ($module == 'user') {
    $filepath = db_result(db_query('SELECT picture FROM {users} WHERE uid = %d', $fid));
    if ($filepath) {
      $file = new stdClass();
      $file->uid = $fid;
      $file->filepath = $filepath;
      $file->filemime = file_get_mimetype($filepath);
    }
  }
  elseif ($module == 'taxonomy_image') {
    $filepath = db_result(db_query('SELECT path FROM {term_image} WHERE tid = %d', $fid));
    if ($filepath) {
      $file_directory = variable_get('file_directory_path', 'sites/default/files');
      $file_directory .= '/' . variable_get('taxonomy_image_path', '');
      $file = new stdClass();
      $file->filepath = $file_directory . '/' . $filepath;
      $file->filemime = file_get_mimetype($filepath);
    }
  }
  if ($file) {

    // make sure it's an image. Any other mime extensions possible?
    // return false if it's not the right mime type
    $filemime = array(
      'image/jpeg',
      'image/gif',
      'image/png',
      'image/pjpeg',
    );
    if (!in_array($file->filemime, $filemime)) {
      return FALSE;
    }

    // access denied if current user hasn't enough permissions
    if (!user_access('crop any image with toolbox')) {
      if ($module != 'user' && !user_access('administer nodes') && $user->uid != $file->uid) {
        drupal_access_denied();
        exit;
      }
      elseif ($user->uid != $file->uid) {
        drupal_access_denied();
        exit;
      }
    }

    // all seems ok, return file
    return $file;
  }

  // return false if no file was found.
  return FALSE;
}

/**
 * Add imagecrop css & javascript
 */
function imagecrop_markup($js, $css) {
  $path = drupal_get_path('module', 'imagecrop');
  if ($css) {
    drupal_add_css($path . '/imagecrop.css');
  }
  if ($js) {
    $path = drupal_get_path('module', 'imagecrop');
    drupal_add_js($path . '/jcrop/jquery.Jcrop.js');
    drupal_add_js($path . '/imagecrop_jcrop.js');
    drupal_add_css($path . '/jcrop/jquery.Jcrop.css');
  }
}

/**
 * Theme image crop.
 *
 * @param $url url of image
 * @param $width width of image
 * @param $height height of image
 * @param $resize wether the cropping box is resizeble or not
 * @return $output html of the javascript crop area
 */
function theme_imagecrop($url, $width, $height, $resize = 0) {
  $url = str_replace("'", "\\'", $url);
  $output = '<div class="imagefield-crop-wrapper" id="imagefield-crop-wrapper" style="position:absolute;margin-top: 45px;"><div id="image-crop-container" style="width:' . $width . 'px; height:' . $height . 'px;"><img src="' . $url . '" id="cropbox"/></div></div>';
  return $output;
}

/**
 * Theme cropped image result.
 *
 * @param string $presetname
 * @param string $filepath
 * @param string $alt
 * @param string $attributes
 * @return image
 */
function theme_imagecrop_result($presetname, $filepath, $alt = '', $attributes = NULL) {
  $url = imagecache_create_url($presetname, $filepath, TRUE);
  return '<img src="' . $url . '" alt="' . check_plain($alt) . '" title="' . check_plain($alt) . '" ' . $attributes . ' />';
}

/**
 * Theme preset tabs
 *
 * @param $tabs array of available presets
 * @param fid file id
 * @param $active_preset Current active preset
 * @return $output html of the tabs
 */
function theme_presettabs($presets, $fid, $active_preset, $module = '', $field = '', $node_type = '') {
  $module = $module ? '/' . $module : '';
  $field = $field ? '/' . $field : '';
  $node_type = $node_type ? '/' . $node_type : '';
  $tabs[0] = array(
    'data' => t('Select a preset to crop &raquo;'),
    'class' => 'preset-label',
  );
  foreach ($presets['tabs'] as $key => $preset) {
    $key++;
    $class = '';
    if ($preset == $active_preset) {
      $tabs[$key]['class'] = 'active';
      $class = 'imagecrop_highlight';
    }
    $url = 'imagecrop/showcrop/' . $fid . '/' . $preset . $module . $field . $node_type;
    $tabs[$key] = array(
      'data' => l($preset, $url),
    );
  }
  return '<div id="imagecrop_presettabs">' . theme('item_list', $tabs, NULL, 'ul', array(
    'id' => 'preset-tabs',
  )) . '<br style="clear: both;"/></div>';
}

/**
 * Add extra js for the modalframe.
 */
function imagecrop_modalframe_js() {
  drupal_add_js(drupal_get_path('module', 'imagecrop') . '/imagecrop_modalframe.js');
}

Functions

Namesort descending Description
create_image_object Helper function to create image
imagecrop_colorbox Render the imagecrop links for colorbox.
imagecrop_cron Implementation of hook_cron(). Delete all references in imagecrop table when a) file doesn't exist anymore. b) when preset has been deleted. c) when javascrip_crop action is removed from a preset.
imagecrop_elements Implementation of hook_elements().
imagecrop_file_delete Implementation of filefield.module's hook_file_delete().
imagecrop_form_alter Implementation of hook_form_alter(). Hook into several existing image modules/fields.
imagecrop_form_imagecache_ui_action_form_alter Implementation of hook_form_{form_id}_alter().
imagecrop_imagecache_actions Implementation of hook_imagecache_actions().
imagecrop_imagecrop_popups Implementation of hook_imagecrop_popups().
imagecrop_linkitem Helper function to add click link
imagecrop_markup Add imagecrop css & javascript
imagecrop_menu Implementation of hook_menu().
imagecrop_modalframe Render the imagecrop links for modalframe.
imagecrop_modalframe_js Add extra js for the modalframe.
imagecrop_perm Implementation of hook_perm().
imagecrop_presets_list Get a list of styles suitable for an #options array.
imagecrop_process_cck_field Process function for imagecrop-enabled cck fields.
imagecrop_shadowbox Render the imagecrop links for shadowbox.
imagecrop_theme Implementation of hook_theme().
imagecrop_theme_registry_alter Implementation of hook_theme_registry_alter().
imagecrop_thickbox Render the imagecrop links for thickbox.
imagecrop_widget_form Configuration form for editing Imagecrop settings for a field instance.
imagecrop_widget_settings A list of settings needed by Imagecrop module on widgets.
imagecrop_widget_settings_alter Implementation of hook_widget_settings_alter().
return_presets Helper function to determine which preset exists and which to load
theme_imagecrop Theme image crop.
theme_imagecrop_result Theme cropped image result.
theme_presettabs Theme preset tabs
_imagecrop_file_load Helper function to load a file into an object