You are here

public function ImageCrop::loadCropSettings in Image javascript crop 7

Load the crop settings that are available.

File

includes/imagecrop.class.inc, line 342
Imagecrop class to handle the javascript imagecrop.

Class

ImageCrop
@file Imagecrop class to handle the javascript imagecrop.

Code

public function loadCropSettings() {
  $size = getimagesize($this->file->uri);
  $this->imageWidth = $this->originalImageWidth = $size[0];
  $this->imageHeight = $this->originalImageHeight = $size[1];
  $settings = db_select('image_crop_settings')
    ->fields('image_crop_settings')
    ->condition('fid', $this->file->fid)
    ->condition('style_name', $this->imageStyle['name'])
    ->execute()
    ->fetchObject();

  // Load settings
  if ($settings) {
    $this->xoffset = $settings->xoffset;
    $this->yoffset = $settings->yoffset;
    $this->width = $settings->width;
    $this->height = $settings->height;
    $this->scale = $settings->scale;
    $this->rotation = $settings->rotation;
    $this->hasSettings = TRUE;
  }
  else {

    // Check for default scale
    if (variable_get('imagecrop_scale_default', FALSE)) {
      $step = variable_get('imagecrop_scale_step', 50);
      $popup_width = variable_get('imagecrop_popup_width', 700);
      $popup_height = variable_get('imagecrop_popup_height', 600) - 50;

      // If controls are in sidebar, count this also.
      if ($this->extraControls) {
        $popup_width -= 215;
      }
      $scale_width = $this->originalImageWidth;
      $aspect = $this->originalImageWidth / $this->originalImageHeight;
      if ($step > 0) {
        $scale_width -= $step;
        while ($scale_width > $this->width && $scale_width / $aspect > $this->height) {
          $scaled_height = intval($scale_width / $aspect);
          if ($scaled_height < $popup_height && $scale_width < $popup_width) {
            $this->scale = $scale_width;
            break;
          }
          $scale_width -= $step;
        }
      }
    }
  }

  // Scale to requested width
  if ($this->scale != 'original') {
    $aspect = $this->originalImageWidth / $this->originalImageHeight;
    $this->imageWidth = $this->scale;
    $this->imageHeight = intval($this->imageWidth / $aspect);
  }

  // Extra actions after the scaling action.
  if (!$this->hasSettings) {

    // Check if the default width is a %
    if (strpos($this->width, '%')) {
      $procent_width = str_replace('%', '', $this->width);
      $this->width = $this->imageWidth * ($procent_width / 100);
    }

    // Check if the default width is a %
    if (strpos($this->height, '%')) {
      $procent_height = str_replace('%', '', $this->height);
      $this->height = $this->imageHeight * ($procent_height / 100);
    }
  }

  // Keep the aspect ratio from original image
  if ($this->resizeAspectRatio == 'KEEP') {
    $this->resizeAspectRatio = $this->imageWidth / $this->imageHeight;
  }
  elseif ($this->resizeAspectRatio == 'CROP') {
    $this->resizeAspectRatio = $this->startWidth / $this->startHeight;
  }

  // Set correct xoffset when a keyword is entered.
  if (!is_numeric($this->xoffset)) {
    switch ($this->xoffset) {
      case 'right':
        $this->xoffset = $this->imageWidth - $this->width;
        break;
      case 'center':
        $this->xoffset = round($this->imageWidth / 2 - $this->width / 2);
        break;
      case 'left':
      default:
        $this->xoffset = 0;
        break;
    }
  }

  // Set correct yoffset when a keyword is entered.
  if (!is_numeric($this->yoffset)) {
    switch ($this->yoffset) {
      case 'bottom':
        $this->yoffset = $this->imageHeight - $this->height;
        break;
      case 'center':
        $this->yoffset = round($this->imageHeight / 2 - $this->height / 2);
        break;
      case 'top':
      default:
        $this->yoffset = 0;
        break;
    }
  }
}