Picture a Day 2009 gallery

"; $html_dual_panel = "

style='opacity:0' class='main_pic' /> "; $html_single_panel = " "; define("MODE_PIC","pic"); define("MODE_GALLERY","gallery"); define("MODE_THUMB","thumb"); define("MODE_THUMBS","thumbs"); define("MODE_SOURCE","source"); define("DEFAULT_COLS",3); define("DEFAULT_ROWS",3); define("DEFAULT_SIZE",700); class PhotoGallery { ############### ## Variables ## ############### var $_rt = './'; // Where the base "root" of our photos are var $lst = array(); // Will hold our list of files var $current_lst = array(); // Will hold our list of neighbors files var $allowed_ext = array(); //list of allowed file extensions var $jpg_quality = 95; var $thumb_coverage = 95; var $thumb_size = 96; var $html; var $current_pic_name = ''; //current picture; var $current_pic_width = ''; var $current_pic_height = ''; ################################################################### ## user definable Variables, which can be defined within the URL ## ################################################################### var $current_pic = ''; //current picture; var $mode = MODE_GALLERY; //MODE_PIC/MODE_GALLERY/MODE_THUMB - script working mode var $cols = DEFAULT_COLS; // How many thumbnails columns there will be var $rows = DEFAULT_ROWS; // How many thumbnails rows there will be across var $size = DEFAULT_SIZE; // main picture size (highest value within width and height) ############### ## Functions ## ############### function PhotoGallery() { global $html_template; $this->html = $html_template; array_push($this->allowed_ext, 'jpg'); array_push($this->allowed_ext, 'jpeg'); //try to retrieve arguments if (array_key_exists('mode', $_GET)) { $mode = $_GET['mode']; if ($mode == MODE_PIC || $mode == MODE_THUMB || $mode == MODE_THUMBS || $mode == MODE_SOURCE) { $this->mode = $mode; } } if (array_key_exists('cols', $_GET) && is_numeric ($_GET['cols'])) { $this->cols = abs(intval($_GET['cols'])); } if (array_key_exists('rows', $_GET) && is_numeric ($_GET['rows'])) { $this->rows = abs(intval($_GET['rows'])); } if (array_key_exists('size', $_GET) && is_numeric ($_GET['size'])) { $this->size = abs(intval($_GET['size'])); } $this->current_pic = $_GET['pic']; //try to retrieve current picture from arguments } function file_ext($file) { $fileExp = explode('.', $file); // make array off the periods return $fileExp[count($fileExp) -1]; // file extension will be last index in array, -1 for 0-based indexes } function get_extra_params() { $extra_params = ""; if ($this->cols != DEFAULT_COLS) $extra_params .= "&cols=$this->cols"; if ($this->rows != DEFAULT_ROWS) $extra_params .= "&rows=$this->rows"; if ($this->size != DEFAULT_SIZE) $extra_params .= "&size=$this->size"; return $extra_params; } function exif_date($file) { $exif = exif_read_data($file); // Date/time $date=""; if (isset($exif['DateTimeOriginal'])) $date=$exif['DateTimeOriginal']; if (empty($date) && isset($exif['DateTime'])) $date=$exif['DateTime']; return $date; } function file_date($file) { $date = $this->exif_date($file); if (!empty($date)) { $date = strtotime($date); } return $date; } function read_directory() { //using the opendir function $dir_handle = @opendir($this->_rt) or die("Unable to open $this->_rt"); while ($file = readdir($dir_handle)) { $ext = $this->file_ext($file); $ext = strtolower($ext); if(in_array($ext, $this->allowed_ext)) { $this->lst[$this->file_date($file)] = $file; } } //closing the directory closedir($dir_handle); //sort by date ksort($this->lst); } function date_to_string($date) { return date("Y/m/d", $date); } function create_thumbs_list() { $idx = 1; $thumbs = ""; $extra_params = $this->get_extra_params(); foreach ($this->current_lst as $date => $file) { $date = $this->date_to_string($date); $thumbs .= "$date thumbnail\n"; if ($this->mode != MODE_THUMBS && $idx%$this->cols == 0) { $thumbs .= "
\n"; } $idx++; } $this->html = str_replace("", $thumbs, $this->html); } function create_navigation() { $extra_params = $this->get_extra_params(); reset($this->lst); $first = $this->lst[key($this->lst)]; end($this->lst); $last = $this->lst[key($this->lst)]; $rand_val = $this->lst[array_rand($this->lst)]; $navi = " "; $this->html = str_replace("", $navi, $this->html); $wh = "
columns:   rows:  
"; $this->html = str_replace("", $wh, $this->html); } function create_page() { global $html_dual_panel; $this->html = str_replace("", $html_dual_panel, $this->html); $main_pic = " src='?pic=$this->current_pic&mode=pic&size=$this->size' width='$this->current_pic_width' height='$this->current_pic_height' alt='$this->current_pic_name' "; $this->html = str_replace("", $main_pic, $this->html); $this->html = str_replace("", $this->current_pic_name, $this->html); $this->create_thumbs_list(); $this->create_navigation(); echo $this->html; } function create_thumbs_page() { global $html_single_panel; $this->html = str_replace("", $html_single_panel, $this->html); $this->create_thumbs_list(); echo $this->html; } function create_pic() { $image = imageCreateFromJPEG($this->current_pic); $width_orig = imageSX($image); $height_orig = imageSY($image); $max_length = max($width_orig, $height_orig); $ratio = $this->size/$max_length; $width_dest = $width_orig*$ratio; $height_dest = $height_orig*$ratio; $img_new = imagecreatetruecolor($width_dest, $height_dest); imagecopyresampled($img_new, $image, 0, 0, 0, 0, $width_dest, $height_dest, $width_orig, $height_orig); ImageDestroy($image); //retrieve resulting output dimensions, as they might be +-1 pixel off from target $width_dest = imageSX($img_new); $height_dest = imageSY($img_new); $new_size = max($width_dest, $height_dest); $ratio = $new_size/$max_length; if (function_exists('imageconvolution') && $ratio != 1) { $sharpness = 5; if ($ratio > 1) { $sharpness = 5; } elseif ($ratio > 0) { $sharpness = 4; } $sharpenMatrix = array( array(-1, -1, -1), array(-1, $sharpness + 9, -1), array(-1, -1, -1) ); $divisor = 1+$sharpness; $offset = 0; imageconvolution($img_new, $sharpenMatrix, $divisor, $offset); } //Tell the browser what kind of file is come in header("Content-Type: image/jpeg"); //Output the newly created image in jpeg format ImageInterlace($img_new, false); ImageJpeg($img_new, '', $this->jpg_quality); ImageDestroy($img_new); } function create_thumb() { $image = imageCreateFromJPEG($this->current_pic); $width_orig = imageSX($image); $height_orig = imageSY($image); $min_length = min($width_orig, $height_orig); $extracted_size = $min_length*$this->thumb_coverage/100; $img_new = imagecreatetruecolor($this->thumb_size, $this->thumb_size); fastimagecopyresampled($img_new, $image, 0, 0, ($width_orig-$extracted_size)/2, ($height_orig-$extracted_size)/2, $this->thumb_size, $this->thumb_size, $extracted_size, $extracted_size); ImageDestroy($image); //Tell the browser what kind of file is come in header("Content-Type: image/jpeg"); //Output the newly created image in jpeg format ImageInterlace($img_new, false); ImageJpeg($img_new, '', $this->jpg_quality); ImageDestroy($img_new); } function current_pic_data() { if (!in_array($this->current_pic, $this->lst)) { $this->current_pic = ''; } if (empty($this->current_pic)) { $this->current_pic = end($this->lst); //default: last picture } $this->current_pic_name = $this->date_to_string($this->file_date($this->current_pic)); list($width_orig, $height_orig) = getimagesize($this->current_pic); $max_length = max($width_orig, $height_orig); $this->current_pic_width = $width_orig*$this->size/$max_length; $this->current_pic_height = $height_orig*$this->size/$max_length; $idx = count($this->lst); end($this->lst); While ($idx > 0) { $idx--; if (current($this->lst) == $this->current_pic) break; prev($this->lst); } $current_index = $idx; $num_pics = $this->rows * $this->cols; $first_index = floor($num_pics/2); $first_index = $current_index - $first_index; $first_index = max(0, $first_index); $this->current_lst = array_slice($this->lst, $first_index, $num_pics, true); } function thumbs_data() { $this->current_lst = $this->lst; } function get_src() { header('Content-Type: text/plain'); $currentFile = $_SERVER["SCRIPT_NAME"]; $parts = Explode('/', $currentFile); $currentFile = $parts[count($parts) - 1]; $source = file_get_contents($currentFile); echo $source; } } $pg = new PhotoGallery(); if ($pg->mode == MODE_GALLERY) { $pg->read_directory(); $pg->current_pic_data(); $pg->create_page(); } else if ($pg->mode == MODE_PIC) { $pg->create_pic(); } else if ($pg->mode == MODE_THUMB) { $pg->create_thumb(); } else if ($pg->mode == MODE_THUMBS) { $pg->read_directory(); $pg->thumbs_data(); $pg->create_thumbs_page(); } else if ($pg->mode == MODE_SOURCE) { $pg->get_src(); } ?>