You are here

function imagefield_focus_scale_and_crop_effect in ImageField Focus 7

1 string reference to 'imagefield_focus_scale_and_crop_effect'
imagefield_focus_image_effect_info in ./imagefield_focus.effects.inc
Implementation of hook_image_effect_info().

File

./imagefield_focus.effects.inc, line 93
Written by Henri MEDOT <henri.medot[AT]absyx[DOT]fr> http://www.absyx.fr

Code

function imagefield_focus_scale_and_crop_effect(&$image, $data) {
  $files = file_load_multiple(array(), array(
    'uri' => $image->source,
  ));
  if (count($files)) {
    $file = reset($files);
    $xoffset = 0;
    $yoffset = 0;
    if (($rect = $file->crop_rect) && ($rect = imagefield_focus_parse($rect))) {
      if (!image_crop_effect($image, $rect)) {
        return FALSE;
      }
      $xoffset = $rect['xoffset'];
      $yoffset = $rect['yoffset'];
    }
    if (($rect = $file->focus_rect) && ($rect = imagefield_focus_parse($rect))) {
      $rect['xoffset'] -= $xoffset;
      $rect['yoffset'] -= $yoffset;
      $scale = min(1, $data['width'] / $rect['width'], $data['height'] / $rect['height']);
      $scale_low = min($scale, max($data['width'] / $image->info['width'], $data['height'] / $image->info['height']));
      if ($data['strength'] == 'medium') {
        $scale = sqrt($scale * $scale_low);
      }
      elseif ($data['strength'] == 'low') {
        $scale = $scale_low;
      }
      $width0 = $image->info['width'] * $scale;
      $height0 = $image->info['height'] * $scale;
      if ($scale < 1 && !image_resize_effect($image, array(
        'width' => $width0,
        'height' => $height0,
      ))) {
        return FALSE;
      }
      foreach ($rect as $key => $value) {
        $rect[$key] = $value * $scale;
      }
      $width = min($width0, $data['width']);
      $height = min($height0, $data['height']);
      $xoffset = min($width0 - $width, max(0, $rect['xoffset'] + ($rect['width'] - $width) / 2));
      $yoffset = min($height0 - $height, max(0, $rect['yoffset'] + ($rect['height'] - $height) / 2));
      return image_crop_effect($image, array(
        'width' => $width,
        'height' => $height,
        'anchor' => round($xoffset) . '-' . round($yoffset),
      ));
    }
  }
  if (@$data['fallback'] == 'smartcrop' && module_exists('smartcrop')) {
    return smartcrop_scale_effect($image, $data);
  }
  return image_scale_and_crop_effect($image, $data);
}