Posted By

pitje on 09/02/07


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


rotating avatars


 / Published in: PHP
 

URL: http://www.poehey.nl/media/img/avatars/

have more than 1 avatar

  1. /*
  2. rotating avatars
  3. create a directory on your webserver, call it avatars and put a few avatars in it, along with this file. (index.php)
  4. ( make sure the avatars are about the same size, and preferably square ( width == height ) )
  5. check http://www.yourdomain.com/avatars/ to see the result. Refresh the page to see another avatar.
  6. */
  7.  
  8. function listImages ( )//reads images in current directory, returns array with filenames
  9. {
  10. $allowed_extensions = array ( ".jpg", ".png", ".gif" );
  11. $dir = opendir ( getcwd ( ) );
  12. while ( $file = readdir ( $dir ) )
  13. {
  14. $extension = strtolower ( substr ( $file, strlen ( $file ) - 4, strlen ( $file ) ) );
  15. if ( in_array ( $extension, $allowed_extensions ) ) $linklist[] = $file;
  16. }
  17. closedir ( $dir );
  18. return $linklist;
  19. }
  20. function mkImg ( $file, $ftype )//returns an image with filetype $ftype, sourceimage is $file
  21. {
  22. $img = imagecreatefromstring ( file_get_contents ( $file ) );
  23. switch ( $ftype )
  24. {
  25. case ".jpg":
  26. header('Content-Type: image/jpg');
  27. imagejpeg($img);
  28. break;
  29. case ".gif":
  30. header('Content-Type: image/gif');
  31. imagegif($img);
  32. break;
  33. case ".png":
  34. default:
  35. header('Content-Type: image/png');
  36. imagepng($img);
  37. break;
  38. }
  39. }
  40. //read the images in the current directory
  41. $images = listImages ( );
  42. //pick a random one
  43. $rnd = $images[array_rand ( $images )];
  44. //for use on forums -> link to /path/to/index.php?ftype=.jpg (fools SOME forum software into thinking it's a jpg)
  45. $ftype = $_REQUEST['ftype'] ? $_REQUEST['ftype'] : ".png";
  46. //return the randomly picked image
  47. mkImg ( $rnd, $ftype );

Report this snippet  

You need to login to post a comment.