Random Avatar »  Show posts from    to     

Icy Phoenix


Free PHP Scripts - Random Avatar



Mighty Gorgon [ Tue 25 May, 2010 20:45 ]
Post subject: 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:
  1. Create a folder called avatars 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.
  2. 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 $random_avatar_n = 699; to reflect the total amount.
  3. Create a new file called avatar.php, 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.: yoursite.com/avatars/avatar.php).
    Code: (avatar.php) [Hide] [Select]
    <?
    $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');
    ?>

  4. Alter your .htacces (in your root) by adding this line (change path and filenames accordingly):
    Code: [Hide] [Select]
    Redirect /avatar.png http://www.yoursite.com/avatars/avatar.php

  5. Go in your browser and just recall the avatar:
    Code: [Hide] [Select]
    Redirect /avatar.png http://www.yoursite.com/avatar.png


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


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

I hope you like this script!


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


nothere [ Sat 07 May, 2011 21:12 ]
Post subject: 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 ]
Post subject: 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 ]
Post subject: 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 ]
Post subject: Re: Random Avatar
Hello,
I apologize for the extremely late replay

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 ]
Post subject: 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 ]
Post subject: Re: Random Avatar
Hi Jimbo,

5 would be a safe bet I guess


cassiopeiastars9 [ Sat 22 Oct, 2011 06:28 ]
Post subject: 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



Joshua203 [ Sat 22 Oct, 2011 07:27 ]
Post subject: 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




Powered by Icy Phoenix