for the search, my way to preload images! haha

Search for in

Random Images

PHP Coding Tutorials | Submited May 22, 2008

Written by: User Picture..

This shows you how to display a random image with PHP. It's pretty brief, but still easy to follow. :)

What's this?


This is a guide on displaying a random image via a PHP file.
There's plenty of tutorials for doing this, but I couldn't find any updated (PHP >4.4) ones that would showed you how to do it without using PHP on the page you wanted to display the image on.

One - Create an array


Yup. The first thing to do is to create an array of images like the code below:

// This is an array of images - you can add more by duplicationg $imgs
$imgs[] = "http://sevenbeasts.com/random/photoshopuserbar.png";
$imgs[] = "http://sevenbeasts.com/random/phpUserbar.png";
$imgs[] = "http://sevenbeasts.com/random/Skem9Userbar.png";

That doesn't need much explaining, does it? It's an array of images, to add another image just add another $imgs[] = ""; line.

Two - Randomize


This is the whole point - randomness.

// This is the part that makes it display at random
srand ((double) microtime() * 1000000);
$randIMG = rand(0,count($imgs)-1);

srand() | microtime() | rand()


Trust me; The PHP website can explain much better than I can.

Three - Header


You'll need to set the header as PNG (or whatever format) so an image is displayed as an image.

//Set the content type as a PNG image
Header ("Content-type: image/png");


Four - Create the image


Yeah, it's creating an image from a randomly selected image in the array, how sexy is that?

// This creates an image from the array of images we made earlier
$doIt = imageCreateFromPNG("".$imgs[$randIMG]."");



Five - Make it a PNG


I prefer PNG, the quality is just better.

// make the image a PNG
ImagePng ($doIt);

Google with logic for more options on this one, it's pretty simple.

Six - DESTROY


Yeah, create a mental image of your image being violently blown up. It makes this a much more fun.

// Self explanitory
ImageDestroy ($doIt);


Done.


If you followed everything correctly you should have something similar to this:

<?php
// This is an array of images - you can add more
$imgs[] = "http://sevenbeasts.com/random/photoshopuserbar.png";
$imgs[] = "http://sevenbeasts.com/random/phpUserbar.png";
$imgs[] = "http://sevenbeasts.com/random/Skem9Userbar.png";

// This is the part that makes it display at random
srand ((double) microtime() * 1000000);
$randIMG = rand(0,count($imgs)-1);

//Set the content type as a PNG image
Header ("Content-type: image/png");

// This creates an image from the array of images we made earlier
$doIt = imageCreateFromPNG("".$imgs[$randIMG]."");

// make the image a PNG
ImagePng ($doIt);

// Self explanitory
ImageDestroy ($doIt);
?>


You can see an example here.

w00ty w00t

tags Tags: random images displayed randomly w00t

2Thumbs up  Thumbs down0

Related Tutorials
Adding Glitter to Images
Images in HTML
Text / Image Reflection
Myspace URL box images
Resize comment images [skem9]
Randomness with PHP
PSP : MAKING IMAGES TRANSPARENT (TUT 2)
PSP : MAKING IMAGES TRANSPARENT (TUT 3)
How to get Header images in a Friendster Layout
Creating "Your IP is" images
Random Images
No Comments Found
Sorry, you have to be a member to comment tutorials