The Sword of Pretty Thoughts

The (Creative) Life and Times of J.G. Jerome

Some Quick Gallery Code

At one time I wanted to show holiday party photos but didn’t want to build a full blown gallery site with plugins and what-not, so I hacked this together in about an hour:

<?php

// filename: index.php
//  purpose: a quick and dirty gallery for photos in a folder
  
 //parts inspired from http://www.webinfopedia.com/php-list-images-in-a-directory.html


//Path to folder which contains images
$dirname = dirname("..");
 
//Use glob function to get the files
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use
//below line and commnent $images variable currently in use
$images = glob($dirname."/*.JPG");
 
 //debug
 //var_dump($images);
   //echo dirname("..");

 
 echo "<!DOCTYPE html>\n";
 echo "<html>\n<head>\n";
 echo "<TITLE>Gallery Title in Browser Window goes here</TITLE>\n";
 
 echo "<center><h3>Title of Folder goes here</h3></center>\n";
 echo "</head>\n";
 
echo "\n\n\n"; 
 
echo "<body>\n";



 if ( !empty ( $images ) ) { 
 
	//Display image using foreach loop
	foreach($images as $image){
	
	//print the image to browser with anchor tag (Use if you want really )
	echo '<a href="'.$image.'" target="_blank"><img src="'.$image.'" style="width:100%; height:auto;" /></a>' . "\n";
	}
 }else
 {
	echo "No JPEG pictures found";
 }
 
 echo "\n</body>\n</html>\n";
 
?>

You’ll also need this in your .htacess file:

Options +Indexes

Zip file for the curious.

Photo credit: Pixabay.com

Next Post

Previous Post

© 2019 The Sword of Pretty Thoughts

Theme by Anders Norén