barrytone Posted June 11, 2006 Posted June 11, 2006 Right, I was inspired by W4RP3D's thread on dynamic signatures to do something a little more simple... A random avatar generator. So I wrote a little bit of PHP to pick an image at random from the folder the file is in and display it. Like so: <?php //Random Avatar 0.1 - SBT 2006 //Image types to use in CSV list $imgtypes = "jpg,gif"; //Pull apart image types string $imgtypes = explode(",", $imgtypes); //Start the pattern string $imgpattern = "(.+."; //Put img types into pattern string $imgpattern .= implode(")|(.+.", $imgtypes); //End the pattern string $imgpattern .= ")"; //Open the current dir $thisdir = opendir("."); //Create array for image file names $images = array(); //Read the contents of the dir while($fn = readdir($thisdir)) { //Check for images. Add images to $images array if (filetype($fn) == "file") { if (eregi($imgpattern, $fn)) { $images[] = $fn; } } } //Get amount of images in dir $nimages = count($images); //Pick Random image $rimage = rand(0, $nimages); //Display it echo file_get_contents($images[$rimage]); //Close the dir closedir($thisdir); //Clear the stat cache clearstatcache(); ?> It works fine for the most part. But every now and again it outputs absolutely nothing. I'm at a loss as to why it would do this... So if anyone can see something wrong with the code (I've tried to comment it the best I can) then please let me know :) Oh... Incase anyone wonders: To allow me to use the url of the file on forums, I've set my server to parse .png files as php. I don't use any png files on my site, so it seemed convenient :P **EDIT** Dammit! I knew if I posted it I'd figure it out soon after :roll: I wasn't counting from zero. I should have my wrist slapped :P //Get amount of images in dir $nimages = count($images); Should read: //Get amount of images in dir, AND MINUS ONE $nimages = count($images); $nimages = $nimages - 1; Feel free to use it... Refesh the page to see it in action :P If anyone has any suggestions, please let me know :) Quote
Shaun Posted June 11, 2006 Posted June 11, 2006 Shouldn't you output the content type header? Firefox figures it out because it's in an img element, but if you go directly to the image in your browser it shows it as text. I bet some browsers might get confused and not show the image properly. Quote
barrytone Posted June 12, 2006 Author Posted June 12, 2006 Thanks anyedie :) Shouldn't you output the content type header? Firefox figures it out because it's in an img element, but if you go directly to the image in your browser it shows it as text. I bet some browsers might get confused and not show the image properly. Good idea! I've edited the code a little... Here's version 0.3 :P <?php //Random Avatar 0.3 - SBT 2006 //Image types to use in CSV list $imgtypes = "jpg,gif,png"; //Pull apart image types string $imgtypes = explode(",", $imgtypes); //Start the pattern string $imgpattern = "(.+."; //Put img types into pattern string $imgpattern .= implode(")|(.+.", $imgtypes); //End the pattern string $imgpattern .= ")"; //Open the current dir $thisdir = opendir("."); //Create array for image file names $images = array(); //Read the contents of the dir while($fn = readdir($thisdir)) { //Check for images. Add images to $images array if (filetype($fn) == "file") { if (eregi($imgpattern, $fn)) { //Get filename of this script $thisfn = explode("/", $_SERVER[SCRIPT_NAME]); $thisfn = $thisfn[count($thisfn) - 1]; //Check img is not this file if($fn != $thisfn) { $images[] = $fn; } } } } //Get amount of images in dir, minus 1. $nimages = count($images); $nimages = $nimages - 1; //Pick Random image $rimage = rand(0, $nimages); //Get the file type list($filename, $extension) = split(".", $fn); //Form header string if ($extension = "jpg") { $extension = "jpeg"; } $headstr = "Content-type: image/$extension"; //Send header header($headstr); //Send img echo file_get_contents($images[$rimage]); //Close the dir closedir($thisdir); //Clear the stat cache clearstatcache(); ?> Changes: -It now outputs the conent type to the header. Will work with jpg, gif and png -It now checks to make sure it's not trying to display itself -I've changed the apache extension handler to ".php.jpg application/x-httpd-php"... That way, I can save the file as avatar.php.jpg, it gets parsed as php, I can use .png images as avatars, forums allow me to use it as an avatar, and it doesn't affect the images I use on the rest of my site :) It can be used between tags too... Handy if you want a random image in your signature... Quote
gbjazzman Posted June 12, 2006 Posted June 12, 2006 So, if you were to use this on a message board, you'd put that link in the spot for your avatar, correct? Quote
barrytone Posted June 12, 2006 Author Posted June 12, 2006 So, if you were to use this on a message board, you'd put that link in the spot for your avatar, correct? Correct :) Most forums check the file extension of the avatar you're linking to though. So you'll have to make sure the server you're running the script on has a handler set to parse the file as PHP. so in my case I have one set as: .php.jpg application/x-httpd-php And then save the script as something like avatar.php.jpg A lot of web hosts let you add your own apache handlers. So you don't always need your own server use the script. Just a hosting account with a reasonable amount of features. Quote
Shaun Posted June 12, 2006 Posted June 12, 2006 Also if they don't you can still most likely use a redirect in .htaccess to redirect avatar.png to avatar.php for instance. Quote
barrytone Posted June 12, 2006 Author Posted June 12, 2006 Also if they don't you can still most likely use a redirect in .htaccess to redirect avatar.png to avatar.php for instance. I've not tried that personally, but it sounds like it would work :) Does that work like a redirection sort of thing, whereby: say I accessed avatar.png with a web browser, would the URL in my address bar change to avatar.php? Or does it work sort of like a symlink? Or neither? Quote
Shaun Posted June 12, 2006 Posted June 12, 2006 It's done in the browser, so the address bar would change, but because it's done in the actual HTTP headers rather than javascript or metatags in HTML like most people do redirects it works with any file type and in any context. Quote
barrytone Posted June 13, 2006 Author Posted June 13, 2006 It's done in the browser, so the address bar would change, but because it's done in the actual HTTP headers rather than javascript or metatags in HTML like most people do redirects it works with any file type and in any context. Cool :) I'll give it a try when I get a chance. Here's a question for you, given that you seem to know your stuff... :) Would it be possible to store image data in a mysql database? Quote
Shaun Posted June 13, 2006 Posted June 13, 2006 Yes, it is although doing it on a large scale isn't a great idea if I recall correctly. For something small like this it shouldn't be a problem. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.