You are here

image_focus.module in Image Focus Crop 7

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

Image Focus Crop module.

File

image_focus.module
View source
<?php

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

/**
 * Implements hook_image_effect_info().
 */
function image_focus_image_effect_info() {
  $effects = array(
    'image_focus_scale_and_crop' => array(
      'label' => 'Focus Scale and Crop',
      'help' => t('The improved version of Image Scale and Crop effect.'),
      'effect callback' => 'image_focus_scale_and_crop_effect',
      'form callback' => 'image_scale_form',
      'summary theme' => 'image_scale_summary',
    ),
  );
  return $effects;
}

/**
 * Image effect callback for image_focus_scald_and_crop.
 */
function image_focus_scale_and_crop_effect(&$image, $data) {
  list($cx, $cy) = image_focus_get_focal_point($image);
  $scale = max($data['width'] / $image->info['width'], $data['height'] / $image->info['height']);
  if (!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 image_crop($image, $x, $y, $data['width'], $data['height']);
}

/**
 * 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_image_effect_info Implements hook_image_effect_info().
image_focus_scale_and_crop_effect Image effect callback for image_focus_scald_and_crop.