Code: Select all
<?PHP
//standard height for thumbnails
define('HEIGHT',80);
//this is the max height and width for the big pictures. We will size them down so
//that their smaller side is less then the appropriate number
define('MAX_HEIGHT',450);
define('MAX_WIDTH',800);
//initialie variables
$i = 0;
$javascript = "";
$thumbNailDisp = "";
//check if there is a thumbs directory, and if not, create one
if(!is_dir('thumbs'))
{
mkdir('thumbs');
}
//open the directory with the thumbs
if($readMe = opendir('thumbs'))
{
while(false !== ($file = readdir($readMe)))
{//loop through files
if(strpos($file, '.jpg') or strpos($file, '.JPG'))
{
//build an arrat of thumbnails so we know whats already been done
$currThumbsї$file] = true;
}
}//end loop through files
}//end if directory opened
closedir($readMe);
//open the directory with the files
if($readMe = opendir(getcwd()))
{
while(false !== ($file = readdir($readMe)))
{ //loop through files
if(strpos($file, '.jpg') or strpos($file, '.JPG'))
{
//build an array of pictures
$picsї$file] = true;
//find out the size of the image we're dealing with here
list($width, $height) = getimagesize($file);
//this is for preloading the images and needs some work
$i++;
$javascript .= "Image$i= new Image($width,$height);\n
Image$i.src = '$file';\n\n";
//if there is no thumbnail for this image, create it!
if(!isset($currThumbsї$file]))
{//create thumbnail
$im=imagecreatefromjpeg($file);
$newHeight = HEIGHT;
$newWidth = ($width*HEIGHT)/$height;
$small = imagecreatetruecolor($newWidth , $newHeight); // new image
ImageCopyResampled($small, $im, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
ImageDestroy($im);
ImageJPEG($small,'thumbs/'.$file,100);
ImageDestroy($small);
}
//array of valid thumbnails (so we can delete the others!)
$validThumbsї$file] = true;
//make sure the picture isn't too big
if($height<$width AND $height > MAX_HEIGHT)
{//if picture is landscape and taller then max height, shrink it
$im=imagecreatefromjpeg($file);
$shrunkHeight = MAX_HEIGHT;//set size for new image
$shrunkWidth = round(($width*MAX_HEIGHT)/$height);
$small = imagecreatetruecolor($shrunkWidth , $shrunkHeight); // new image
ImageCopyResampled($small, $im, 0, 0, 0, 0, $shrunkWidth, $shrunkHeight, $width, $height);
ImageDestroy($im);
ImageJPEG($small,$file,100);
ImageDestroy($small);
$width=$shrunkWidth;
$height=$shrunkHeight;
}
else if($height>$width AND $width > MAX_WIDTH)
{//if its portrait style and too wide, shrink it
$im=imagecreatefromjpeg($file);
// path to your galleryunlink($file);
$shrunkWidth = MAX_WIDTH;//set size for new image
$shrunkHeight = ($height*MAX_WIDTH)/$width;
$small = imagecreatetruecolor($shrunkWidth , $shrunkHeight); // new image
ImageCopyResampled($small, $im, 0, 0, 0, 0, $shrunkWidth, $shrunkHeight, $width, $height);
ImageDestroy($im);
ImageJPEG($small,$file,100);
ImageDestroy($small);
$width=$shrunkWidth;
$height=$shrunkHeight;
}
$thumbNailDisp .= "<a href="javascript:changePicture('$file', '".$width."', '".$height."');"'><img src='thumbs/".$file."'></a>";
}
}//end loop through files
}//end if directory opened
closedir($readMe);
if(isset($currThumbs) and is_array($currThumbs))
{
//get thumbnails whose pictures no longer exist
$invalidThumbs = array_diff(array_keys($currThumbs),array_keys($validThumbs));
//loop through invalid thumbnails and delete them
foreach($invalidThumbs as $fileName)
{
unlink("thumbs/$fileName");
}
}
$picNum=0;
?>
<html>
<head>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
<SCRIPT language="javascript">
<!--
var maxHeight=<?=MAX_HEIGHT?>;
var maxWidth=<?=MAX_WIDTH?>;
function changePicture(source, newWidth, newHeight)
{
var img=document.getElementById("picItself");
img.src=source;
img.width=newWidth;
img.height=newHeight;
var e=document.getElementById("picHolder");
if(newWidth>900)
e.style.width = maxWidth+'px';
else
e.style.width = (newWidth*1+20)+'px';
if(newHeight>500)
e.style.height = maxHeight+'px';
else
e.style.height = (newHeight*1+20)+'px';
}
-->
</SCRIPT>
</head>
<body bgcolor = '#000033' >
<center>
<table width = '900' margin = 0 border = 0>
<tr>
<td nowrap style='overflow : auto;' bgcolor='#000033'>
<div style='width : 900px; height : 110px; overflow : auto;'>
<?=$thumbNailDisp ?>
</div>
</td>
</tr>
<tr>
<td valign='top' align='center' bgcolor='#000033'>
<div id = 'picHolder' style='width : 900px; height : 110px; overflow : auto;'>
<img src='instruct.gif' id='picItself'>
</div>
</td>
</tr>
</table>
</center>
<SCRIPT language="javascript">
<!--
<?=$javascript?>
-->
</SCRIPT>
</body>
</html>