16 Mar 2010

Simple PHP Random Image Script

PHP 9 Comments

There are times when working with smaller sites you need a simple method of displaying random images. This can be done in one line of text directly in your HTML and a collection of images.

Prepare the Images

The first step is to gather together the images you wish to have randomly rotated. Change all of their file names to a numerical order, starting with the number 1.

  • 1.jpg
  • 2.jpg
  • 3.jpg
  • 4.jpg

Be sure each image has the same extension (either all jpg, png, or gif), and that there are no gaps in the numbers. Also, place these images in their own folder to keep everything organized.

Write the Code

Now in the HTML of your page, insert the following code where you want an image to display:

<img src="/path/to/<?php echo rand(1,n);?>.jpg" alt="Random Image" />

The “rand” operator will display a randomly generated number between 1 and ‘n’. Change ‘n’ to the total amount of pictures you have arranged numerically (or the highest numbered picture you have). In the example above I only have 4 pictures, therefor I would change it to: rand(1,4);

To finish it off, change ‘/path/to/’ to the path your stored the images in.

9 Responses to “Simple PHP Random Image Script”

  1. bruce says:

    thanks for this simple approach!!
    do you know a way to have the images change in sequence?
    thanks again

  2. chad says:

    Bruce,

    You will either need to look into using a slideshow, or you can track user’s sessions. You can assign a session variable that keeps track of the last image that was shown for that particular user, and have the image change every time the page is refreshed in sequence.

  3. Sean says:

    Hi Chad, you’ve been a great help.
    I’m building a animated “gif” hosting site, i’d like to categorize my images and have 4 of the most viewed images at the top of the page and then the newest stuff below.
    I’d like to be able to find the gifs in pre-determined categories as well.
    I’m savvy on using the divs or tables to space them i just don’t know what kind of code i need to write.
    can i do this in html or will i need to use php?

  4. chad says:

    If you’re pulling the images from a database, you will need to use a combination of HTML and PHP to display them in that order.

  5. deck says:

    that’s awesome! thanks!

  6. Mike says:

    Thanks Chad. Is there away to assign a a href link around each of those images and an alt tag unique to each image?

  7. chad says:

    Mike,

    It will take creating an array and looping through it randomly. If you are interested, I could make another blog post with an example that would accomplish what you are looking for.

  8. Optimizare says:

    problem is the script will post duplicate images..

  9. chad says:

    Optimizare,

    Are you trying to call this code multiple times on the same page? If so, yes, it can randomly select the same number numerous times. When calling it more than once on the same page you will need to look into a more involved solution that keeps track of previously used images.

Leave a Reply