Displaying a random image using PHP

Lyndi posted about displaying an image at random the other day. Her solution was to use javascript as it enabled the page to do this even if the server had caching on. It was a good solution, but it got me thinking about going in a slightly different direction…

The solution relied on the image names being hard coded into the script. I thought it would be quite cool to come up with a solution that simply looked in a directory for all the JPG’s or GIF’s, and plucked one at random.

This is what I came up with:

$dir = “images”; // The directory where your images are
$filetypes = (“jpg”||”gif”); // Whatever images are valid. You could add “png” etc

srand((double)microtime()*123456789); // Generates a random number seed
$count = 0; // Initialises the counter

$dirOpen = opendir($dir); // Opens the image directory

while(($im = readdir($dirOpen)))
if($im != “..” && $im != “.” && substr($im,-3)==$filetypes) // Disregards “..” and “.” (direcrory structure)
$image[$count] = $im; // Reads an image
$count++; // Increments the counter

closedir($dirOpen); // Closes the directory

$randname = rand(0,(count($image)-1)); // Generates the random number
echo ‘<img src=”‘.$dir.’/’.$image[$randname].'” alt=”Random Image” />’; // Publishes the image


I have commented the code, so it should be fairly self-explanatory how it works.

It is a bit rough and ready, but it does the job. To implement this on a website, simply use:

<? include(‘randimg.php’)?>

The beauty of this solution is you can add as many images as you like to the images (or whatever you call it) folder. Add and remove them at will and the script just picks from the images that are there at the time.

Let me know what you think.

Kontera affiliate review

konteralogoI decided a while back that I was going to use this blog as a springboard to try out new technologies and to write about them. I have tried a few and am in the process of experimenting with several others, but I have come to the end of the line with Kontera Affiliates and would like to share my experience.

As you can probably see I do have a small amount of monetization on this blog. Nothing too in your face or things that impede the user experience, but just a small amount which generates the odd click every now and again and puts a few pennies in the pot.

I have other sites that use heavier monetization strategies, but for the purpose of this blog I deliberately kept it light.

In August 2008 I implemented Kontera Affiliate links onto my blog. Kontera scans your content and links certain words in your content to adverts. As well as linking to the advert it also pops up a small advert when you roll over the word. To be honest the implementation is not too bad, although I am still unsure about the popups.

The problem I found is two fold. One, the earnings per click is very very low, in fact I would say lower than I have seen on any other network. The second issue is Kontera seems to have a very weird way of linking things. I would mention something about server speed or similar and it would link to Sun Microsystems. I can assure you most people looking at this blog will not be in the market for direct-purchasing a Sun server!

So, the failings are two fold and for this reason I have pulled the Kontera implementation before I even reached the minimum earnings for withdrawing the cash.

I don’t mind earnings being small, my focus is (and always will be) improving the content, earnings are just a bonus. What I do object to is diluting the quality of the blog by showing visitors adverts they have no interest in and have no relevance to their interests.

I will replace Kontera shortly and see how that goes.

If any of you have experienced Kontera, positively or negatively, I would be interested to hear your experiences.

Freshbooks now uses Google Checkout

freshbooks2Yes, that’s right, my constant complaining and moaning about the only thing wrong with Freshbooks has finally paid off. Freshbooks have just announced they support integration with Google Checkout!

I have just logged into my control panel and enabled it (easy to do, they provide idiot-proof instructions) and that’s about it. From now on all invoices that go out will include the Google Checkout link alongside the Paypal (spit!) link.

I am impressed that I single handedly managed to get them to do this, with no help from anyone, they must think really highly of me! Just kidding, there has been a bit of a push on their forums for some time, and in the grand scheme of things it really didn’t take them that long to implement.

Great work guys, your work is really appreciated!

Multitasking doesnt work

I read a post by Jay the other day about multitasking, or rather the myth of multitasking. Without going into the technical details of how computers (appear!) to multitask with humans it doesn’t work. People boast about it because it makes them seem great, and people write it on their CV because they think (and they may well be right) that it is what employers want to read.

