rotating avatars


/ Published in: PHP
Save to your folder(s)

have more than 1 avatar


Copy this code and paste it in your HTML
  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. switch ( $ftype )
  23. {
  24. case ".jpg":
  25. header('Content-Type: image/jpg');
  26. imagejpeg($img);
  27. break;
  28. case ".gif":
  29. header('Content-Type: image/gif');
  30. imagegif($img);
  31. break;
  32. case ".png":
  33. default:
  34. header('Content-Type: image/png');
  35. imagepng($img);
  36. break;
  37. }
  38. }
  39. //read the images in the current directory
  40. $images = listImages ( );
  41. //pick a random one
  42. $rnd = $images[array_rand ( $images )];
  43. //for use on forums -> link to /path/to/index.php?ftype=.jpg (fools SOME forum software into thinking it's a jpg)
  44. $ftype = $_REQUEST['ftype'] ? $_REQUEST['ftype'] : ".png";
  45. //return the randomly picked image
  46. mkImg ( $rnd, $ftype );

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.