how to create a random a avtar

Status
Not open for further replies.

wolvrine

Broken In
To do this used php , the GD library and the most important ingredient the rand() function.

The rand function selects a random number between the 2 numbers you give it.
So rand(1,25) will output a random number between 1 and 25 everytime the script is run. This function is usefull for making any dynamic random content and can be used for many different things.
We are going to use it to select a random image.

Ok, so lets go through the code and see what it does.
Code:
$rand = rand(1, 25);

The first line of our code selects a random number between 1 and 25, so we have the image we are using straight away.

Code:
$image = "$rand.png";

The second line tells the server what image we are going to be using.
Note the use of $rand followed by .png
This creates the name of the image. 1.png up to 25.png

Code:
$im = imagecreatefrompng($image);

The next line tells the server to create an image using the randomly selected png image.

Code:
header("Content-Type: image/png");

The next line is imprtant.
It instructs the persons browser to expect binary data.
If this isnt specified the browser will output text (like you see when you edit a picture in notepad).
With this line the browser knows it is supposed to create an image from the information it recieves.

Code:
Imagepng($im,'',100);
ImageDestroy ($im);

The final two lines create the image and then throw it away.

Now, we should have a script that looks like this
Code:
<?php
$rand = rand(1, 25);
$image = "$rand.png";
$im = imagecreatefrompng($image);

header("Content-Type: image/png");
Imagepng($im,'',100);
ImageDestroy ($im);
?>

If you put that in a folder with 25 images called
1.png
2.png
3.png
all the way up to 25.
Each time the image is loaded it will out put a different image.
 
D

deadman

Guest
By the way, DIGIT is not alowing siggy

so no use in this forum

deadman is here
 

joshna7

Right off the assembly line
why create random avatar when you can create your own avatar?

*www.ceveni.com/2009/01/create-your-own-animated-avatar-using.html
 

Liverpool_fan

Sami Hyypiä, LFC legend
why create random avatar when you can create your own avatar?

*www.ceveni.com/2009/01/create-your-own-animated-avatar-using.html

Wow you really have the power to raise the dead back from life. :p
Raising from the dead is a gift indeed, use it well. :cool:
 
Status
Not open for further replies.
Top Bottom