Close

20 July 2010

Adding a photo album

Not happy with the existing solutions/plugins to generate photo albums in WordPress I decided to create my own based on a BASH script and some PHP code.

  • Upload the photos you want to a folder, example:

http://abadcer.com/photos/

  • Because this folder doesn’t contain any index.php or default.html or similar I enabled “directory browsing” so you can see the files, in this case the photos, and click on them to see them. To do so create a file called .htaccess and put inside:
+Indexes
  • At this point you just have some folders with big JPG files inside, I decided to automate a process which creates thumbnails of this files in a subfolder called “.thumbs”. It is designed to search all JPGs in all the subfolders. My example worked in Debian in the folder where the images are:
 find . -type f | grep -E "jpg|JPG" | grep -v "/.thumbs/" | awk '{ run="convert -geometry 150x150 " "\"" $0 "\" ";
file=$0;
sub(/(.*)\//, "", file);
folder=$0;
sub(file, "", folder);
system("mkdir " "\"" folder ".thumbs/" "\"");
run=run "\"" folder ".thumbs/" file "\"";
system(run);}'
  • This will create the thumbnail (max 150 pixels wide or long) in /.thumbs/ subfolder. It basically finds all JPG files and run this command for each file found:
convert -geometry '150x150' "./Alex day 5/DSC_6395_resize.JPG" "./Alex day 5/.thumbs/DSC_6395_resize.JPG"
 <?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
$jpg = strpos($file,"JPG");
$jpg_low = strpos($file,"jpg");
if($jpg == true | $jpg_low == true) {
echo "<a href=\"$file\"> <img src=\".thumbs/$file\" /></a>\n";
}
}
closedir($handle);
}
?>

One Comment on “Adding a photo album

abad
4 March 2011 at 11:44 PM

This is an improbed version of the PHP code which shows the files in alphabetic order:


<?php
$allfiles = array();
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
$jpg = strpos($file,"JPG");
$jpg_low = strpos($file,"jpg");
if($jpg == true | $jpg_low == true) {
$allfiles[] = $file;
}
}
sort($allfiles);
foreach ($allfiles as $image) {
echo "<a href=\"$image\" rel="nofollow"> <img src=\".thumbs/$image\" /></a>\n";
}
closedir($handle);
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *