Forum Moderators: coopster
<?php
$columns = 3;
$thmb_width = 120;
$thmb_height = 80;
function resizeImage($originalImage,$toWidth,$toHeight){
// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;
// Recalculate new size with default ratio
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}
// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
return $imageResized;
}
function generateThumbnails(){
global $thmb_width,$thmb_height;
// Open the actual directory
if ($handle = opendir(".")) {
// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether tha actual item is a valid file
if (is_file($file)){
// Check whether the actual image is a thumbnail
if (strpos($file,'_th.jpg')){
$isThumb = true;
} else {
$isThumb = false;
}
if (!$isThumb) {
// Process the file string
$dirName = substr($file,0,strpos($file,basename($file)));
if (strlen($dirName) < 1) $dirName = '.';
$fileName = basename($file);
$fileMain = substr($fileName,0,strrpos($fileName,'.'));
$extName = substr($fileName,strrpos($fileName,'.'),
strlen($fileName)-strrpos($fileName,'.'));
// Check if the actual file is a jpeg image
if (($extName == '.jpg') ¦¦ ($extName == '.jpeg') ¦¦ ($extName == '.JPG') ¦¦ ($extName == '.JPEG')){
$thmbFile = $dirName.'/'.$fileMain.'_th.jpg';
// If a thumbnail dosn't exists tahn create a new one
if (!file_exists($thmbFile)){
imagejpeg(resizeImage($file,$thmb_width,$thmb_height),$thmbFile,80);
}
}
}
}
}
}
}
function getNormalImage($file){
$base = substr($file,0,strrpos($file,'_th.jpg'));
if (file_exists($base.'.jpg')) return $base.'.jpg';
elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';
else return "";
}
function displayPhotos(){
global $columns;
generateThumbnails();
$act = 0;
// Open the actual directory
if ($handle = opendir(".")) {
// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether tha actual item is a valid file
if (is_file($file)){
// Check whether the actual image is a thumbnail
if (strpos($file,'_th.jpg')){
++$act;
if ($act > $columns) {
echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';
$act = 1;
} else {
echo '<td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/></a></td>';
}
}
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Photo Gallery</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main">
<div class="caption">Photo Gallery</div>
<table align="center"><tr>
<?php displayPhotos();?>
</table>
</div>
</body>
1)first scan your directory for image files. get the number of images if you can, this can help you determine the maximum pages and find the number of items you want to display per page.
2)get the url of each of those files, and store in a two dimensional array. the first key of the array would be the page number, and the second, the url of the picture. so you can have something like this when you're done.
$pictures[1]["mypic/pic1.jpg"]; //page 1, pic1.jpg
$pictures[1]["mypic/pic2.jpg"]; //page 1, pic2.jpg
....
$pictures[10]['mypic/pic10.jpg"]; //page 10, pic10.jpg
3) The only thing left to do is to display the array content. so you can set up a $_GET variable which has values from 1 to your array lenght(in this case 10). so whatever the current $_GET['page'] value is, you display the array base on. so if $_GET['page'] is one, you only go through $pictures[1] and dislpay all the urls there. the same as you progress to the array with each page.
4) displaying the "previous 1 2 3 4 5 next" links maybe a little tricky involving some nested if loops becuase you will need to check the array elements and see if it has valid data or not.
Good luck.