In actual fact people don’t multitask very well, certainly not in creative or development environments. Focusing on one thing at a time is a far more effective way of achieving objectives and maintaining quality. If you really are that busy that you feel the need to multitask then in actual fact what you probably need is to outsource, leaving you to focus on single important tasks at a time.

People often get backed into a corner by lack of planning, hence they end up multi-tasking. How otfen have you left something to the last minute, only to find out that you then find something else that is also urgent?

Sometimes I find myself multitasking without even knowing it. Multitasking doesn’t have to mean working on two projects at once, it can be as simple as reading the email that just came in, or answering an MSN message.

Jay sites a study by Eben Pagan about how long it takes for your mind to adjust back to what you were doing (and become productive again) once you have been distracted. Apparently it is 10 minutes or so. This doesn’t sound a lot on the face of it, but if you are distracted 6 times in a day (and you probably get distracted a lot more than that) then that is already 1 hour. I’m sure you could find a better use for that hour, I certainly could!

Eben Pagan says that “multitasking lowers the IQ more than smoking pot”. While I would not condone smoking the weed as a viable alternative to multitasking he does have a point.

Sometimes it is worth remembering that email is not instant messenger. The email that just came in will still be there in half an hour, when you have finished the job you were doing, yet for some reason there can be an overwhelming compulsion to deal with it right away.

A good experiment is to close all distractions (email/MSN/twitter etc) while you are working. If you spend an hour focusing on what you should be doing instead of multitasking you will be surprised how much you get done.

There was a lot more to Jay’s post than just multitasking, but as is often the case I read a post and something jumps out to me over and above the rest.

Have a read for yourself and let me know, do you suffer from multitasking?

Backup strategy revisited

I am taking my Macbook Pro in for repair today, so I spent most of last night backing up all the data (critical and non-critical) in case the system needs restoring or it has to be away for a while. It made me re-evaluate my backup strategy a bit.

I do the majority of my work on my Macbook pro (120Gb) on my home wireless network. I also have a Linux machine as a backup solution. This backs up to an external 250Gb hard drive overnight, using rsync (so as to only backup the changed files).

This solution is not great, and it is a bit cumbersome. I plan to replace this with a solution that will involve RAID-1 (two hard drives mirrored. If one fails nothing is lost).

There are a few ways to do this:

  • I could add RAID 1 to the Linux machine. This is a little difficult as it is a tiny machine that sits underneith the TV and there is really not the room for a second hard drive.
  • I could replace the external hard drive with a USB enclosure that uses RAID 1. This is not too expensive, but adds a second device which also needs space and power.
  • I could invest in a NAS box, such as a Synology, QSNAP or Thecus.

BUT, it’s not that simple…

As I do most of my work on the Macbook the first and formost important thing for me is to backup that data, live data that I am working on at the time.

As I work on a wireless network I dont want to transfer 120Gb every time I backup. This leaves be two options:

  • I could use a backup program that detects changes
  • I could use OSX’s built in Time Machine

Time Machine takes hourly backups of the current dat, along with weekly and monthly backups as space allows. The problem is it only works (properly) using Apple Airport hardware. There are hacks around for getting it working on a normal NAS, but there are pitfalls as well.

I have experimented with rsync options and even those seem to interfere with the operation of the macbook while I am working. Time Machine (from what I see) is fairly seamless.


That leaves one option, the Apple Airport Extreme.

Although it does not (as standard) use RAID the data is actually in two places anyway, the Macbook and the backup drive. This is a perfect solution for the Macbook backup, but what about archives?

Archives work a little differently. Data is passed from the Macbook to the Airport-attached hard drive. Once it is there it gets deleted from the Macbook.

This means that is the hard drive fails the data is lost… back to RAID!

After consideration I feel my best solution is to use the Apple Airport Extrme with a hardware RAID external hard drive. This means that the macbook live data is actually on 3 hard drives, and the archive data is on 2.

What do you guys use for your backup solution and archive data?