abstract class GdImageAnalysis in Image Effects 8.3
Same name and namespace in other branches
- 8 src/Component/GdImageAnalysis.php \Drupal\image_effects\Component\GdImageAnalysis
- 8.2 src/Component/GdImageAnalysis.php \Drupal\image_effects\Component\GdImageAnalysis
Image analysis helper methods for GD.
Hierarchy
- class \Drupal\image_effects\Component\GdImageAnalysis
Expanded class hierarchy of GdImageAnalysis
3 files declare their use of GdImageAnalysis
- GdImageAnalysisTest.php in tests/
src/ Unit/ GdImageAnalysisTest.php - GDOperationTrait.php in src/
Plugin/ ImageToolkit/ Operation/ gd/ GDOperationTrait.php - ImageEffectsTestBase.php in tests/
src/ Functional/ ImageEffectsTestBase.php
File
- src/
Component/ GdImageAnalysis.php, line 8
Namespace
Drupal\image_effects\ComponentView source
abstract class GdImageAnalysis {
/**
* Calculates the mean pixel intensity.
*
* @param resource $image
* A GD image resource.
*
* @return float
* The mean pixel intensity value.
*/
public static function mean($image) {
$mean = 0;
$size = imagesx($image) * imagesy($image) * 3;
for ($i = 0; $i < imagesx($image); $i++) {
for ($j = 0; $j < imagesy($image); $j++) {
$rgb = imagecolorat($image, $i, $j);
$r = $rgb >> 16 & 0xff;
$g = $rgb >> 8 & 0xff;
$b = $rgb & 0xff;
$mean += $r / $size;
$mean += $g / $size;
$mean += $b / $size;
}
}
return $mean;
}
/**
* Generates a GD resource calculating the difference of two images.
*
* The images must have the same dimensions.
*
* @param resource $image1
* A GD image resource.
* @param resource $image2
* A GD image resource.
*
* @return resource
* A GD image resource, with the subtracted image, or NULL if the dimensions
* of the two images differ.
*/
public static function difference($image1, $image2) {
if (imagesx($image1) !== imagesx($image2) || imagesy($image1) !== imagesy($image2)) {
return NULL;
}
$difference = imagecreatetruecolor(imagesx($image1), imagesy($image1));
for ($i = 0; $i < imagesx($image1); $i++) {
for ($j = 0; $j < imagesy($image1); $j++) {
$rgb1 = imagecolorat($image1, $i, $j);
$r1 = $rgb1 >> 16 & 0xff;
$g1 = $rgb1 >> 8 & 0xff;
$b1 = $rgb1 & 0xff;
$rgb2 = imagecolorat($image2, $i, $j);
$r2 = $rgb2 >> 16 & 0xff;
$g2 = $rgb2 >> 8 & 0xff;
$b2 = $rgb2 & 0xff;
imagesetpixel($difference, $i, $j, imagecolorallocate($difference, abs($r2 - $r1), abs($g2 - $g1), abs($b2 - $b1)));
}
}
return $difference;
}
/**
* Computes the histogram of an image.
*
* @param resource $img
* A GD image resource.
*
* @return array
* The image histogram as an array.
*/
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;
}
/**
* Computes the entropy of an image, defined as -sum(p.*log2(p)).
*
* @param resource $img
* A GD image resource.
*
* @return float
* The entropy of the image.
*/
public static function entropy($img) {
$histogram = static::histogram($img);
$histogram_size = array_sum($histogram);
$entropy = 0;
foreach ($histogram as $p) {
if ($p == 0) {
continue;
}
$p = $p / $histogram_size;
$entropy += $p * log($p, 2);
}
return $entropy * -1;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GdImageAnalysis:: |
public static | function | Generates a GD resource calculating the difference of two images. | |
GdImageAnalysis:: |
public static | function | Computes the entropy of an image, defined as -sum(p.*log2(p)). | |
GdImageAnalysis:: |
public static | function | Computes the histogram of an image. | |
GdImageAnalysis:: |
public static | function | Calculates the mean pixel intensity. |