GdImageAnalysis.php in Image Effects 8.2
File
src/Component/GdImageAnalysis.php
View source
<?php
namespace Drupal\image_effects\Component;
abstract class GdImageAnalysis {
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;
}
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;
}
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;
}
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;
}
}