You are here

function _smartcrop_gd_histogram in Smart Crop 7

Same name and namespace in other branches
  1. 6 imageapi_gd.inc \_smartcrop_gd_histogram()

Compute a histogram of an image.

Parameters

resource $img GD image resource.:

Return value

array histogram as an array.

1 call to _smartcrop_gd_histogram()
_smartcrop_gd_entropy in ./image.gd.inc
Compute the entropy of an image, defined as -sum(p.*log2(p)).

File

./image.gd.inc, line 148
libgd implementation of smartcrop action.

Code

function _smartcrop_gd_histogram($img) {
  $histogram = array_fill(0, 768, 0);
  for ($i = 0; $i < imagesx($img); $i++) {
    for ($j = 0; $j < imagesy($img); $j++) {
      $rgb = imagecolorat($img, $i, $j);
      $r = $rgb >> 16 & 0xff;
      $g = $rgb >> 8 & 0xff;
      $b = $rgb & 0xff;
      $histogram[$r]++;
      $histogram[$g + 256]++;
      $histogram[$b + 512]++;
    }
  }
  return $histogram;
}