techno_race
04-06-2011, 04:02 AM
I am trying to create a PHP script to increase and decrease the brightnesses of images in a directory such that they all appear similar, then datestamp and save them (the datestamp is in the format of "MM/DD/YY HH:MM:SS AP" at the bottom and "Age: [weeks since March 22, 2011] weeks" at the top, based on a filename format of the images of "YYYY-MM-DDHH-MM-SS.jpg").
In the directory are this PHP script ("normalize.php"), a number of images, and two subdirectories, "brightness," which contains 1x1-pixel grayscale versions (I made these already) of the images as a basis to measure the overall brightness based on the grey level, and "corrected," which should contain the images generated by the script.
$images = array();
$processed = 0;
$brightnesses = array();
$offsets = array();
$dates = array();
$ages = array();
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "brightness" && $file != "corrected" && $file != "normalize.php") {
$images[$processed] = "$file";
}
$processed++;
}
closedir($handle);
}
$processed = 0;
while ($processed < $images.count) {
$image = imagecreatefromjpeg("brightness/".$images[$processed]);
$rgb = imagecolorat($image, 1, 1);
$brightness = $rgb & 0xFF;
$brightnesses[$images[$processed]] = $brightness;
imagedestroy($image);
$processed++;
}
$processed = 0;
$total = 0;
while ($processed < $images.count) {
$total = $total + $brightnesses[$images[$processed]];
$processed++;
}
$processed = 0;
$average = $total/count($brightnesses);
while ($processed < $images.count) {
if ($brightnesses[$images[$processed]] === 0)
$offset = $average;
else
$offset = (100*($average-$brightnesses[$images[$processed]]))/$brightnesses[$images[$processed]];
$offsets[$images[$processed]] = $offset;
$processed++;
}
$processed = 0;
while ($processed < $images.count) {
$image = imagecreatefromjpeg($images[$processed]);
imagefilter($image,IMG_FILTER_BRIGHTNESS,$offsets[$images[$processed]]);
$width = imagesx($image);
$height = imagesy($image);
//YYYY-MM-DDHH-MM-SS.jpg
//0123456789012345678901
//MM/DD/YY HH:MM:SS AP
//Age: WW weeks
$year = substr($images[$processed],2,2);
$month = substr($images[$processed],5,2);
$day = substr($images[$processed],8,2);
$hour = (float) substr($images[$processed],10,2);
if (hour < 12 && hour > 0)
$ampm = 'AM';
else {
$ampm = 'PM';
if (hour === 0)
$hour = 12;
else
$hour = $hour - 12;
}
$minute = substr($images[$processed],13,2);
$second = substr($images[$processed],16,2);
$date = strtotime($year.'-'.$month.'-'.$day);
$born = strtotime('2011-03-22');
$difference = $date-$born;
$age = $difference/36288000000;
if ($age < 10)
$age = ' '.$age;
if (substr($month, 0, 1) === 0)
$month = ' '.substr($month,1,1);
if (substr($day, 0, 1) === 0)
$day = ' '.substr($day,1,1);
if ($hour < 10)
$hour = ' '.$hour;
imagettftext($image,10,0,0,$height,imagecolorallocate($image,255,255,255),"courier.ttf",$month.'/'.$day.'/'.$year.' '.$hour.':'.$minute.':'.$second.' '.$ampm);
imagettftext($image,10,0,0,10,imagecolorallocate($image,255,255,255),"courier.ttf",'Age: '.$age.' weeks');
imagejpeg($image, "corrected/".$images[$processed]);
imagedestroy($image);
$processed++;
}
For some reason, this script doesn't populate the $brightnesses array, thus causing a divide by zero on line 33 and, of course, the script not to function. I don't know if this happens with the other arrays, as the script never ran past the first one.
There must be someone out there who has a clue what I just said. REVEAL YOURSELF...
In the directory are this PHP script ("normalize.php"), a number of images, and two subdirectories, "brightness," which contains 1x1-pixel grayscale versions (I made these already) of the images as a basis to measure the overall brightness based on the grey level, and "corrected," which should contain the images generated by the script.
$images = array();
$processed = 0;
$brightnesses = array();
$offsets = array();
$dates = array();
$ages = array();
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "brightness" && $file != "corrected" && $file != "normalize.php") {
$images[$processed] = "$file";
}
$processed++;
}
closedir($handle);
}
$processed = 0;
while ($processed < $images.count) {
$image = imagecreatefromjpeg("brightness/".$images[$processed]);
$rgb = imagecolorat($image, 1, 1);
$brightness = $rgb & 0xFF;
$brightnesses[$images[$processed]] = $brightness;
imagedestroy($image);
$processed++;
}
$processed = 0;
$total = 0;
while ($processed < $images.count) {
$total = $total + $brightnesses[$images[$processed]];
$processed++;
}
$processed = 0;
$average = $total/count($brightnesses);
while ($processed < $images.count) {
if ($brightnesses[$images[$processed]] === 0)
$offset = $average;
else
$offset = (100*($average-$brightnesses[$images[$processed]]))/$brightnesses[$images[$processed]];
$offsets[$images[$processed]] = $offset;
$processed++;
}
$processed = 0;
while ($processed < $images.count) {
$image = imagecreatefromjpeg($images[$processed]);
imagefilter($image,IMG_FILTER_BRIGHTNESS,$offsets[$images[$processed]]);
$width = imagesx($image);
$height = imagesy($image);
//YYYY-MM-DDHH-MM-SS.jpg
//0123456789012345678901
//MM/DD/YY HH:MM:SS AP
//Age: WW weeks
$year = substr($images[$processed],2,2);
$month = substr($images[$processed],5,2);
$day = substr($images[$processed],8,2);
$hour = (float) substr($images[$processed],10,2);
if (hour < 12 && hour > 0)
$ampm = 'AM';
else {
$ampm = 'PM';
if (hour === 0)
$hour = 12;
else
$hour = $hour - 12;
}
$minute = substr($images[$processed],13,2);
$second = substr($images[$processed],16,2);
$date = strtotime($year.'-'.$month.'-'.$day);
$born = strtotime('2011-03-22');
$difference = $date-$born;
$age = $difference/36288000000;
if ($age < 10)
$age = ' '.$age;
if (substr($month, 0, 1) === 0)
$month = ' '.substr($month,1,1);
if (substr($day, 0, 1) === 0)
$day = ' '.substr($day,1,1);
if ($hour < 10)
$hour = ' '.$hour;
imagettftext($image,10,0,0,$height,imagecolorallocate($image,255,255,255),"courier.ttf",$month.'/'.$day.'/'.$year.' '.$hour.':'.$minute.':'.$second.' '.$ampm);
imagettftext($image,10,0,0,10,imagecolorallocate($image,255,255,255),"courier.ttf",'Age: '.$age.' weeks');
imagejpeg($image, "corrected/".$images[$processed]);
imagedestroy($image);
$processed++;
}
For some reason, this script doesn't populate the $brightnesses array, thus causing a divide by zero on line 33 and, of course, the script not to function. I don't know if this happens with the other arrays, as the script never ran past the first one.
There must be someone out there who has a clue what I just said. REVEAL YOURSELF...