Jump to content
  • Advertisement
Sign in to follow this  
glyvin

[web] PHP gallery script

This topic is 3656 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys I have been using this prebuilt PHP gallery script

<?php
/*************************************************
 * Micro Photo Gallery
 *
 * Version: 1.0
 * Date: 2007-04-05
 *
 * Usage:
 * Just copy these files into your image folder
 *
 ****************************************************/

 $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,$thmb_height;
	
	// Open the actual directory
	if ($handle = opendir("gallery")) {
		// 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')){
			    		$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("gallery")) {
		// 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>HABC Gallery</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <div id="main">
    <div class="caption">Gallery</div>
      <table align="center"><tr>     
    		<?php displayPhotos(); ?>
      </table>		
    		
	<div id="source">Micro Photo Gallery 1.0</div>
  </div>
</body>   




<html>
<head>


<meta http-equiv= Content-Type: text/html; charset=utf-8" />
<title>Hamilton Athletic Booster Club</title>
<link href="style.css" rel="stylesheet" type="text/css" />

</head>
<body>

<center><img src = "banner.jpg"></center>
<Center><table class = "border" width = 900  bgcolor = "white" border = 0>

<tr>
<td valign = top >

<?php include("links.php");?>
</td>
<td>
  <table align="center"><tr>     
    		<?php displayPhotos(); ?>
      </table>		
    		
	<div id="source">Micro Photo Gallery 1.0</div>
  </div>



</td></tr>
</table>
</td>



</body>
</html>


And It works fine and dandy when I have the gallery script in the same directory as the pictures but I would like it so I can have it outside that directory. I changed the if ($handle = opendir("gallery")) so that it would look for the images in the right spots. Any idea whats wrong? Best, Will

Share this post


Link to post
Share on other sites
Advertisement
I would try "gallery/" instead of "gallery". If you look up is_dir it shows in the examples which strings it will see as folders and which it will see as files.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!