Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - [PHP] Help: Advanced Random Image Display Script
  
User name:
Password:
Reply:
 

UserPost
sloat
Posts: 26/85
You can do this instead of using array_merge:


$images = glob("{*.jpg,*.gif,*.png}", GLOB_BRACE);
Gywall
Posts: 306/356
Originally posted by windwaker
Yeah, so you'd go...


$files = array_merge (
glob('*.jpg'),
glob('*.png')
);

$result = count($files);
$result = $result - 1;
$random = rand(0, $result);
$theimage = $files['$random'];



Simple code but must be placed in the same folder as the images:
<?php
$files=array_merge (glob('*.jpg'),glob('*.png'),glob('*.gif'));
$result = count($files);
if($result=="0") die(Error - No images found);
$random = rand(1, $result);
if(substr($files[$random],-3)=="png") Header("Content-type:image/png");
if(substr($files[$random],-3)=="gif") Header("Content-type:image/gif");
if(substr($files[$random],-3)=="jpg") Header("Content-type:image/jpeg");
readfile($files[$random]);
?>
Elric
Posts: 307/687
I'm now totally confused. Conglaturation! A winner is you!
Vystrix Nexoth
Posts: 203/348
or, more simply:
$file = $files[array_rand($files)];
windwaker
Posts: 788/1797
Yeah, so you'd go...


$files = array_merge (
glob('*.jpg'),
glob('*.png')
);

$result = count($files);
$result = $result - 1;
$random = rand(0, $result);
$theimage = $files['$random'];
Vystrix Nexoth
Posts: 201/348
it lets you use wildcards too.
$files = array_merge (
glob('*.jpg'),
glob('*.png')
);

...and it contains a list of each JPG and PNG file in the directory of the script.
windwaker
Posts: 785/1797
Why don't you just use glob()? It selects all the files in a directory and puts them in an array.
Zem
Posts: 489/1107
I could try. I have not encountered such a phenomenon before. Go ahead and post it, or PM, or upload it somewhere, or contact me on AIM (bsznm), or whatever. I would be very interested in finding out how it might be done.

EDIT: not on AIM for the rest of the night. Also, glob() sounds useful. I'm rescripting thecaw at a rate of maybe one function per two weeks, so maybe when I get back to the file management areas in a few hundred years I can make use of that. =D
Elric
Posts: 306/687
Thanks Zem. I'll try that.

And Ogre gave me the script he created for multi-rank sets for AcmlmBoard v1.8a, which loaded a different set of images based on your sex and the job you chose in your profile. However, I was somehow able to make it show a random rank image for admins, and every post I made in a thread showed a different image for my rank. So, for example, if I had 3 posts in a thread, it showed 3 different images.

I've looked at the code, and since most of it isn't mine, and I haven't touched it in months, I have no clue how I did it. I can send you the file to look it over. Maybe you can figure out how it was done...

Edit
Well, unless I did something wrong, that didn't seem to work. I added that to my script file, and commented out the array with the images defined in it. I also commented out the $baseurl line, and changed the last line to readfile($files[$random]);. Then I uploaded the file, put it into an <img> tag, and got a broken image. Any idea what I did wrong?
Zem
Posts: 487/1107
Here is how to read everything in a given directory into an array:
$files = array();
if($dir = @opendir('guessWhatGoesHere')) {
while(($file = readdir($dir)) !== false) { // it returns false at the end of the dir
if($file != "." && $file != "..") { // it also comes up with these, which we don't want
$files[] = $file;
}
}
}

For picking one out at random, use $files[array_rand($files)] (note that array_rand() returns a random key from the array, not the element itself).

And no browser I know will request an image more than once per page, even if told not to cache it, so it'll always be the same per pageview.
Elric
Posts: 305/687
Right now, I have a script I use for my Avatar that randomly selects an image from an array, and displays it.

I want to make an "advanced" version that will read the images in a directory I specify in the file, load them into an array, and randomly select one. The problem is, I have no idea how to make it read the contents of a directory (including any sub directories) into an array.

Here's the current code I use:

<?php

$files=array("aylaeatfrog.gif",
"elfricav.gif",
"elricav.gif",
"mort.png",
"skelricav.gif",
"phoenix3.gif",
"setzer.gif",
"bg.gif");

$baseurl="http://eds.castledragmire.com/pix/";
$random=rand(0,count($files)-1);

if(substr($files[$random],-3)=="png") Header("Content-type:image/png");
if(substr($files[$random],-3)=="gif") Header("Content-type:image/gif");
if(substr($files[$random],-3)=="jpg") Header("Content-type:image/jpeg");
readfile($baseurl.$files[$random]);

?>

I put the URL to the PHP file into the textarea in my profile where it asks for my avatar, so it gets turned into <img src=http://eds.castledragmire.com/board/ava.php>.

I'd also like it to show a random image each time it's called on a page, instead of only choosing an image once, and displaying that for all instances (which is what my avatar does now), but I can live with it not doing that.

If anyone can help, I would appreciate it.
Acmlm's Board - I2 Archive - Programming - [PHP] Help: Advanced Random Image Display Script


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.012 seconds.