Jump to content

help with my image gallery


thejj924

Recommended Posts

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="thickbox.js"></script>
<link rel="stylesheet" href="thickbox.css" type="text/css" media="screen" />
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<?php
$columns     = 4;
$thmb_width  = 150;
$thmb_height = 120;

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')){
		    		$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).'" class="thickbox"><img src="'.$file.'" alt="'.$file.'"/></a></td>';	
					$act = 1;
				} else {
					echo '<td class="photo"><a href="'.getNormalImage($file).'"class="thickbox"><img src="><img src="'.$file.'" alt="'.$file.'"/></a></td>';	
				}
      			
      		}
      	}
	}
}	
}

?>
<style type="text/css">
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #000;
}
a:link {
color: #000;
}
a:visited {
color: #000;
}
a:hover {
color: #000;
}
a:active {
color: #000;
}
.Main {
Color:000;
font:Arial, Helvetica, sans-serif;
font-size:12pt;
}
-->
</style></head>
<body bgcolor="#555555" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"><Center>
<table width="805" bgcolor="#999999">
<tr><td><center>
<table id="Table_01" width="800" height="200" border="0" cellpadding="0" cellspacing="0">
<tr>
	<td colspan="7">
		<img src="../images/banner_01.jpg" width="800" height="164" alt=""></td>
</tr>
<tr>
	<td><a href="../index2.html"><img src="../images/Untitled-1_02.jpg" width="100" height="36" border="0" alt=""></a></td>
	<td><a href="../models.html"><img src="../images/Untitled-1_03.jpg" width="100" height="36" border="0" alt=""></a></td>
	<td><a href="../access.html"><img src="../images/Untitled-1_04.jpg" width="100" height="36" border="0" alt=""></a></td>
	<td>
		<img src="../images/Untitled-1_05.jpg" width="200" height="36" border="0" alt=""></td>
	<td><a href="index.php"><img src="../images/Untitled-1_06.jpg" width="100" height="36" border="0" alt=""></a></td>
	<td><a href="../aboutus.html"><img src="../images/Untitled-1_07.jpg" width="100" border="0" height="36" alt=""></a></td>
	<td><a href="contact.html"><img src="../images/banner_08.jpg" width="100" border="0" height="36" alt=""></a></td>
</tr>
<tr>
  <td colspan="7">  <table align="center"><tr>
  <center><b><font size="11pt">Welcome to Our Photo Gallery</font></b></center>
  <?php displayPhotos(); ?>

</table>
        </td>
  </tr>
</table>
</center></td></tr></table>
</Center>
</body>
</html>

when i view source on the page when uploaded it comes up as

	  <td class="photo"><a href=""class="thickbox"><img src="><img src="boardtrackerrightside_th.jpg" alt="boardtrackerrightside_th.jpg"/></a></td><td class="photo"><a href=""class="thickbox"><img src="><img src="boardtrackerleftside_th.jpg" alt="boardtrackerleftside_th.jpg"/></a></td><td class="photo"><a href=""class="thickbox"><img src="><img src="beachcomberrightside_th.jpg" alt="beachcomberrightside_th.jpg"/></a></td><td class="photo"><a href=""class="thickbox"><img src="><img src="beachcomberleftside_th.jpg" alt="beachcomberleftside_th.jpg"/></a></td> 

 

Link to comment
https://forums.phpfreaks.com/topic/202876-help-with-my-image-gallery/
Share on other sites

Found it:

 

 

you need brackets around these lines:

 

if (file_exists($base.'.jpg')) return $base.'.jpg';

elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';

else return "";

 

as:

 

if (file_exists($base.'.jpg')) {return $base.'.jpg'}
elseif (file_exists($base.'.jpeg')) {return $base.'.jpeg'}
else {return ""}

 

I love using the bracketless if syntax for solid one liners, but you need the brackets for an if else OR if elseif else

I've never had a script to fail a one line bracketless syntax for if/elseif/else statements.  Although, I agree that it is good practice to use brackets for all if/else statements.

 

I tested your getNormalImage() function, and it seems to work correctly.  I cannot test your displayPhotos() function, but did see some problems with it.  You can compare the changes I made, some may be redundant and not needed, but ...

function displayPhotos(){
   global $columns;
   
   generateThumbnails();
   $act = 0;
   // Open the actual directory
   if ($handle = opendir(".")) {
      // Read all file from the actual directory
      while (false !== ($file = readdir($handle)))  {
         // Check whether tha actual item is a valid file
               if (is_file($file) && ($file != '.' || $file != '..')){
            // Check whether the actual image is a thumbnail
               if (strpos($file,'_th.jpg') !== false){
               ++$act;
               if ($act > $columns) {
                  echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'" class="thickbox"><img src="'.$file.'" alt="'.$file.'"/></a></td>';   
                  $act = 1;
               } else {
                  echo '<td class="photo"><a href="'.getNormalImage($file).'"class="thickbox"><img src="><img src="'.$file.'" alt="'.$file.'"/></a></td>';   
               }
                  
               }
            }
      }
   }   
}

 

I see it is already fixed, but added this anyway.  Since I spent time on it.  :shrug:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.