<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

define('CDN_BASE', 'http://archive.gersteinlab.org/static');

$accepted_exts = array('jpg', 'jpeg', 'png', 'gif');

if ( ! is_dir('thumbs')) mkdir('thumbs') or die('can\' create thumbs directory');

$thumbs_list = array();
$file_list = array();

if ($handle = opendir('.')) 
{
	while (($file = readdir($handle)) !== FALSE) 
	{
		if (in_array(strtolower(array_pop(explode('.', $file))), $accepted_exts)) 
		{
			if ( ! file_exists('thumbs/' . $file)) 
			{
				build_thumb($file);
			}
			
			$file_list[] = $file;
		}
	}
	closedir($handle);
}

if ($handle = opendir('thumbs')) 
{
	while (($file = readdir($handle)) !== FALSE) 
	{
		if ( ! file_exists($file)) 
		{
			unlink('thumbs/' . $file);
		}
		
		$parts = pathinfo($file);
		
		if (in_array($parts['extension'], $accepted_exts)) 
		{
			$thumbs_list[] = $file;
		}
	}
	closedir($handle);
}

$count = 0;
$total = count($file_list);

function build_thumb($file)
{
	$save_path = getcwd().'/thumbs/';
	$parts = pathinfo($file);
	
	if ($parts['extension'] == "jpeg" || $parts['extension'] == "jpg") 
	{
		$im = imagecreatefromjpeg($file); 
	} 
	else if ($parts['extension'] == "png") 
	{
		$im = imagecreatefrompng($file);
	}
	else if ($parts['extension'] == "gif") 
	{	
		$im = imagecreatefromgif($file);	
	} 
	else 
	{
		echo "Invalid image format " . $parts['extension'];
		return;
	}
	
	$new_x = imagesx($im) / 5;
	$new_y = imagesy($im) / 5;
	$small = imagecreatetruecolor($new_x, $new_y);
	imagecopyresampled($small, $im, 0, 0, 0, 0, $new_x, $new_y, 
					   imagesx($im), imagesy($im));
	imagejpeg($small, $save_path.$file, 85);
	imagedestroy($im);
	imagedestroy($small);
	usleep(100);
	set_time_limit(90);
	flush();
}
?>


<!doctype html>
<html lang="en" class="no-js">
<head>
	<meta charset="utf-8">

	<!-- www.phpied.com/conditional-comments-block-downloads/ -->
	<!--[if IE]><![endif]-->

	<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame 
		 Remove this if you use the .htaccess -->
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

	<title>Gerstein Lab</title>
	<meta name="description" content="">
	<meta name="author" content="">

	<!--  Mobile Viewport Fix
		  j.mp/mobileviewport & davidbcalhoun.com/2010/viewport-metatag 
	device-width : Occupy full width of the screen in its current orientation
	initial-scale = 1.0 retains dimensions instead of zooming out if page height > device height
	maximum-scale = 1.0 retains dimensions instead of zooming in if page width < device width
	-->
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">

	<!-- Place favicon.ico and apple-touch-icon.png in the root of your domain and delete these references -->
	<link rel="shortcut icon" href="favicon.ico">
	<link rel="apple-touch-icon" href="apple-touch-icon.png">

	<!-- CSS : implied media="all" -->
	<link rel="stylesheet" href="<? echo CDN_BASE; ?>/css/style.css?v=1">

	<!-- All JavaScript at the bottom, except for Modernizr which enables HTML5 elements & feature detects -->
	<script language="JavaScript" src="<? echo CDN_BASE; ?>media/js/modernizr-1.5.min.js"></script>
	
	<style type="text/css">
		img.pic {
			margin: 20px;
			border: 0px;
		}
		.thumb-container {
			float: left;
			margin: 15px;
		}
		.thumb-thumb {
			text-align: center;
			display: block;
		}
		.thumb-caption {
			text-align: center;
			display: block;
		}
		.clear {
			clear: all;
		}
		
	</style>
</head>

<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->

<!--[if lt IE 7 ]> <body class="ie6"> <![endif]-->
<!--[if IE 7 ]>    <body class="ie7"> <![endif]-->
<!--[if IE 8 ]>    <body class="ie8"> <![endif]-->
<!--[if IE 9 ]>    <body class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <body> <!--<![endif]-->

	<div id="global-title-container">
		<div id="title-wrapper" class="clearfix">
			<div id="title-left">
				<a href="http://www.gersteinlab.org"><strong>Gerstein</strong> Lab</a>
			</div>
			<div id="title-right">
				<a href="http://cbb.yale.edu">
					Bioinformatics
				</a>
			</div>
		</div>
	</div>
	<div id="global-container">
		<div id="global-main" class="clearfix">
			<div id="global-full">
				<h1>Archived Image gallery</h1>
				
				<div class="clearfix">
				<?php sort($thumbs_list); ?>
				<? foreach($thumbs_list as $file): ?>
					<? $path = 'thumbs/'.$file; ?>
					<? if (!is_file($file)): ?> 
						<? continue; ?>
					<? endif; ?>
					<div class="thumb-container">
						<div class="thumb-thumb">
							<a href="<? echo $file; ?>">
								<img src="<? echo $path; ?>" class="thumb" alt="<? echo $file; ?>">
							</a>
						</div>
						<div class="thumb-caption">
							<a href="<? echo $file; ?>"><? echo $file; ?></a>
						</div>
					</div>
				<? endforeach; ?>
				</div>
				<br class="clear" />
			</div>
		</div>
		<div id="global-footer">
			&copy; <a href="http://info.gersteinlab.org/Permissions">2011</a> <strong><a href="http://www.yale.edu">Yale University</a></strong>
		</div>
	</div>
</body>
</html>
