Hi, I'm trying to create a PHP gallery for my website, I found this script and is very good but I wanted to add how many images to be display per page in this script. So lets say I have 60 images in one directory, I will like to display only 20 images per page instead of 60. And some how add a pagination Links under the gallery automatically to change pages. Is there any way to do this? Heres the PHP code.. Thank you in advance! <?php $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; // 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).'" rel="lightbox"><img src="'.$file.'" alt="'.$file.'"/></a></td>'; $act = 1; } else { echo '<td class="photo"><a href="'.getNormalImage($file).'"rel="lightbox"><img src="'.$file.'" alt="'.$file.'"/></a></td>'; } } } } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html> <head> <title>Gallery</title> <link href="style/style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="../js/lightbox.js"></script> </head> <body> <div id="main"> <center><div class="caption">Gallery</div></center> <table align="center"><tr> <?php displayPhotos(); ?> </table> </div> </body> PHP:
That is a lot for someone to do for free my friend. First, I recommend using mysql to store your image names, filenames, ect. That way if you decide to add ratings and comments later, you can with ease.
Oh wow so is harder then what I thought it was. I guess I'm going to find another one or try to do it myself. Thanks.
Here is a different script but it doesn't use lightbox <?php ######################################################### # Set the following configuration variables if you don't# # want to use the defaults. We suggest giving the script# # a test-run with the defaults to see how it looks, then# # deleting the auto generated thumbnails and making any # # desired changes to the settings. When you load up the # # script again, new thumbs will be generated. # ######################################################### # Path to HTML template which contains the %output% token, if you # want to use your own template. Defaults to built-in template. # If you create your own, just stick %output% in the HTML file # wherever you want the gallery output to appear. $template = ""; # Here are the width and height settings for thumbnails. In most # cases you will want to specify a value for one of these, and set # the other one to 0, this enables the auto ratio calculations. # If you set both to nonzero values, your thumbnails will all # be generated at the exact same height/width but they'll end up # looking "warped" and out of proportion. You MUST set at least # one, either the height or the width. # Desired width of thumbnails (or 0 for auto) $thumbw = 200; # Desired height of thumbnails (or 0 for auto) $thumbh = 0; # The thumbnails will be displayed in a table. The following # variables control the appearance of the table. # $perrow = number of thumbnails to display in each row $perrow = 3; # $perpage = number of images to display per page, this # really should be some multiple of $perrow $perpage = 9; # Size of the border for the table, in pixels $tableborder = 0; # Width of the table $tablewidth = "90%"; # Cell spacing for the table, in pixels $cellspacing = 3; # Color of the table $bgcolor = "#f0f0f0"; # Font to use for displaying the table $fontface = "Verdana, Arial"; # Font size $fontsize = 2; # You can have information about each image displayed below # its thumbnail in the table. Use the following settings to # toggle these displays on or off by setting to 1 or 0. # Display file name $showname = 1; # Display image coordinates $showcoords = 1; # Display file size in KB $showsize = 1; # If you want PHP to include() a header before displaying # the gallery page, set the full system path here, e.g. # /home/foo/public_html/gallery/header.php $header = ''; # If you want PHP to include() a footer after displaying # the gallery page, set the full system path here, e.g. # /home/foo/public_html/gallery/footer.php $footer = ''; # By default, thumbnails displayed in the gallery will link # directly to the fullsize JPG. If you'd rather have them # point somewhere else (for example to your own script that # checks a user's cookie or tracks how many times each pic # has been viewed), you can take advantage of the $linkurl # variable. The %picname% token will be replaced automatically. # For example you might want the thumbnails to link to # "/your/site/viewpic.php?image=%picname%" where viewpic.php # is a script you wrote that counts the view and displays the # image specified by %picname% # If you do not need this capability, leave the default # value, the default will work for most people. $linkurl = "%picname%"; ######################################################### # End of configuration variables # ######################################################### set_time_limit(0); #If there's no template file, use the default if(!$template){ $template .= <<<EOT <title>Displaying Gallery</title><body bgcolor="#FFFFFF"><center> <hr noshade size="1"><font face="$fontface" size="$fontsize">Click a thumbnail to view the full-size image</font></center><p align="center"><font face="$fontface" size="$fontsize">%output%</font></p> EOT; } else{ $template = implode("", file($template)); } #Open the cwd and build a list of JPG files $jpegs = array(); $directory = opendir(getcwd()); while($filename = readdir($directory)){ #ignore . and .. as well as existing thumbnails if((strlen($filename) > 2) && (strcasecmp(substr($filename, 0, 5), 'thumb') != 0)){ $localext = substr($filename, -3); if(strcasecmp($localext, 'jpg') != 0) continue; else{ array_push($jpegs, $filename); } } } sort($jpegs); reset($jpegs); #Now that we have the files, we need to create thumbs if #they don't exist foreach($jpegs as $var){ if(!file_exists("thumb$var")){ if(!isset($pleasehold)){ $pleasehold = 1; echo "<font face='$fontface' size='$fontsize'><b>One moment please...</b><br><br>New images were found in this gallery, and thumbnails must be made for them.<br><br>Processing images: "; flush(); } echo "<font face='$fontface' size='$fontsize'>$var... </font>"; flush(); makejpegthumb("$var", $thumbw, $thumbh, "thumb$var", 100); } } if($pleasehold){ echo "<font face='$fontface' size='$fontsize'><br><br><b>Thumbnails created!</b></font><br>"; } #Build the navigation links $nav = "<center><font face='$fontface' size='$fontsize'>"; #$numlinks = round(count($jpegs)/$perpage, 0); $numlinks = (int)ceil(count($jpegs)/$perpage); $linkstart = 1; for($i=1; $i<=$numlinks; $i++){ $nav .= "<a href='$_SERVER[PHP_SELF]?start=$linkstart' title='Go to page $i'>Page $i</a> "; $linkstart += $perpage; } $nav .= '</font></center><br>'; $output = "$nav\n"; #It's time to display the gallery! $picnum = (isset($_GET[start])) ? (($_GET[start] == 0) ? 0 : $_GET[start] - 1) : 0; $output .= <<<EOT <!-- Begin Table -->\n\n<table width="$tablewidth" bgcolor="$bgcolor" border="$tableborder" cellspacing="$cellspacing" align="center"> EOT; for($i=0; $i<(int)($perpage/$perrow); $i++){ $output .= '<tr>'; for($j=0; $j<$perrow; $j++){ $output .= "<td align='center' valign='middle'>"; if(file_exists($jpegs[$picnum])){ $target = str_replace('%picname%', $jpegs[$picnum], $linkurl); $output .= "<a href='$target' target='_blank'><img src='thumb$jpegs[$picnum]' border='0' alt='Click to view $jpegs[$picnum]'></a>"; if($showname == 1){ $output .= "<br><font face='$fontface' size='$fontsize'>$jpegs[$picnum]</font>"; } if($showcoords == 1){ $coords = getimagesize($jpegs[$picnum]); $output .= "<br><font face='$fontface' size='$fontsize'>$coords[0] x $coords[1]</font>"; } if($showsize == 1){ $output .= "<br><font face='$fontface' size='$fontsize'>" . sprintf("%.02f", (float)(filesize($jpegs[$picnum]) / 1024)) . 'KB</font>'; } $output .= "\n"; } $picnum++; } $output .= '</tr>'; } $output .= "</table><br>$nav\n\n"; $template = str_replace('%output%', $output, $template); if($header){ include($header); } echo $template; if($footer){ include($footer); } #Make a JPG thumbnail of a source image function makejpegthumb($infile, $outxcoord, $outycoord, $outfile, $quality){ $insize = getimagesize("$infile"); $inxcoord = $insize[0]; $inycoord = $insize[1]; $inresource = imageCreateFromJPEG("$infile"); if($outxcoord == 0){ #auto-width: calculate new width based on ratio of old to new height $ratio = (float)($outycoord / $inycoord); $outxcoord = $inxcoord * $ratio; } else if($outycoord == 0){ #auto-height: calculate new height based on ratio of old to new height $ratio = (float)($outxcoord / $inxcoord); $outycoord = $inycoord * $ratio; } $outresource = imagecreatetruecolor($outxcoord, $outycoord); $target_pic = imagecopyresampled($outresource, $inresource, 0, 0, 0, 0, $outxcoord, $outycoord, $inxcoord, $inycoord); imagejpeg($outresource, "$outfile", $quality); } ?> PHP:
The script I posted will do exactly what you want... make sure you set write permissions on the folder... also I am sure it won't work in safe mode.