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 | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Elric

Chasupa


Currently Playing:
You Like A Lute.
Level: 40

Posts: 305/687
EXP: 440016
For next: 1293

Since: 03-15-04
From: Melniboné

Since last post: 6 hours
Last activity: 6 hours
Posted on 12-24-04 09:42 AM Link | Quote
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.
Zem
You can be civil without being flowery, dipshits.
Level: 49

Posts: 487/1107
EXP: 829398
For next: 54485

Since: 06-13-04

Since last post: 131 days
Last activity: 131 days
Posted on 12-24-04 09:58 AM Link | Quote
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.


(edited by Zem on 12-24-04 01:01 AM)
Elric

Chasupa


Currently Playing:
You Like A Lute.
Level: 40

Posts: 306/687
EXP: 440016
For next: 1293

Since: 03-15-04
From: Melniboné

Since last post: 6 hours
Last activity: 6 hours
Posted on 12-24-04 10:43 AM Link | Quote
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?


(edited by Elric on 12-24-04 01:53 AM)
Zem
You can be civil without being flowery, dipshits.
Level: 49

Posts: 489/1107
EXP: 829398
For next: 54485

Since: 06-13-04

Since last post: 131 days
Last activity: 131 days
Posted on 12-24-04 10:55 AM Link | Quote
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


(edited by Zem on 12-24-04 02:46 AM)
windwaker

Ball and Chain Trooper
WHY ALL THE MAYONNAISE HATE
Level: 61

Posts: 785/1797
EXP: 1860597
For next: 15999

Since: 03-15-04

Since last post: 4 days
Last activity: 6 days
Posted on 12-24-04 11:03 AM Link | Quote
Why don't you just use glob()? It selects all the files in a directory and puts them in an array.
Vystrix Nexoth

Level: 30

Posts: 201/348
EXP: 158678
For next: 7191

Since: 03-15-04
From: somewhere between anima and animus

Since last post: 3 days
Last activity: 2 days
Posted on 12-24-04 09:23 PM Link | Quote
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

Ball and Chain Trooper
WHY ALL THE MAYONNAISE HATE
Level: 61

Posts: 788/1797
EXP: 1860597
For next: 15999

Since: 03-15-04

Since last post: 4 days
Last activity: 6 days
Posted on 12-24-04 09:56 PM Link | Quote
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

Level: 30

Posts: 203/348
EXP: 158678
For next: 7191

Since: 03-15-04
From: somewhere between anima and animus

Since last post: 3 days
Last activity: 2 days
Posted on 12-25-04 04:19 AM Link | Quote
or, more simply:
$file = $files[array_rand($files)];
Elric

Chasupa


Currently Playing:
You Like A Lute.
Level: 40

Posts: 307/687
EXP: 440016
For next: 1293

Since: 03-15-04
From: Melniboné

Since last post: 6 hours
Last activity: 6 hours
Posted on 12-27-04 08:38 AM Link | Quote
I'm now totally confused. Conglaturation! A winner is you!
Gywall

Silver axe
Level: 30

Posts: 306/356
EXP: 164069
For next: 1800

Since: 03-15-04
From: In front of my moniter, where you're not!

Since last post: 104 days
Last activity: 14 hours
Posted on 12-27-04 03:48 PM Link | Quote
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]);
?>



(edited by Gywall on 12-27-04 07:08 AM)
(edited by Gywall on 12-27-04 07:09 AM)
(edited by Gywall on 12-27-04 07:09 AM)
sloat

Level: 16

Posts: 26/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 12-27-04 10:38 PM Link | Quote
You can do this instead of using array_merge:


$images = glob("{*.jpg,*.gif,*.png}", GLOB_BRACE);
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
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.018 seconds.