HOME  TUTORIALS

Alt-Web Demo: Dynamic Photo Gallery with Bootstrap 4, PHP & Fancybox 3

This gallery of thumbnails and full sized slides is dynamically populated from a folder of images with PHP code. The modal viewer is powered by jQuery & Fancybox.

 

 

streams
YosemiteFallsBase
YosemiteFalls
VernalFalls
UpperYosemite
TreeMoss
Tourist
StanResting
SentinelView
SentinelTrail
SentinelRock
SentinelDome
SenitnelDomeSnowBall
RetrivingHat
NevadaFalls
Natives
MistTrailSteps
MirrorLakeEntry
MirrorLake
Lichen
HalfDomePeak
HalfDome-w
HalfDome-P
HalfDome-L
GlacierPtRock
GettingSoaked
DeadManRock
DSC00364
CapedCrusader
BridalVeilTop
BridalVeil
BlueJayBegging

Last updated: May 2018   |   File count: 32


Relevant HTML & PHP Code

<div class="row"> <div class="card-columns">
<!--begin dynamic gallery-->
<?php
$directory = 'thumbnails/folder'; //path to thumbnails
$link = 'slides/folder'; //path to full-sized images
$allowed_types = array('jpg','jpeg','png');
$aFiles = array();
$dir_handle = @opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle)) //traverse through directory {
if($file=='.' || $file == '..') continue; //skip links to parent directories
$file_parts = explode('.',$file); //split file parts and put each into an array
$ext = strtolower(array_pop($file_parts)); //last part is extension
$title = implode('.',$file_parts); //whats left is filename
$title = htmlspecialchars($title); //make the filename html-safe
if(in_array($ext,$allowed_types))
{
$aFiles[] = $file;
}
}
closedir($dir_handle); //close directory
natsort($aFiles); // natural sort filenames
$aFiles = array_reverse($aFiles); //display in reverse order
$i=0; //loop
foreach ($aFiles as $file) { //repeat as before
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);
echo '
<div class="card"> <a data-fancybox="gallery" class="fancybox" data-slide="'.++$i.'" href="'.$link.'/'.$file.'"><img class="img-fluid" alt="'.$title.'" src="'.$directory.'/'.$file.'"></a></div>'; } ?> </div><!--end gallery->