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"

  • Finally you just need a PHP code which reads the file list in the folders, and generates the links to the files showing the thumbnails, like in here: http://abadcer.com/photos/Alex day 2/

<?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);
}
?>

How did I put all this together

  • Got a domain in 123-reg.co.uk
  • Got a dedicated Linux server from ovh.co.uk
  • Got Apache2, MySQL and PHP5 installed
  • Changed the DNS settings of abadcer.com to point to this server.
  • Downloaded and install WordPress from wordpress.org
  • Found a nice theme to skin the site.
  • Done

It is easier than what it looks, more details soon.

First post

Hello! Not much to see here yet. I’ll post interesting things soon.