All about PHP/MySQL

Saturday, February 7, 2009

PHP function for Basic Image Gallery

This PHP function is a very basic one, yet useful for a single page image gallery within a small website. It reads all the images from given folder and creates a simple looking gallery with given sized thumbnails.

Function:

function Create_Gallery($dir, $width, $height){

$folder=dir($dir);

$gallery_html = '<div> ';

while($folderEntry=$folder->read())
{
if ($folderEntry != "." && $folderEntry != ".."){
$gallery_html .= ' <a href="'.$dir.'/'.$folderEntry.'"> <img style="border: 6px double rgb(84, 85, 101);" src="'.$dir.'/'.$folderEntry.'" alt="The thumb" width="'.$width.'" height="'.$height.'"/></a>';
}
}

$gallery_html .= ' </div> ';
$folder->close();

echo $gallery_html;

return;
}

Usage:

Create_Gallery("gallery_images", "200", "140");

Where 'gallery_images' is the folder containing the images. 2nd and 3rd arguments of the function are the desired width and height of thumbnails.

Good Luck :)

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home