You are here

public static function GdImageAnalysis::histogram in Image Effects 8.3

Same name and namespace in other branches
  1. 8 src/Component/GdImageAnalysis.php \Drupal\image_effects\Component\GdImageAnalysis::histogram()
  2. 8.2 src/Component/GdImageAnalysis.php \Drupal\image_effects\Component\GdImageAnalysis::histogram()

Computes the histogram of an image.

Parameters

resource $img: A GD image resource.

Return value

array The image histogram as an array.

3 calls to GdImageAnalysis::histogram()
GdImageAnalysis::entropy in src/Component/GdImageAnalysis.php
Computes the entropy of an image, defined as -sum(p.*log2(p)).
GdImageAnalysisTest::testDifference in tests/src/Unit/GdImageAnalysisTest.php
Verify the difference calculation.
GdImageAnalysisTest::testHistogram in tests/src/Unit/GdImageAnalysisTest.php
Verify the histogram calculation with a known image.

File

src/Component/GdImageAnalysis.php, line 87

Class

GdImageAnalysis
Image analysis helper methods for GD.

Namespace

Drupal\image_effects\Component

Code

public static function 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;
}