Original message (1794 Views )
| Replies: |
Undead Fred 2585th Post
Platinum Carpet V.I.P- Board Master
| "Re(1):Undead Fred, question" , posted Tue 13 Dec 09:29
quote: How was that thing about random images in av or tag done?
I had asked around here a while back on how to do it... I think CharlesProphony was the one that posted this code:
<?php
//uberRandom Image Script, //by Ryan Lukasik (ryan@uberpolak.tk), Jan. 12, 2003 //Free to use, just please leave this block intact. //Call this script with <IMG SRC="whateveryousavethisas.php">
//"images.dat" will contain the name of each image file $imgs = file("images.dat");
header("Location: " . $imgs[rand(0,sizeof($imgs) - 1)]);
?>
Open Notepad or some other simple text program and paste this in. Save it as "randimg.php", then create another file called "images.dat", where you'll place all of the filenames for the pictures you want randomized. Then place them in a directory on a server with the images that you want randomized and (provided the server supports PHP), it should work just fine. You'll link directly to the PHP file in for avatar or tag in your profile here.
As an example, this is how mine works: I made a directory called "tags" or something like that, opened Notepad, dropped in the code and saved it to "randimg.php". Then I made another file with just "undeadtag16.gif" up to "undeadtag19.gif" or whatever I called the files listed out and saved that as "images.dat" in the same directory as the randimg.php file. Then I put randimg.php, images.dat, and all of the tags together in the same folder online and put the link to the PHP file as the source for my tags here at the Cafe. Let me know if you try it and have any problems.
|
CharlesProphony 15th Post
New Customer
| "Re(2):Undead Fred, question" , posted Wed 14 Dec 00:16
I gave another code that reads the directory, without the need of another file indicating the list of images, but I got to use this script in other forums and some forum types(phpbb, iB, etc) doesn't accept avs or signs using different extensions other than jpg, gif, png... thus refusing the .php script or giving errors, then I came with this thing I've read before somewhere and created a dir called "blablabla.gif" and put the script in and named it as index.php... with some tweaking like indicating the type of the file with a new header and voilá! it works! the code :
<?php //change "$dir_imgs" to the name of directory where your images are $dir_imgs="./imgs/"; $dir_handler = @opendir($dir_imgs); //this part will read the directory while ($file = readdir($dir_handler)) { if ($file!="." && $file!="..") { $data[]=$file; } } closedir($dir_handler);
//the next piece of code detects the type of image looking at the extension... nothing more... well, just more lazyness :P switch (substr($data[array_rand($data)], -3,3)) { case "gif" : $imgtype=="gif"; break; case "jpg" : $imgtype=="jpeg"; break; case "jpeg" : $imgtype=="jpeg"; break; case "png" : $imgtype=="png"; break; } //the header that will redirect the browser to the random file in the "$dir_imgs" directory header("Content-type: image/".$imgtype); header("Location: ./".$dir_imgs."/".$data[array_rand($data)]); ?>
now some explanations about the code... because of the name of the directory ("blablabla.gif") some browsers may get somewhat crazy with the type of images(like "why it's identified as a gif and read as a jpeg?") but as I tested in the firefox, nothing gone bad... if you have any problems or questions, just post here, I'm entering every day... just honouring my fame as a lurker
just improving my english...
|
|
|