Jump to content
HomeUserlistGamesWipsExamplesOtherForums
Images Flash Files Examples Audio Files Rules Terms Of Service
[php]Random avatar script by Friiks
[php]Random avatar script
[php]Random avatar script

Hey!

Some of you may be still thinking about how to create a random avatar - avatar which will show different image when you load it.

Well to be honest it's quite simple, all you need is a web server which allows php and htaccess to be used, some basic php knowledge and avatars you want to be rotating.


Lets start by creating image.php file which will contain our script that will rotate the images.
When you've made the file make a directory named avatars in the same directory image.php is.

When you've done that open image.php and enter this code.

PHP Code


<?php

header
("Content-type: image/png"); //tell the script to output a png image



function extension($file){

$extension explode(".",$file);

$ext strtolower($extension[count($extension)-1]);

return 
$ext;

}

//function above strips extension of the file given



function dircontent($directory){

$files = array();

if(
$dir = @opendir($directory)){

while ((
$file readdir($dir)) !== false){

if(
$file != "." && $file != ".." && extension($file) == 'png'){

$files[] = $file;

}

}

closedir($dir);

}

return 
$files;

}

//function above gets all files from a directory and picks the ones with extension .png



$images dircontent("images");//get .png files of directory images

$total count($images);//get total number of images



$image "images/".$images[mt_rand(0,$total-1)];//get a random image from the images array

//Decreasing by 1 is needed because arrays start with 0, like

//$images array would be $images[0], $images[1] ... etc. but

//count gets the TOTAL number of values in the array. 





$new_image imagecreatefrompng($image); 

//create the image

imagepng($new_image);

//output the image 

imagedestroy($new_image); 

//destroy the image

?>



So that's the script with explanations.
Next thing to create is the .htaccess file. Do that now and pu it in same directory with image.php!

When you've created it, open it and put this in it

Code


RewriteEngine On
RewriteRule image.png image.php



The above code tells .htaccess to sort of redirect your request to image.php whenever you call image.png.
It isn't actually a redirect, it's a re-write :p

So now you can use the image.
The good things about this script is that you can get a file with an extension .png because many web sites and forums don't allow files which don't have image extension as avatar.
It also reads the directory automatically and gets only the files needed.

The weaknesses are that the script (at the moment) can output only png (gif/jpg... if you set it to) images.

So that's about it.
Dont be afraid to ask me questions about the script.
Hope you find it useful :)


Cheers, Friiks.


P.S.
Here's mine -
http://snowmoons.com/rand_avatar/image.png
Reload to see it change!



File Info
Downloads0
Ratingrating imagerating imagerating imagerating imagerating image
Comments
Samuraikill's Avatar
Samuraikill


Comments:7
Money:$0

Hey, that's pretty cool :) I didn't even know you could do that :)

24th September 2008 | Report
Friiks's Avatar
Friiks


Comments:204
Money:$1000

You can, and, as you see, it's pretty easy :)

24th September 2008 | Report