Created
October 10, 2013 09:26
-
-
Save raelga/6915574 to your computer and use it in GitHub Desktop.
Histogram of an image using PHP, from http://bubble.ro/How_to_create_the_histogram_of_an_image_using_PHP.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$source_file = "test_image.jpg"; | |
// histogram options | |
$maxheight = 300; | |
$barwidth = 2; | |
$im = ImageCreateFromJpeg($source_file); | |
$imgw = imagesx($im); | |
$imgh = imagesy($im); | |
// n = total number or pixels | |
$n = $imgw*$imgh; | |
$histo = array(); | |
for ($i=0; $i<$imgw; $i++) | |
{ | |
for ($j=0; $j<$imgh; $j++) | |
{ | |
// get the rgb value for current pixel | |
$rgb = ImageColorAt($im, $i, $j); | |
// extract each value for r, g, b | |
$r = ($rgb >> 16) & 0xFF; | |
$g = ($rgb >> 8) & 0xFF; | |
$b = $rgb & 0xFF; | |
// get the Value from the RGB value | |
$V = round(($r + $g + $b) / 3); | |
// add the point to the histogram | |
$histo[$V] += $V / $n; | |
} | |
} | |
// find the maximum in the histogram in order to display a normated graph | |
$max = 0; | |
for ($i=0; $i<255; $i++) | |
{ | |
if ($histo[$i] > $max) | |
{ | |
$max = $histo[$i]; | |
} | |
} | |
echo "<div style='width: ".(256*$barwidth)."px; border: 1px solid'>"; | |
for ($i=0; $i<255; $i++) | |
{ | |
$val += $histo[$i]; | |
$h = ( $histo[$i]/$max )*$maxheight; | |
echo "<img src=\"img.gif\" width=\"".$barwidth."\" | |
height=\"".$h."\" border=\"0\">"; | |
} | |
echo "</div>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment