Skip to content

Instantly share code, notes, and snippets.

@Rudis1261
Created March 14, 2014 08:38
Show Gist options
  • Save Rudis1261/9544131 to your computer and use it in GitHub Desktop.
Save Rudis1261/9544131 to your computer and use it in GitHub Desktop.
Script to determine the perceived luminosity of an image. The darker the image the lower the number returned, the lighter the image the higher the number.
<?php
# Function to determine the average luminance of an image
function image_avg_luminance($filename, $num_samples=10, $section="all")
{
# Variables required
$img = imagecreatefromjpeg($filename);
$width = imagesx($img);
$height = imagesy($img);
$x_step = intval($width/$num_samples);
$y_step = intval($height/$num_samples);
$x_start = 0;
$y_start = 0;
$total_lum = 0;
$sample_no = 1;
$section = strtolower(trim($section));
# We need to be able to section the image out.
switch($section)
{
# We need to be able to section the top of the image alone
case "top":
$height = ceil($height / 2);
break;
# We also need to be able to section the bottom
case "bottom":
$y_start = ceil($height / 2);
break;
# We need to be able to section the top of the image alone
case "left":
$width = ceil($width / 2);
break;
# We also need to be able to section the bottom
case "right":
$y_start = ceil($width / 2);
break;
# If there is not section, or it's all sections we will not alter the width or height
case "all":
default:
break;
}
# Loop through the x axis
for ($x=$x_start; $x<$width; $x+=$x_step)
{
# Loop through the y axis
for ($y=$y_start; $y<$height; $y+=$y_step)
{
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
# Luminance formula
// http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
$lum = ($r+$r+$b+$g+$g+$g)/6;
$total_lum += $lum;
# Debugging
// echo "$sample_no - XY: $x,$y = $r, $g, $b = $lum<br />";
$sample_no++;
}
}
# Determine the average
$avg_lum = ceil($total_lum / $sample_no);
return $avg_lum;
}
?>
<?php
include("image.php");
# Get the path and then open the directory
$path = 'uploads/large/';
$dir = opendir($path);
# Loop through the contents of the directory
while ($dir AND ($file = readdir($dir)) !== false)
{
# Check for the . and .. directives
if (($file == ".") OR ($file == "..")) continue;
echo '<div>
<h1>TOP: ' . image_avg_luminance($path . $file, 10, "top") . ', BTM: ' . image_avg_luminance($path . $file, 10, "bottom") . '</h1>
<img src="' . $path . $file . '" width="350" />
</div>';
//var_dump($file);
//echo "<hr />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment