https://www.icyphoenix.com/viewtopic.php?f=33&t=7168
-----------------------------------
Mighty Gorgon
Tue 25 May, 2010 20:45

Random Avatar
-----------------------------------
I have created for a friend a very simple script which creates a random avatar to be used on any forum/blog software.

All you have to do is the following:
[list=1][*]Create a folder called [b]avatars[/b] in your site root with all the PNG images you want to use (I have forced this script to process PNG). Of course you can use any other name or path, but then you will need to change other references to it accordingly.
[*]Rename all the images with a consecutive number with leading zeroes (000001.png, 000002.png, 000003.png and so on...), all files names should be 6 chars long. Then change the number of images [b]$random_avatar_n = 699;[/b] to reflect the total amount.
[*]Create a new file called [b]avatar.php[/b], paste in it the code from the box here below and then copy the file it in the folder you just created with all images (i.e.: [b]yoursite.com/avatars/avatar.php[/b]).
[code filename=avatar.php linenumbers=false syntax=php]<?
$random_avatar_n = 699;
if (function_exists(mt_rand))
{
	$rand = mt_rand(1, $random_avatar_n);
}
else
{
	$rand = rand(1, $random_avatar_n);
}
header('Content-type: image/png');
header('Cache-Control: no-store, no-cache, no-transform, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0, max-age=0', false);
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Pragma: no-cache');
echo file_get_contents(str_pad($rand, 6, '0', STR_PAD_LEFT) . '.png');
?>[/code]
[*]Alter your .htacces (in your root) by adding this line (change path and filenames accordingly):
[codeblock]Redirect /avatar.png http://www.yoursite.com/avatars/avatar.php[/codeblock]
[*]Go in your browser and just recall the avatar:
[codeblock]Redirect /avatar.png http://www.yoursite.com/avatar.png[/codeblock][/list]

If you did everything correctly you should have something like this:
[codeblock]http://www.mightygorgon.com/avatar.png[/codeblock]

Once the page is loaded, just refresh it and the image should change... :wink:

I hope you like this script!


P.S.: I forgot to mention that you should carefully choose your images to not be too wide... :wink:


-----------------------------------
nothere
Sat 07 May, 2011 21:12

Re: Random Avatar
-----------------------------------
Hi,
Thank you for the script. I think it's really cool.

I have a question. Do I place the script in the overall_footer or overall_header ; neither? In the meantime, I'll mess around with this in WAMP. 

Thanks again,

Dan


-----------------------------------
Joshua203
Fri 13 May, 2011 09:23

Re: Random Avatar
-----------------------------------
Hi nothere,

If I understand MG's post correctly... neither.. according to point 3 you create a new file called avatar.php for the script


-----------------------------------
Mighty Gorgon
Sun 15 May, 2011 12:09

Re: Random Avatar
-----------------------------------
Instructions were not clear... I have adjusted the first post... let me know if everything is clear now.


-----------------------------------
nothere
Thu 14 Jul, 2011 07:18

Re: Random Avatar
-----------------------------------
Hello,
I apologize for the extremely late replay  :oops: 

However, I just wanted to let you know that the instructions worked very well.

And thank you so much for such a wonderful script.


-----------------------------------
Jimbo75
Fri 02 Sep, 2011 23:45

Re: Random Avatar
-----------------------------------
Can you clarify - sorry, I might be being a bit slow on the uptake here as I've only done a very limited amount of php before.

Point 2 has: "Then change the number of images $random_avatar_n = 699; to reflect the total amount."

So, if I have 5 avatar images: 000001.png, 000002.png, 000003.png, 000004.png, 000005.png
Would I change $random_avatar_n = 699 to:
$random_avatar_n = 05, or
$random_avatar_n = 5


-----------------------------------
Joshua203
Sat 03 Sep, 2011 11:38

Re: Random Avatar
-----------------------------------
Hi Jimbo,

5 would be a safe bet I guess :wink:


-----------------------------------
cassiopeiastars9
Sat 22 Oct, 2011 06:28

Re: Random Avatar
-----------------------------------
Hi, MG....

do you have any specific max pixel size or kb size perhaps.... so the image still can be loaded?

thanks anyway  :)  :)  :) 

 :thanks:  :thanks:  :thanks:


-----------------------------------
Joshua203
Sat 22 Oct, 2011 07:27

Re: Random Avatar
-----------------------------------
Since you are the one in charge of which images are uploaded it would be easiest to just make sure they do not exceed the smallest limit of where you will use them.

Maybe even shrink them or blow them up a bit before uploading them to make them all the same size  :wink:


