Another PHP gallery question (nearly done!)

Discussion in 'Web Design and Development' started by LastLine, May 28, 2007.

  1. LastLine macrumors 65816

    Joined:
    Aug 24, 2005
    #1
    Hey all, need a little help adapting this code, I think I know what I have to do, but I'm not sure how to do it...perhaps a php god can help me ;)

    The code is designed to create a php gallery of image thumbnails within the directory - which it does perfectly, the code immediately below this paragraph takes the thumbnail and displays the full size image of said thumbnail

    Code:
    					if ($act > $columns) {
     $trimname = substr($file, 0, -7);  
    						echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/><br>'.$trimname.' </a></td>';	
    						$act = 1;
    					} else {
    						echo '<td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/><br>'.$trimname.'</a></td>';	
    However the problem here is that it implements this purely as displaying the jpg. Now what I'd like to adapt it to do is to display it in a html page (or perhaps php page - either way) So for example when a user clicks on "Thumbnail 1" the site opens up a php page displaying "Full Picture 1" based around my normal style sheet, rather than just displaying "Full Picture 1" by itself.

    Anyone got any thoughts on this? Full code is displayed below, and the code in action is here

    http://www.clarkephotography.co.uk/files/animals/index.php

    Any help much appreciated :)
    Code:
    <?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(".")) {
    		// 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;
    $trimname = substr($filename, 0, -7);
    
    	// 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)){
     $trimname = substr($file, 0, -7); 
    				// Check whether the actual image is a thumbnail
    	      		if (strpos($file,'_th.jpg')){
     $trimname = substr($file, 0, -7); 
    					++$act;
    					if ($act > $columns) {
     $trimname = substr($file, 0, -7);  
    						echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/><br>'.$trimname.' </a></td>';	
    						$act = 1;
    					} else {
    						echo '<td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/><br>'.$trimname.'</a></td>';	
    					}
    	      			
    	      		}
    	      	}
    		}
    	}	
    }
    
    ?>
     
  2. SDDave2007 macrumors regular

    Joined:
    Apr 12, 2007
    #2
    Not sure I understand your problem

    In my photo gallery (btw-trikers.org) when the user clicks a thumbnail, it opens another PHP page passing the photo ID as a parameter, this new page then displays the picture inside a HTML page complete with naviagtion buttons to other points on our site
     
  3. LastLine thread starter macrumors 65816

    Joined:
    Aug 24, 2005
    #3
    Yeah, that's pretty much exactly what I need actually. Any idea how I can implement it? Preferably with this script as a basis as I like how I don't have to add images to the gallery manually (it just looks for _th.jpg in the file names within a given foler)
     
  4. LastLine thread starter macrumors 65816

    Joined:
    Aug 24, 2005
    #4
    Ok - little bump here

    If you go to the animals link on www.clarkephotography.co.uk I have this all sorted and happy now - thanks to everyone who's offered advice in the last few weeks - very much appreciated.

    Only have one small problem - my "View" buttons look great in safari, but funky in IE and Firefox (take a look, they're kinda like flat rectangles with a slightly off blue background) can anyone suggest how to get them looking more obvious? CSS for the form is attached below.

    Code:
    form.button {
        margin-left: 0px;
    	border: 0px solid #cccccc;
    width:60px;
    	background: #F7EED4;
        font-family: Arial, Helvetica, sans-serif;
        font-weight:normal;
        font-size : 10px;
    
    }
    Any help very much appreciated :)
     
  5. yagran macrumors 6502a

    Joined:
    Jan 8, 2007
    Location:
    Brighton, East Sussex, UK
    #5
    i think...

    you set the link on the images on the gallery page so they link to:

    http://www.yousite.com/showphoto.php&imgvar=X

    where X is the photo number

    then you make a php file called showphoto.php

    which would echo your stylesheet link and then echo

    <img src="http://www.yousite.com/photos/image_.imgvar.">

    if you see what i mean? ill check back when i get home and write up the code more fully if you ent figured it out :D
     

Share This Page