<?php

$thumb_dimension = 100;
$boxsize = 120;
$gallery_title = 'Quick Gallery';
$thumb_quality = 80;


// if it's a thumb request, generate and serve a thumb

if (isset($_GET['generate']) && isset($_GET['dimensions']))
{
    // Get new dimensions
    list($new_width, $new_height) = explode('x', $_GET['dimensions'], 2);

    // Check for obvious problems or attempts to abuse
    if(($new_width * $new_height > ($thumb_dimension * $thumb_dimension)) || 
        ($new_width > $thumb_dimension) || 
        ($new_height > $thumb_dimension) ||
        ($new_width <= 5) || 
        ($new_height <= 5)) { die('Bad Dimensions'); }
    if (preg_match('/^\./', $_GET['generate'])) { header("Status: 403 Forbidden"); die('403 Forbidden'); }
    if (!file_exists($_GET['generate'])) { header("Status: 404 Not Found"); die('404 Not Found'); }

    // Resample
    $image = imagecreatefromjpeg($_GET['generate']);
    $image_p = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, imagesx($image), imagesy($image));

    // Output, tell browser to cache for 30 days
    header("Expires: " . date("D, j M Y H:i:s", time() + (86400 * 30)) . " UTC");
    header("Cache-Control: Public");
    header("Pragma: Public");
    header('Content-type: image/jpeg');    
    imagejpeg($image_p, null, $thumb_quality);
    exit();
}

// otherwise, scan current directory for jpegs

$d = dir(".");
$images = array();
while (false !== ($entry = $d->read()))
    if (preg_match('/^([^.].*)\.(jpg|jpeg|JPG|JPEG)$/', $entry, $result))
        $images[] = array('file' => $result[1], 'ext' => $result[2]);
$d->close();



// Output starts here...

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<!--

Instant PHP Photogallery Script
by Mike Purvis | http://uwmike.com

-->
<head>
    <title><?= $gallery_title ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="generator" content="Quick Gallery 0.1" />
    <style type="text/css">

h1 {
  font-family: Helvetica, sans-serif;
  font-size: 24px;
  border-bottom: 1px solid #ccc;
}
#images {
  border-bottom: 1px solid #ccc;
  padding-bottom: 20px;
}
#images ul {
  list-style: none;
  margin: 0; padding: 0;
}
#images ul li {
  float: left;
  margin: 8px; 
  position: relative;
}
#images ul li a { 
  display: block;
  position: relative;
  width: <?= $boxsize ?>px;
  height: <?= $boxsize ?>px;
  border: 1px solid #ccc;
}
#images ul li a:hover { border: 1px solid #999; }
#images ul li a img {
  position: absolute;
  border: 1px solid #555;
}
.clearing { clear: both; height: 1px; font-size: 1px; }

    </style>
</head>

<body>
    <h1><?= $gallery_title ?></h1>
    <div id="images">
        <ul>
<?        foreach ($images as $this_image):
            extract($this_image);
            list($width, $height) = getimagesize("$file.$ext");
            if ($width > $height) { $thumb_width = $thumb_dimension; $thumb_height = round($thumb_width * $height / $width); }
            else { $thumb_height = $thumb_dimension; $thumb_width = round($thumb_height * $width / $height); }
?>
            <li><a href="<?= htmlentities("$file.$ext") ?>" ><img style="top: <?= floor(($boxsize - $thumb_height) / 2) ?>px; left: <?= floor(($boxsize - $thumb_width) / 2) ?>px;" width="<?= $thumb_width ?>" height="<?= $thumb_height ?>" src="?generate=<?= htmlentities("$file.{$ext}&dimensions={$thumb_width}x{$thumb_height}") ?>" alt="<?= $file ?>" /></a></li>
<?        endforeach; ?>
        </ul>
        <div class="clearing"></div>
    </div>
</body>
</html>