PHP Script
following is the PHP Script which will generate a photo gallery as thumbnails and show images on browser.
create new folder named “images” and put some images inside the created folder, and create new PHP file and paste the following code within HTML <body> </body> tag and save it as “index.php” outside the images folder.
<?php
$folder_path = 'images/'; //image's folder path
$num_files = glob($folder_path . "*.{JPG,jpg,gif,png,bmp}", GLOB_BRACE);
$folder = opendir($folder_path);
if($num_files > 0)
{
while(false !== ($file = readdir($folder)))
{
$file_path = $folder_path.$file;
$extension = strtolower(pathinfo($file ,PATHINFO_EXTENSION));
if($extension=='jpg' || $extension =='png' || $extension == 'gif' || $extension == 'bmp')
{
?>
<a href="<?php echo $file_path; ?>"><img src="<?php echo $file_path; ?>" height="250" /></a>
<?php
}
}
}
else
{
echo "the folder was empty !";
}
closedir($folder);
?>
“glob()” function returns an array of files and directories matching a specified pattern and it will count the number of files in the given directories/folder, and returns true or false. “GLOB_BRACE” was used to expand {a,b,c} to match ‘a’,’b’,or ‘c’.
The CSS/Stylesheet
add some css stylesheet to make image gallery looks better, you can add javascript also to make it awesome.
body
{
background:#fff;
}
img
{
width:auto;
box-shadow:0px 0px 20px #cecece;
-moz-transform: scale(0.7);
-moz-transition-duration: 0.6s;
-webkit-transition-duration: 0.6s;
-webkit-transform: scale(0.7);
-ms-transform: scale(0.7);
-ms-transition-duration: 0.6s;
}
img:hover
{
box-shadow: 20px 20px 20px #dcdcdc;
-moz-transform: scale(0.8);
-moz-transition-duration: 0.6s;
-webkit-transition-duration: 0.6s;
-webkit-transform: scale(0.8);
-ms-transform: scale(0.8);
-ms-transition-duration: 0.6s;
}
Complete Script / index.php
this is the complete script copy paste the following code and try to creating image gallery by adding some extra functionalities.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Creating an Image Gallery From Folder using PHP</title>
<style type="text/css">
body
{
background:#fff;
}
img
{
width:auto;
box-shadow:0px 0px 20px #cecece;
-moz-transform: scale(0.7);
-moz-transition-duration: 0.6s;
-webkit-transition-duration: 0.6s;
-webkit-transform: scale(0.7);
-ms-transform: scale(0.7);
-ms-transition-duration: 0.6s;
}
img:hover
{
box-shadow: 20px 20px 20px #dcdcdc;
-moz-transform: scale(0.8);
-moz-transition-duration: 0.6s;
-webkit-transition-duration: 0.6s;
-webkit-transform: scale(0.8);
-ms-transform: scale(0.8);
-ms-transition-duration: 0.6s;
}
</style>
</head>
<body>
<?php
$folder_path = 'images/'; //image's folder path
$num_files = glob($folder_path . "*.{JPG,jpg,gif,png,bmp}", GLOB_BRACE);
$folder = opendir($folder_path);
if($num_files > 0)
{
while(false !== ($file = readdir($folder)))
{
$file_path = $folder_path.$file;
$extension = strtolower(pathinfo($file ,PATHINFO_EXTENSION));
if($extension=='jpg' || $extension =='png' || $extension == 'gif' || $extension == 'bmp')
{
?>
<a href="<?php echo $file_path; ?>"><img src="<?php echo $file_path; ?>" height="250" /></a>
<?php
}
}
}
else
{
echo "the folder was empty !";
}
closedir($folder);
?>
</body>
</html>
Hope you guys like this tutorial, and please don’t forget to share.