Hello
i am trying to create a simple few functions to create image galleries
the code is layed out as follows.
root/
build.php
content/photos/
content/photos/1/1.jpg
content/photos/1/2.jpg
content/photos/1/3.jpg
the problem i am having seems to be with the path settings inside the build.php script, i am trying to 1st scan the content/photos/1 directory to find all files, then as files are found, thumbs are created with a _th.jpg added to orgianl images and then saved in the content/photos/1 directory
it should then output code to display the generated image gallery
the problem i seem to be having if i have the files 1.jpg,1_th.jpg, 2.jpg, 2_th.jpg and 3.jpg, 3_th.jpg in the root folder the same as build.php, it will create the thumbs etc in the correct directory, but once removed from the root directory the script fails
i'm simply trying to scan a directory, find all the images, create thumbs and then display the results
i really need it to scan the directory , find the images inside that , create thumbs inside that directory and then output content with displayPhotos();.
heres my script that i'm having problems with, if anyone can see the faults here , i whould be gratefull i've been on this one for 2 days now trying to slove the problem but can not find the problem here
<?php
/**
* Usage of Simple GalleryCreator Class
*
* @version : 1.0
* Purpose : Parsing images from a set directory creating html pages and thumbs
*/
//require_once "./includes/GalleryCreator.class.php";
$gallery_path = "./content/photos/1";
$columns = 5;
$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;
global $thmb_height;
global $gallery_path;
// Open the actual directory
if ($handle = opendir($gallery_path)) {
// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether the actual item is a valid file
//echo $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 = $gallery_path;
$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')){
$thmbFile = $dirName.'/'.$fileMain.'_th.jpg';
// If a thumbnail dosn't exists then 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;
global $gallery_path;
$gallery_pathb = $gallery_path."/";
generateThumbnails();
$act = 0;
// Open the actual directory
if ($handle = opendir($gallery_path)) {
// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether the 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="'.$gallery_pathb.getNormalImage($file).'">
<img src="'.$gallery_pathb.$file.'" alt="'.$file.'"/></a></td>';
$act = 1;
} else {
echo '<td class="photo"><a href="'.$gallery_pathb.getNormalImage($file).'">
<img src="'.$gallery_pathb.$file.'" alt="'.$file.'"/></a></td>';
}
}
}
}
}
}
displayPhotos();
?>