Random Avatar

Tags And Keywordsfree php scripts, random avatar, random image


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) [Download] [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: [Download] [Hide] [Select]
    Redirect /avatar.png http://www.yoursite.com/avatars/avatar.php

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


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


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:

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

Profile PM  
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

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

Subject: 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.

Profile PM  
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

Subject: Re: Random Avatar
Hi Jimbo,

5 would be a safe bet I guess :wink:

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 :) :) :)

:thanks: :thanks: :thanks:

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 :wink:


Page 1 of 1


  
You cannot post new topics
You cannot reply to topics
You cannot edit your posts
You cannot delete your posts
You cannot vote in polls
You cannot attach files
You can download files
You cannot post calendar events

   

This is a "Lo-Fi" version of our main content. To view the full version with more information, formatting and images, please click here.

Powered by Icy Phoenix based on phpBB
Generation Time: 0.5386s (PHP: 4% SQL: 96%)
SQL queries: 31 - Debug Off - GZIP Enabled