You are here

public function ImageFocusEntropy::calculateFocalPoint in Image Focus Crop 8

Calculates the focal point.

1 call to ImageFocusEntropy::calculateFocalPoint()
ImageFocusEntropy::getFocalPoint in src/lib/ImageFocusEntropy.php
Get focal point.

File

src/lib/ImageFocusEntropy.php, line 89

Class

ImageFocusEntropy
Class ImageFocusEntropy.

Namespace

Drupal\image_focus\lib

Code

public function calculateFocalPoint() {

  // Split the image into multiple zones and calculate entropy in each one.
  // $d is the size of the zones. This value is chosen emperically.
  // Then calculate the center of mass using entropy.
  $d = (int) max((imagesx($this->image) + imagesy($this->image)) / 50, 10);
  $nx = floor(imagesx($this->image) / $d);
  $ny = floor(imagesy($this->image) / $d);
  $entropies = array_fill(0, $nx * $ny, 0);
  $sum = $wsum_x = $wsum_y = 0;
  for ($i = 0; $i < $nx; $i++) {
    for ($j = 0; $j < $ny; $j++) {
      $entropy = $this
        ->calculateEntropy($i * $d, $j * $d, $d, $d);
      $entropies[$i + $j * $nx] = $entropy;
      $sum += $entropy;
      $wsum_x += $entropy * $i;
      $wsum_y += $entropy * $j;
    }
  }
  $this->cx = $sum ? $wsum_x / $sum * $d : 0;
  $this->cy = $sum ? $wsum_y / $sum * $d : 0;
}