Created
March 14, 2014 12:58
-
-
Save Rudis1261/9547197 to your computer and use it in GitHub Desktop.
Luminosity in practice
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 | |
# 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; | |
} | |
# Get the path and then open the directory | |
$path = 'uploads/large/'; | |
$dir = opendir($path); | |
$counter = 0; | |
$max = 28; | |
echo '<html> | |
<head> | |
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> | |
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> | |
</head>'; | |
# Loop through the contents of the directory | |
while ($dir AND ($file = readdir($dir)) !== false) | |
{ | |
# Check for the . and .. directives | |
if (($file == ".") OR ($file == "..")) continue; | |
$topLums = image_avg_luminance($path . $file, 10, "top"); | |
//$bottomLums = image_avg_luminance($path . $file, 10, "bottom"); | |
$colors = ' background: #333; color: white; -webkit-box-shadow: 0px 5px 5px 0px rgba(0, 0, 0, 0.82); | |
-moz-box-shadow: 0px 5px 5px 0px rgba(0, 0, 0, 0.82); | |
box-shadow: 0px 5px 5px 0px rgba(0, 0, 0, 0.82);text-shadow: 1px 3px 4px rgba(0, 0, 0, 1);'; | |
if ($topLums > 100) | |
{ | |
$colors = ' background: #222; color: white; -webkit-box-shadow: 0px 5px 5px 0px rgba(0, 0, 0, 0.82); | |
-moz-box-shadow: 0px 5px 5px 0px rgba(0, 0, 0, 0.82); | |
box-shadow: 0px 5px 5px 0px rgba(0, 0, 0, 0.82);text-shadow: 1px 3px 4px rgba(0, 0, 0, 1);'; | |
} | |
echo '<div style="font-family: tahoma, verdana; width: 350px; padding: 0px; margin: 0px; float: left; overflow: hidden; height: 219px;"> | |
<!--<h1 style="padding: 0px margin: 0px;">TOP: ' . $topLums . ', BTM: ' . $bottomLums . '</h1>--> | |
<div style="padding: 0px; margin: 0px;"> | |
<div class="title" style="position: absolute; padding: 15px; margin:0px; ' . $colors . ' font-size: 28px; width: 320px; font-weight: bold;"> | |
Rudi Strydom <span style="font-size: 70%;">[Photography]</span> | |
</div> | |
<img src="' . $path . $file . '" width="350" /> | |
</div> | |
</div>'; | |
# Count and break out of the loop | |
$counter++; | |
if ($counter == $max) break; | |
} | |
echo '<script> | |
// Fade the titles | |
$(".title").fadeTo(0, 0.7); | |
</script> | |
</html>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment