You are here

function _imagefield_crop_resize in Imagefield Crop 5

Same name and namespace in other branches
  1. 6 imagefield_crop_widget.inc \_imagefield_crop_resize()
  2. 7.3 imagefield_crop.module \_imagefield_crop_resize()
  3. 7 imagefield_crop.module \_imagefield_crop_resize()
  4. 7.2 imagefield_crop.module \_imagefield_crop_resize()
1 call to _imagefield_crop_resize()
_imagefield_crop_widget_prepare_form_values in ./imagefield_crop.module

File

./imagefield_crop.module, line 571

Code

function _imagefield_crop_resize($src, $crop = NULL, $resize = 0, $dst = NULL) {
  if (!is_file($src)) {
    return FALSE;
  }

  /**
   * The drupal image API is not good enough:
   * 1. It enforces file saving between operations
   * 2. In case of lossy compression (jpeg) quality is reduced on every action (http://drupal.org/node/128963)
   *
   * ==> This should be better with Drupal 6, so take note to fix this for the Drupal 6 version of this module
   */
  if (!_imagefield_crop_verify_gd()) {
    return FALSE;
  }
  $info = image_get_info($src);
  if (!$info) {
    return FALSE;
  }

  // if an unscaled version of the image exists, we should use it for cropping
  $unscaled = $src . '.unscaled';
  if (is_file($unscaled)) {
    $unscaled_info = image_get_info($unscaled);
    $scale = array();
    $scale['width'] = $unscaled_info['width'] / $info['width'];
    $scale['height'] = $unscaled_info['height'] / $info['height'];
    if (!empty($crop)) {
      $crop['x'] *= $scale['width'];
      $crop['y'] *= $scale['height'];
      $crop['width'] *= $scale['width'];
      $crop['height'] *= $scale['height'];
    }
    $src = $unscaled;
    $info = $unscaled_info;
  }

  // find the final image dimensions
  list($width, $height) = array(
    $info['width'],
    $info['height'],
  );
  if ($resize) {
    list($width, $height) = explode('x', $resize);
  }
  elseif (!empty($crop)) {
    list($width, $height) = array(
      $crop['width'],
      $crop['height'],
    );
  }

  /* REFACTOR: Don't scale up */
  $im = image_gd_open($src, $info['extension']);
  $res = imageCreateTrueColor($width, $height);
  $background = imageColorAllocate($res, 255, 255, 255);
  imageCopyResampled($res, $im, 0, 0, $src_x = $crop['x'] ? $crop['x'] : 0, $src_y = $crop['y'] ? $crop['y'] : 0, $width, $height, $src_w = $crop['width'] ? $crop['width'] : $info['width'], $src_h = $crop['height'] ? $crop['height'] : $info['height']);
  $ratio_w = $width / $src_w;
  $ratio_h = $height / $src_h;

  /**
   * Fill background around the image if required.
   * Note that if it is not required, the rectangles are outside of the
   * image boundaries anyway.
   * REFACTOR: verify which is better in performance - with conditionals or w/o
   */

  /* bottom */
  imageFilledRectangle($res, 0, $ratio_h * ($info['height'] - $src_y), $width, $height, $background);

  /* right */
  imageFilledRectangle($res, $ratio_w * ($info['width'] - $src_x), 0, $width, $height, $background);

  /* top */
  if ($src_y < 0) {
    imageFilledRectangle($res, 0, 0, $width, $ratio_h * -1 * $src_y, $background);
  }

  /* left */
  if ($src_x < 0) {
    imageFilledRectangle($res, 0, 0, $ratio_w * -1 * $src_x, $height, $background);
  }
  $result = image_gd_close($res, empty($dst) ? $src : $dst, $info['extension']);
  imageDestroy($im);
  imageDestroy($res);
  return $result;
}