Author Topic: Avatar rotation script  (Read 3125 times)

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Avatar rotation script
« on: August 17, 2007, 11:35:03 AM »
Here is the script I use to do the avatar rotation.  It uses the caching function we put into SMF 2.0 so it doesn't have to recreate the file list everytime.  If you want to use it without SMF all you have to do is define your own cache_get_data and cache_put_data functions.  cache_get_data should return null if the cache doesn't exist or it is expired.

Essentially what the cache_ functions do is add a timestamp and serialize the data and put it into a file.  The read part looks for the file and reads it and then check the timestamp to see if it is still good.

I think some of the code is not used anymore since it now just redirects insteads of trying to create the image.

Code: [Select]
<?php
require_once('../forum/SSI.php');
$url 'http://www.geekoverloaded.com/avatars/';

$dir dir(dirname(__FILE__));

$imgtypes = array(
'.png' => 'imagecreatefrompng',
'.jpg' => 'imagecreatefromjpeg',
'.gif' => 'imagecreatefromgif',
);
if ((
$imgdata cache_get_data('avatar_rotation_image'180)) == null)
{
$imgs getFiles();
$img $imgs[rand(0count($imgs)-1)];
$imgsize getimagesize(dirname(__FILE__) . '/' $img);
$imgdata = array($img$imgsize);
cache_put_data('avatar_rotation_image'$imgdata180);
}
else
list ($img$imgsize) = $imgdata;
$ext substr($img, -4);

if (isset(
$_REQUEST['directory']))
{
echo '
<table>
<tr>
<td>File</td>
<td>Image</td>
</tr>'
;

if (!isset($imgs))
$imgs getFiles();
foreach($imgs AS $image)
{
echo '
<tr>
<td align="left">'
$image'</td>
<td align="center" valign="middle"><img src="'
$url$image'" alt="'$image'" /></td>
</tr>'
;
}
echo '
</table>'
;
exit;
}

header("Location: " $url $img);

function 
getFiles()
{
global $imgtypes;
// Try to shortcut
if (isset($_REQUEST['recache']) || ($imgs cache_get_data('avatar_rotation'43200)) == null)
{
$imgs = array();
$dir dir(dirname(__FILE__));
while (($entry $dir->read()) !== false)
{
$ext substr($entry, -4);
if (isset($imgtypes[$ext]))
$imgs[] = $entry;
}
$dir->close();
cache_put_data('avatar_rotation'$imgs43200);
}
return $imgs;
}
?>

Steve

  • This 49%er supports Romney
  • Just a Jackass
  • *
  • Posts: 16120
  • Karma: +31/-410
  • Mr. Mom
Re: Avatar rotation script
« Reply #1 on: August 17, 2007, 08:53:44 PM »
Safe to assume it wont work on the 1.1.3 release (since it using the 2.0 caching)?
hey ethic if you and i were both courting lily allen..... oh wait, which one of us has a relationship that lasted more than the bus ride home?

Canuck

  • Eh?!!
  • Founders
  • Posts: 792
  • Karma: +51/-3
  • Andy Moog Fan
    • My Website
Re: Avatar rotation script
« Reply #2 on: August 17, 2007, 09:08:23 PM »
I knew there had to be a reason why your avatar was chaning so constantly!

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Avatar rotation script
« Reply #3 on: August 17, 2007, 11:20:02 PM »
Safe to assume it wont work on the 1.1.3 release (since it using the 2.0 caching)?
Unless we put caching in 1.1 and I forgot about it then yea it won't work.  But really it isn't that hard to write your own caching functions.

Govtcheez

  • Town Idiot
  • Jackass In Charge
  • Posts: 4717
  • Karma: +9/-52
Re: Avatar rotation script
« Reply #4 on: August 17, 2007, 11:27:22 PM »
if username = "Queatrix"
user.delete

Steve

  • This 49%er supports Romney
  • Just a Jackass
  • *
  • Posts: 16120
  • Karma: +31/-410
  • Mr. Mom
Re: Avatar rotation script
« Reply #5 on: August 17, 2007, 11:29:01 PM »
Safe to assume it wont work on the 1.1.3 release (since it using the 2.0 caching)?
Unless we put caching in 1.1 and I forgot about it then yea it won't work.  But really it isn't that hard to write your own caching functions.

Thats what i thought, but i figured i would ask the stupid question before i made more code. Thanks :)
hey ethic if you and i were both courting lily allen..... oh wait, which one of us has a relationship that lasted more than the bus ride home?