You are here

image_focus.module in Image Focus Crop 6

Same filename and directory in other branches
  1. 8 image_focus.module
  2. 7 image_focus.module

Image Focus Crop module.

File

image_focus.module
View source
<?php

/**
 * @file
 * Image Focus Crop module.
 */

/**
 * Implements hook_imagecache_actions().
 */
function image_focus_imagecache_actions() {
  $actions = array(
    'image_focus_scale_and_crop' => array(
      'name' => 'Focus Scale and Crop',
      'description' => t('The improved version of ImageCache Scale and Crop action.'),
    ),
  );
  return $actions;
}

/**
 * Implements hook_theme().
 */
function image_focus_theme() {
  $theme = array(
    'image_focus_scale_and_crop' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
  return $theme;
}

/**
 * Form callback for the image_focus_scald_and_crop action.
 */
function image_focus_scale_and_crop_form($data = array()) {
  module_load_include('inc', 'imagecache', 'imagecache_actions');
  return imagecache_resize_form($data);
}

/**
 * Image callback for the image_focus_scald_and_crop action.
 */
function image_focus_scale_and_crop_image(&$image, $data) {
  list($cx, $cy) = image_focus_get_focal_point($image);
  $scale = max($data['width'] / $image->info['width'], $data['height'] / $image->info['height']);
  if (!imageapi_image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
    return FALSE;
  }
  $x = max(0, min($image->info['width'] - $data['width'], $cx * $scale - $data['width'] / 2));
  $y = max(0, min($image->info['height'] - $data['height'], $cy * $scale - $data['height'] / 2));
  return imageapi_image_crop($image, $x, $y, $data['width'], $data['height']);
}

/**
 * Theme a cropped image.
 */
function theme_image_focus_scale_and_crop($element) {
  $element['#value']['upscale'] = FALSE;
  module_load_include('inc', 'imagecache', 'imagecache_actions');
  return theme_imagecache_scale($element);
}

/**
 * Returns the focal point of the image.
 */
function image_focus_get_focal_point($image) {
  $extension = str_replace('jpg', 'jpeg', $image->info['extension']);
  $function = 'imagecreatefrom' . $extension;
  if (function_exists($function) && ($resource = $function($image->source))) {

    // Try different algorithms.
    if ($image->info['file_size'] <= variable_get('image_focus_face_detection_maxsize', 50 * 1024) && ($result = image_focus_get_focal_point_face_detection($resource))) {
      return $result;
    }
    if ($result = image_focus_get_focal_point_entropy($resource)) {
      return $result;
    }
  }
  return array(
    $image->info['width'] / 2,
    $image->info['height'] / 2,
  );
}

/**
 * Get the focal point using Face Detection.
 *
 * @see http://svay.com/blog/face-detection-in-pure-php-without-opencv/
 */
function image_focus_get_focal_point_face_detection($resource) {
  module_load_include('php', 'image_focus', 'php-facedetection/FaceDetector');
  $detector = new Face_Detector(drupal_get_path('module', 'image_focus') . '/php-facedetection/detection.dat');
  $detector
    ->face_detect($resource);
  if ($face = $detector
    ->getFace()) {
    return array(
      $face['x'] + $face['w'] / 2,
      $face['y'] + $face['w'] / 2,
    );
  }
}

/**
 * Get the focal point using entropy.
 *
 * @see http://www.mathworks.com/help/toolbox/images/ref/entropy.html
 * @see http://drupal.org/project/smartcrop
 */
function image_focus_get_focal_point_entropy($resource) {
  module_load_include('inc', 'image_focus', 'image_focus.entropy');
  $entropy = new ImageFocusEntropy($resource);
  return $entropy
    ->getFocalPoint();
}

/**
 * Get the focal point using edge-maximizing crop.
 *
 * @see
 * http://jueseph.com/2010/06/opticrop-content-aware-cropping-with-php-and-imagemagick/
 *
 * @todo However it does not work as expected.
 */
function image_focus_get_focal_point_edge_maximizing($resource) {
  $w = imagesx($resource);
  $h = imagesy($resource);
  $xcenter = 0;
  $ycenter = 0;
  $sum = 0;
  $n = 100000;
  for ($k = 0; $k < $n; $k++) {
    $i = mt_rand(0, $w - 1);
    $j = mt_rand(0, $h - 1);
    $val = imagecolorat($resource, $i, $j) & 0xff;
    $sum += $val;
    $xcenter += ($i + 1) * $val;
    $ycenter += ($j + 1) * $val;
  }
  $xcenter /= $sum;
  $ycenter /= $sum;
  return array(
    $xcenter,
    $ycenter,
  );
}

Functions

Namesort descending Description
image_focus_get_focal_point Returns the focal point of the image.
image_focus_get_focal_point_edge_maximizing Get the focal point using edge-maximizing crop.
image_focus_get_focal_point_entropy Get the focal point using entropy.
image_focus_get_focal_point_face_detection Get the focal point using Face Detection.
image_focus_imagecache_actions Implements hook_imagecache_actions().
image_focus_scale_and_crop_form Form callback for the image_focus_scald_and_crop action.
image_focus_scale_and_crop_image Image callback for the image_focus_scald_and_crop action.
image_focus_theme Implements hook_theme().
theme_image_focus_scale_and_crop Theme a cropped image.