/** * By doing random sampling from the image, find the most energetic point on the passed in * image * * @param Imagick $image * @return array */ protected static function getHighestEnergyPoint(Imagick $image) { // It's more performant doing random pixel uplook via GD if (($im = imagecreatefromstring($image->getImageBlob())) === false) { throw new Exception('GD failed to create image from string'); } $size = $image->getImageGeometry(); $xcenter = 0; $ycenter = 0; $sum = 0; // Only sample 1/50 of all the pixels in the image $sampleSize = round($size['height'] * $size['width']) / 50; for ($k = 0; $k < $sampleSize; $k++) { $i = mt_rand(0, $size['width'] - 1); $j = mt_rand(0, $size['height'] - 1); $rgb = imagecolorat($im, $i, $j); $r = $rgb >> 16 & 0xff; $g = $rgb >> 8 & 0xff; $b = $rgb & 0xff; $val = Color::rgb2bw($r, $g, $b); $sum += $val; $xcenter += ($i + 1) * $val; $ycenter += ($j + 1) * $val; } if ($sum) { $xcenter /= $sum; $ycenter /= $sum; } $sum = $sum / round($size['height'] * $size['width']); return array('x' => $xcenter, 'y' => $ycenter, 'sum' => $sum); }
/** * Find out the entropy for a color image * * If the source image is in color we need to transform RGB into a grayscale image * so we can calculate the entropy more performant. * * @param Imagick $image * @return float */ protected static function colorEntropy(Imagick $image) { $histogram = $image->getImageHistogram(); $newHistogram = array(); // Translates a color histogram into a bw histogram $colors = count($histogram); for ($idx = 0; $idx < $colors; $idx++) { $colors = $histogram[$idx]->getColor(); $grey = Color::rgb2bw($colors['r'], $colors['g'], $colors['b']); if (isset($newHistogram[$grey])) { $newHistogram[$grey] += $histogram[$idx]->getColorCount(); } else { $newHistogram[$grey] = $histogram[$idx]->getColorCount(); } } return static::getEntropy($newHistogram, static::area($image)); }