Random Thumbnail Script

I need a PHP script that can be invoked refererencing a
subdirectory where thumbnails are stored and have it randomly display
one of those thumbnails.

To expand upon my needs, here is the hierarchy of the Web
site:

main pages public_html

subpages apple banana coconut

sub-subpages aa bb cc dd ee ff gg

Each of the aa-gg subdirectories will contain thumbnail .jpg
files. I'd like to be able to invoke the script quite a few times
within the same PHP page.

Example:

From the page in /banana, use <?php random_image $dir=aa> to
return a thumbnail that it randomly took from sub page /banana/aa. I
would invoke a thumbnail from another directory by changing aa to bb,
cc, dd, ee, ff, or gg.

So far, every script I've looked at I haven't quite been able
to make work.

Though I have written a few PHP scripts, I am a PHP novice and
I need to 'keep it simple' so I can repeatedly use the script in the
page.

If the script is above the <html> tag and can be called from
within the actual page, that should work fine for my purposes.

Thanks for any help you can provide.

Regards,



Fred
Fred Atkinson [ Fr, 02 März 2007 20:20 ] [ ID #1645801 ]

Re: Random Thumbnail Script

| To expand upon my needs ...

hmmm...the need to google and rtfm?

| main pages public_html
|
| subpages apple banana coconut
|
| sub-subpages aa bb cc dd ee ff gg
|
| Each of the aa-gg subdirectories will contain thumbnail .jpg
| files. I'd like to be able to invoke the script quite a few times
| within the same PHP page.
|
| Example:
|
| From the page in /banana, use <?php random_image $dir=aa> to
| return a thumbnail that it randomly took from sub page /banana/aa. I
| would invoke a thumbnail from another directory by changing aa to bb,
| cc, dd, ee, ff, or gg.

i call upon the almighty zeus, mythros, jesus christ, and budah. i summon
your powers and invoke the thumbnail!

how the fuck do you invoke a thumbnail?!!!

anyway, that's a rediculous way to do it...but, ok.

| So far, every script I've looked at I haven't quite been able
| to make work.

and the problem is the script? and you hope the script we give you will
likewise, "not work"?

| Though I have written a few PHP scripts, I am a PHP novice and
| I need to 'keep it simple' so I can repeatedly use the script in the
| page.

look up the functions those scripts used. then go and do something
remarkable...think. then find the manual. read it. 'fix' the script(s) you
have and your problems are gone, AND you have learned something in the
process.

| If the script is above the <html> tag and can be called from
| within the actual page, that should work fine for my purposes.

uh...you need to get a beginners book. just like javascript, you can execute
php code anywhere you wish. unlike javascript, php will be processed on the
server before the client ever gets it.

| Thanks for any help you can provide.

i'd mail you a manual, but it's already available online at php.net.
Steve [ Fr, 02 März 2007 20:37 ] [ ID #1645802 ]

Re: Random Thumbnail Script

You are everything that is bad about USENET.


On Fri, 2 Mar 2007 13:37:18 -0600, "Steve" <no.one [at] example.com> wrote:

>| To expand upon my needs ...
>
>hmmm...the need to google and rtfm?
>
>| main pages public_html
>|
>| subpages apple banana coconut
>|
>| sub-subpages aa bb cc dd ee ff gg
>|
>| Each of the aa-gg subdirectories will contain thumbnail .jpg
>| files. I'd like to be able to invoke the script quite a few times
>| within the same PHP page.
>|
>| Example:
>|
>| From the page in /banana, use <?php random_image $dir=aa> to
>| return a thumbnail that it randomly took from sub page /banana/aa. I
>| would invoke a thumbnail from another directory by changing aa to bb,
>| cc, dd, ee, ff, or gg.
>
>i call upon the almighty zeus, mythros, jesus christ, and budah. i summon
>your powers and invoke the thumbnail!
>
>how the fuck do you invoke a thumbnail?!!!
>
>anyway, that's a rediculous way to do it...but, ok.
>
>| So far, every script I've looked at I haven't quite been able
>| to make work.
>
>and the problem is the script? and you hope the script we give you will
>likewise, "not work"?
>
>| Though I have written a few PHP scripts, I am a PHP novice and
>| I need to 'keep it simple' so I can repeatedly use the script in the
>| page.
>
>look up the functions those scripts used. then go and do something
>remarkable...think. then find the manual. read it. 'fix' the script(s) you
>have and your problems are gone, AND you have learned something in the
>process.
>
>| If the script is above the <html> tag and can be called from
>| within the actual page, that should work fine for my purposes.
>
>uh...you need to get a beginners book. just like javascript, you can execute
>php code anywhere you wish. unlike javascript, php will be processed on the
>server before the client ever gets it.
>
>| Thanks for any help you can provide.
>
>i'd mail you a manual, but it's already available online at php.net.
>
Fred Atkinson [ Fr, 02 März 2007 22:58 ] [ ID #1645803 ]

Re: Random Thumbnail Script

On 2 Mar, 19:20, Fred Atkinson <fatkin... [at] mishmash.com> wrote:
> I need a PHP script that can be invoked refererencing a
> subdirectory where thumbnails are stored and have it randomly display
> one of those thumbnails.
>
> To expand upon my needs, here is the hierarchy of the Web
> site:
>
> main pages public_html
>
> subpages apple banana coconut
>
> sub-subpages aa bb cc dd ee ff gg
>
> Each of the aa-gg subdirectories will contain thumbnail .jpg
> files. I'd like to be able to invoke the script quite a few times
> within the same PHP page.
>
> Example:
>
> From the page in /banana, use <?php random_image $dir=aa> to
> return a thumbnail that it randomly took from sub page /banana/aa. I
> would invoke a thumbnail from another directory by changing aa to bb,
> cc, dd, ee, ff, or gg.
>
> So far, every script I've looked at I haven't quite been able
> to make work.
>
> Though I have written a few PHP scripts, I am a PHP novice and
> I need to 'keep it simple' so I can repeatedly use the script in the
> page.
>
> If the script is above the <html> tag and can be called from
> within the actual page, that should work fine for my purposes.
>
> Thanks for any help you can provide.
>
> Regards,
>
> Fred

I'm feeling a teeeny bit more generous than Steve, but you have a few
basic terminology odds and ends to sort out before people will start
"handing you" scripts, as we all know you'll be straight back with "it
doesnt uite work" until we've spent far too much time on it, when
there are millions of scripts that do this out there already.

You need to be structured about this, what are the thumbnails called.
If they are random go find a script that can build an array of
filesnames from any given directory. That will be 3 or 4 lines long.
Hoever if you know they are called thumb_001.jpg thumb_002.jpg then
its easier because we know beforehand what's in the subDIRECTORIES
(not subpages)

then you need to make 2 more arrays,

$arrSubDirs = array('bananas','oranges','apples');
$arrSubSubDirs = array('aa','bb','cc','dd','ee');
this again only works if you know ahead of time what they are and it
doesnt keep changing, otherwise the same script that you got to show
you a list of files inside a directory can be slightly modified to
show only directories, see the manual for examples.

next you need to build this into a function, which outputs the html
src URL to go into the <img> tag and into the width and height

Now how many times are you going to call this function , 5, 10, 20
times, it will slow things down, how many files are there in these
directories, 100, 1000, 10000000,
you want to go in there ONCE and grab a random selection, not repeated
go in there and get one each time, (or you might end up with a repeat
and have to code for that case...)
If yuo have say 100 thumbs go into dir, grab all the thumbs of
type .jpg into an array,
$arrayThumbDetails['path_and_name'][] = 'the filenames you get';
and shuffle() the array, pick out the first however many you might
need for your web page, the stat() them to get their width and height,
and store them in the same array
$arrayThumbDetails['width'][] = 'the width that stat returned';
$arrayThumbDetails['height'][] = 'the height that stat returned';


your function must therefore take in as values,
the sub and subsub folder, the number of images you want back
and must return an array of the relative path to the img names with
the folders in front (like /orange/aa/thumb_asdd.jpg
, widths and heights.
so you would do this
$arrayImageDetails = getImageDetails('orange','aa',10);

Now you have a random array of 10 images with the widths and heights,
you just need to get that into html form so you put this in the html


<img
src="<?php echo array_pop($arrayImageDetails['path_and_name']); ?>"
height="<?php echo array_pop($arrayImageDetails['height']); ?>"
width="<?php echo array_pop($arrayImageDetails['width']); ?>"
alt="<?php echo
basename(array_pop($arrayImageDetails['path_and_name'])); ?>"
/>

for each image, making sure that when you call the function at the top
of the page you return at least as many image properties (we called
10) as you will need html img tags.

now you understand this isnt the most elegant or fast way, its just a
way I thought you might understand, if you are a newbie, you could do
worse - as steve pointed out graphically - that go get yourself a copy
of the downloadable php extended manual for windows (I assume you are
a windows user!) which has other php programmers helpful comments
inside, and borrow some of the example code from this manual, good
luck.
shimmyshack [ Fr, 02 März 2007 23:24 ] [ ID #1645804 ]

Re: Random Thumbnail Script

On 2 Mar 2007 14:24:12 -0800, "shimmyshack" <matt.farey [at] gmail.com>
wrote:

>I'm feeling a teeeny bit more generous than Steve, but you have a few
>basic terminology odds and ends to sort out before people will start
>"handing you" scripts, as we all know you'll be straight back with "it
>doesnt uite work" until we've spent far too much time on it, when
>there are millions of scripts that do this out there already.
>
>You need to be structured about this, what are the thumbnails called.
>If they are random go find a script that can build an array of
>filesnames from any given directory. That will be 3 or 4 lines long.
>Hoever if you know they are called thumb_001.jpg thumb_002.jpg then
>its easier because we know beforehand what's in the subDIRECTORIES
>(not subpages)
>
>then you need to make 2 more arrays,
>
>$arrSubDirs = array('bananas','oranges','apples');
>$arrSubSubDirs = array('aa','bb','cc','dd','ee');
>this again only works if you know ahead of time what they are and it
>doesnt keep changing, otherwise the same script that you got to show
>you a list of files inside a directory can be slightly modified to
>show only directories, see the manual for examples.
>
>next you need to build this into a function, which outputs the html
>src URL to go into the <img> tag and into the width and height
>
>Now how many times are you going to call this function , 5, 10, 20
>times, it will slow things down, how many files are there in these
>directories, 100, 1000, 10000000,
>you want to go in there ONCE and grab a random selection, not repeated
>go in there and get one each time, (or you might end up with a repeat
>and have to code for that case...)
>If yuo have say 100 thumbs go into dir, grab all the thumbs of
>type .jpg into an array,
>$arrayThumbDetails['path_and_name'][] = 'the filenames you get';
>and shuffle() the array, pick out the first however many you might
>need for your web page, the stat() them to get their width and height,
>and store them in the same array
>$arrayThumbDetails['width'][] = 'the width that stat returned';
>$arrayThumbDetails['height'][] = 'the height that stat returned';
>
>
>your function must therefore take in as values,
>the sub and subsub folder, the number of images you want back
>and must return an array of the relative path to the img names with
>the folders in front (like /orange/aa/thumb_asdd.jpg
>, widths and heights.
>so you would do this
>$arrayImageDetails = getImageDetails('orange','aa',10);
>
>Now you have a random array of 10 images with the widths and heights,
>you just need to get that into html form so you put this in the html
>
>
><img
>src="<?php echo array_pop($arrayImageDetails['path_and_name']); ?>"
>height="<?php echo array_pop($arrayImageDetails['height']); ?>"
>width="<?php echo array_pop($arrayImageDetails['width']); ?>"
>alt="<?php echo
>basename(array_pop($arrayImageDetails['path_and_name'])); ?>"
>/>
>
>for each image, making sure that when you call the function at the top
>of the page you return at least as many image properties (we called
>10) as you will need html img tags.
>
>now you understand this isnt the most elegant or fast way, its just a
>way I thought you might understand, if you are a newbie, you could do
>worse - as steve pointed out graphically - that go get yourself a copy
>of the downloadable php extended manual for windows (I assume you are
>a windows user!) which has other php programmers helpful comments
>inside, and borrow some of the example code from this manual, good
>luck.

And you certainly were much more couteous and respectful.
Thank you.

I wasn't expecting anyone to write all of that though I do
appreciate it. I was just hoping someone would know of a free or an
inexpensive pay script to solve my problem and give me a URL or
something to find it.

I'll look your coding over. Maybe I can figure it out well
enough to code it into my page. .

Regards, and thanks again,



Fred
Fred Atkinson [ Sa, 03 März 2007 04:10 ] [ ID #1646542 ]

Re: Random Thumbnail Script

"Fred Atkinson" <fatkinson [at] mishmash.com> wrote in message
news:8g7hu2p64j3924uos578ktgfflahnt07sv [at] 4ax.com...
| You are everything that is bad about USENET.

;^)
Steve [ Sa, 03 März 2007 04:16 ] [ ID #1646543 ]

Re: Random Thumbnail Script

Hello,
you can also use MySQL for this, which is probably better for you,
since you might not want to handle so many arrays.

* This is not a raw MySQL command, just the structure of the table for
YOU to make on your own, using a manual, perhaps. :P

1) Create the thimbnails table:
table thumbnails: id, alt, src, name, sub ;
If you are not sure about creating the table, then I will show you how
to.

2) Then, back in php, fetch a query and load the results onto an
array, as shimmyshack, basically.

$result = mysql_query(/* please */"SELECT id AS ID,alt AS ALT,src AS
SRC,name AS NAME, sub AS SUB FROM thumbnails");
while($row = mysql_fetch_array($result)) {
if($row[SUB] == 0) {
$thumbs[$row[ID]] = ("<img alt='$row[ALT]' src='$row[SRC]'
name='$row[NAME]'/>" => "main");
}
elseif($row[SUB] == 1) {
$thumbs[$row[ID]]
}
elseif($row[SUB] == 2) {

}
}

Dunno, it's un-finished, bugged, and it's way too annoying to continue
with it...
I really don't know how the heck you came-up with it, and why in the
form you have suggested.
If you still insist that all you wanted is a pre-made script then
please read the following.
a) GO TO HELL, I HAD TO GET MY MOMMY UPSET FOR MAKING THIS, SINCE SHE
MADE LUNCH.
b) GO TO HELL, WHY DID YOU POST IT ON GOOGLE GROUPS AND WHY DIDN'T YOU
MENTION THAT ALL YOU WANTED IS A SCRIPT BEFORE SHIIMMYSHACK HAS POSTED
LIKE 200 LINES OF POST!
c) Anyways, Steve: hear, hear!!


Dearly,
Shaffer.
Shaffer [ Sa, 03 März 2007 09:57 ] [ ID #1646547 ]

Re: Random Thumbnail Script

Hello,
you can also use MySQL for this, which is probably better for you,
since you might not want to handle so many arrays.

* This is not a raw MySQL command, just the structure of the table
for
YOU to make on your own, using a manual, perhaps. :P


1) Create the thimbnails table:
table thumbnails: id, alt, src, name, sub ;
If you are not sure about creating the table, then I will show you
how
to.


2) Then, back in php, fetch a query and load the results onto an
array, as shimmyshack, basically.


$result = mysql_query(/* please */"SELECT id AS ID,alt AS ALT,src AS
SRC,name AS NAME, sub AS SUB FROM thumbnails");
while($row = mysql_fetch_array($result)) {
if($row[SUB] == 0) {
$thumbs[$row[ID]] = ("<img alt='$row[ALT]' src='$row[SRC]'
name='$row[NAME]'/>" => "main");
}
elseif($row[SUB] == 1) {
$thumbs[$row[ID]]
}
elseif($row[SUB] == 2) {


}



}


Dunno, it's un-finished, bugged, and it's way too annoying to
continue
with it...
I really don't know how the heck you came-up with it, and why in the
form you have suggested.
If you still insist that all you wanted is a pre-made script then
please read the following.
a) GO TO HELL, I HAD TO GET MY MOMMY UPSET FOR MAKING THIS, SINCE SHE
MADE LUNCH.
b) GO TO HELL, WHY DID YOU POST IT ON GOOGLE GROUPS AND WHY DIDN'T
YOU
MENTION THAT ALL YOU WANTED IS A SCRIPT BEFORE SHIIMMYSHACK HAS
POSTED
LIKE 200 LINES OF POST!?
c) Anyways, Steve: hear, hear!!

Dearly,
Shaffer.
Shaffer [ Sa, 03 März 2007 10:09 ] [ ID #1646548 ]

Re: Random Thumbnail Script

Message-ID: <fjohu29bo3enigbupm4npjmino161i2g86 [at] 4ax.com> from Fred
Atkinson contained the following:

> I'll look your coding over. Maybe I can figure it out well
>enough to code it into my page. .

If the data is structured in an organised way it will be easier to
extract in the way you want (even if that way is random).

Getting a random number $z (let's not get into a discussion about true
randomness kiddies) is easy. Say you wanted a random number between $x
and $y. You just do $z=rand($x,$y);

Now you can see that if images are coded with a sequential number as the
name it would be easy to do

<img src="image<?php echo $z;?> alt="">

However, chances are they won't be. Arrays however, can have numeric
keys.

So if you have an array like this

$array[]=cat.jpg;
$array[]=dog.jpg;
$array[]=fish.jpg;
$array[]=goat.jpg;

PHP will assign numeric keys to the array, starting at zero

The number of keys is derived from count($array). So the last key is
Applying that to the above we get

$z=rand(0,(count($array)-1));
(aside - I know about shuffle folks - this is easier to explain)

and you output it like this
<img src="image<?php echo $array[$z];?> alt="">


Now your only problem is getting the contents of a given directory into
an array. For this you have to loop through the contents of a directory
to find all the image files and assign them to the array.

See my gallery script for an idea of how this is done
www.walkingoutdoors.co.uk/Geoff/gallery/gallery.zip

example:
http://www.walkingoutdoors.co.uk/Geoff/tryfan/

--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Geoff Berrow [ Sa, 03 März 2007 11:21 ] [ ID #1646549 ]

Re: Random Thumbnail Script

Folks,

I found another script that does the job. It needs a little
tweaking, but the code is simple enough that I can do that without too
much trouble.

The script I found is at:
http://www.theprojects.org/scripts/Get_Image_List/ in case you'd like
to have a look at it.

Regards,



Fred
Fred Atkinson [ Sa, 03 März 2007 16:20 ] [ ID #1646552 ]

Re: Random Thumbnail Script

"Shaffer" <gnrhapr [at] gmail.com> wrote in message
news:1172912253.921308.309240 [at] z35g2000cwz.googlegroups.com.. .
| Hello,
| you can also use MySQL for this, which is probably better for you,
| since you might not want to handle so many arrays.
|
| * This is not a raw MySQL command, just the structure of the table for
| YOU to make on your own, using a manual, perhaps. :P
|
| 1) Create the thimbnails table:
| table thumbnails: id, alt, src, name, sub ;
| If you are not sure about creating the table, then I will show you how
| to.
|
| 2) Then, back in php, fetch a query and load the results onto an
| array, as shimmyshack, basically.
|
| $result = mysql_query(/* please */"SELECT id AS ID,alt AS ALT,src AS
| SRC,name AS NAME, sub AS SUB FROM thumbnails");
| while($row = mysql_fetch_array($result)) {
| if($row[SUB] == 0) {
| $thumbs[$row[ID]] = ("<img alt='$row[ALT]' src='$row[SRC]'
| name='$row[NAME]'/>" => "main");
| }
| elseif($row[SUB] == 1) {
| $thumbs[$row[ID]]
| }
| elseif($row[SUB] == 2) {
|
| }
| }
|
| Dunno, it's un-finished, bugged, and it's way too annoying to continue
| with it...
| I really don't know how the heck you came-up with it, and why in the
| form you have suggested.
| If you still insist that all you wanted is a pre-made script then
| please read the following.
| a) GO TO HELL, I HAD TO GET MY MOMMY UPSET FOR MAKING THIS, SINCE SHE
| MADE LUNCH.
| b) GO TO HELL, WHY DID YOU POST IT ON GOOGLE GROUPS AND WHY DIDN'T YOU
| MENTION THAT ALL YOU WANTED IS A SCRIPT BEFORE SHIIMMYSHACK HAS POSTED
| LIKE 200 LINES OF POST!
| c) Anyways, Steve: hear, hear!!

lol. actually, roflmfao! holy cow, i knew my response was a little
hash...but wow! ;^)

i suppose you and i have both seen enough of these kinds of posts to know
that the op is winking as he types, 'i really just need a pointer here and
there because i'm so new'. i think the giveaway is that it is obvious, to me
anyway, they want access to the author of the 'example' so there is less for
them to do when they find the *script* doesn't work. why is reading, trial,
and error such an afront to people? i mean, we aren't making stuff with an
enigma machine they have to crack to understand. christ, how hard would this
be to imagine and then look for the functions in the manual:

(assumes all images in same dir)

build an array of file names in a directory
find/use function to generate a random number
access an image from the array using the random number
output html image tag using source path to image

that's 5 minutes worth of reading, right? it translates to:

$path = /* your image server path here */;
$images = array();
$directory = opendir($path);
while (($fileName = readdir($directory)) !== false)
{
if ($fileName == '.' || $fileName == '..'){ continue; }
if (!is_file($path. $fileName)){ continue; }
$images[] = $fileName;
}
closedir($directory);
if (!is_array($images)){ $images = array(); }
$index = rand(0, count($images) - 1);
$src = /* your image http path here */;
echo '<img alt="RTFM" src="' . $src . urlencode($images[$index]) . '">';

that'd be another 5 minutes. and hey, with SOME imagination, you could
create an array of image paths, get a random image from each using the
above, and then randomly choose an image from the paths. but i
digress...numbnuts' eyes and mind are sufficiently boggled by now.

cheers.
Steve [ Sa, 03 März 2007 18:04 ] [ ID #1646555 ]

Re: Random Thumbnail Script

"Fred Atkinson" <fatkinson [at] mishmash.com> wrote in message
news:3f4ju2943fcbb3vrf4qmhbr7m82dbeuv2v [at] 4ax.com...
| Folks,
|
| I found another script that does the job. It needs a little
| tweaking, but the code is simple enough that I can do that without too
| much trouble.

yeah, right.

| The script I found is at:
| http://www.theprojects.org/scripts/Get_Image_List/ in case you'd like
| to have a look at it.

i don't think we need to.
Steve [ Sa, 03 März 2007 18:07 ] [ ID #1646557 ]

Re: Random Thumbnail Script

| now you understand this isnt the most elegant or fast way, its just a
| way I thought you might understand, if you are a newbie, you could do
| worse - as steve pointed out graphically - that go get yourself a copy
| of the downloadable php extended manual for windows (I assume you are
| a windows user!)

someone had referenced how much time you'd given responding to the op on
this topic, so i thought i'd read it. i'm glad you made that sentence be
toward the end or i'd have been laughing too much to continue...as it is, i
had to wait about five minutes to recover enough to write this.

'i assume you are a windows user'...that's priceless! i like the os just
fine, but don't that just sum up about 90% of it's consumers' mentality.
'dammut jim! i'm givuner allshegot...i got nutton left to click!'. that
makes me wonder less about commercials that have the 'easy button'. we truly
are pandering no longer to the least common...the pandering can be
considered 'marketing' because the masses were once those least among us;
the lazy and unimaginative.

sorry, that's me being graphic again <he notes as he stands down from the
soapbox>. i did think that was hysterical though! windows user...lol.

cheers.
Steve [ Sa, 03 März 2007 19:08 ] [ ID #1646560 ]

Re: Random Thumbnail Script

On 3 Mar, 15:20, Fred Atkinson <fatkin... [at] mishmash.com> wrote:
> Folks,
>
> I found another script that does the job. It needs a little
> tweaking, but the code is simple enough that I can do that without too
> much trouble.
>
> The script I found is at:http://www.theprojects.org/scripts/Get_Image_List/in case you'd like
> to have a look at it.
>
> Regards,
>
> Fred

as you modify it, try to think of how efficient it will be, if I were
you and I wanted to call this script many times in a webpage, I would
definately build the array, and thin it down to the humber of images
you need in yout page, then stat the images, and also use its "return
the array of images" option, but extend the array to add the width and
height and alt, then you will have excatly what I was on about before.
The reason why returning an array is a good idea, is that you read the
dir once, and then have the details of the all the images. Then when
you put these details into the html, your users will request each in
turn and the server will go get each. This way you dont hit the disk
so much. If you have a great many images, You should split them as you
seem to have done, by directory to improve response time.
If all that seems little boring, welcome to programming where you do
well to think of every tiny thing, it is a good habit to get into. Of
course there are a few problems with this approach - not the least of
which is returning an array of a set number of images (because its
faster) but perhaps adjusting your html later to include more images.
For this reason you might like to call the function many times
returning on one each time, but on a very busy system this makes
things slow and also this kind of habit decreases the life of your
hard disk. zzZ
shimmyshack [ Sa, 03 März 2007 19:19 ] [ ID #1646561 ]

Re: Random Thumbnail Script

"shimmyshack" <matt.farey [at] gmail.com> wrote in message
news:1172945967.893113.300330 [at] t69g2000cwt.googlegroups.com.. .
| On 3 Mar, 15:20, Fred Atkinson <fatkin... [at] mishmash.com> wrote:
| > Folks,
| >
| > I found another script that does the job. It needs a little
| > tweaking, but the code is simple enough that I can do that without too
| > much trouble.
| >
| > The script I found is
at:http://www.theprojects.org/scripts/Get_Image_List/in case you'd like
| > to have a look at it.
| >
| > Regards,
| >
| > Fred
|
| as you modify it, try to think of how efficient it will be, if I were
| you and I wanted to call this script many times in a webpage, I would
| definately build the array, and thin it down to the humber of images
| you need in yout page, then stat the images, and also use its "return
| the array of images" option, but extend the array to add the width and
| height and alt, then you will have excatly what I was on about before.
| The reason why returning an array is a good idea, is that you read the
| dir once, and then have the details of the all the images. Then when
| you put these details into the html, your users will request each in
| turn and the server will go get each. This way you dont hit the disk
| so much. If you have a great many images, You should split them as you
| seem to have done, by directory to improve response time.
| If all that seems little boring, welcome to programming where you do
| well to think of every tiny thing, it is a good habit to get into. Of
| course there are a few problems with this approach - not the least of
| which is returning an array of a set number of images (because its
| faster) but perhaps adjusting your html later to include more images.
| For this reason you might like to call the function many times
| returning on one each time, but on a very busy system this makes
| things slow and also this kind of habit decreases the life of your
| hard disk. zzZ


yes, and we haven't even gotten into random selection before even
knowing/careing about what is on the hd...or other functions that are less
hd intensive than enumerating a dir before it actually has to happen...or
talked about using require/include/require_once/include_once to selectively
perform the function on demand...or, how to use oop to apply logic to
accessing the data and giving some sort of caching mechanism for the info of
a dir per session. a myriad of other things...then again, he just wanted a
hand-out of code that required the least amount of work for him to
implement...yet still run despite his great propensity to break things
during the process.

;^)
Steve [ Sa, 03 März 2007 19:38 ] [ ID #1646564 ]

Re: Random Thumbnail Script

> you can also use MySQL for this, which is probably better for you,
> since you might not want to handle so many arrays.

Certianly MySQL is a poweful tool for dealing with image galleries and
may other needs for a website, but pulling random files out of a
directory and placing them inside image tags? MySQL seems like
overkill for something that simple.
dimo414 [ Sa, 03 März 2007 21:00 ] [ ID #1646565 ]

Re: Random Thumbnail Script

Fred Atkinson wrote:
> I need a PHP script that can be invoked refererencing a
> subdirectory where thumbnails are stored and have it randomly display
> one of those thumbnails.
>
> To expand upon my needs, here is the hierarchy of the Web
> site:
>
> main pages public_html
>
> subpages apple banana coconut
>
> sub-subpages aa bb cc dd ee ff gg
>
> Each of the aa-gg subdirectories will contain thumbnail .jpg
> files. I'd like to be able to invoke the script quite a few times
> within the same PHP page.
> From the page in /banana, use <?php random_image $dir=aa> to
> return a thumbnail that it randomly took from sub page /banana/aa. I
> would invoke a thumbnail from another directory by changing aa to bb,
> cc, dd, ee, ff, or gg.

Make a file that you can include in your pages

--- randomimg.php ---
class randomimg {

protected $imgs=array();
protected $basepath;

function randomimg($base='.') {
$this->basepath=$base;
}

function getimg($path) {
if(empty($this->imgs[$path])) {
$this->imgs[$path]=scandir($this->basepath.'/'.$path);
}

/* Older versions need srand to work */
$version=explode('.',phpversion());
if((intval($version[0])<4) || ((intval($version[0])==4) &&
(intval($version[1])<2))) {
/* We have PHP 4.1 or older, so we need srand() to get random
numbers */
list($usec, $sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
}

return $path.'/'.$this->imgs[$path][rand(0,count($this->imgs[$path] )-1)];
}
}
--- eof ---

> From the page in /banana, use <?php random_image $dir=aa> to
> return a thumbnail that it randomly took from sub page /banana/aa. I
> would invoke a thumbnail from another directory by changing aa to bb,
> cc, dd, ee, ff, or gg.

in the page where you need the random images:

--- subpages.php ---
include_once('randomimg.php')
$images= new randomimg();

$path_to_the_image=$images->getimg('aa');
--- eof ---

--- main_pages.php ---
include_once('randomimg.php')
$images= new randomimg();

$path_to_the_image=$images->getimg('coconut/bb');
--- eof ---

It will just read a directory once, store all the files/directories in an
array and if the same directory is used more than once, it will skip the part
of scanning the directory.

Of course, I would most likely used a database for the file names/paths,
classes and so on.

--

//Aho
Shion [ Sa, 03 März 2007 21:06 ] [ ID #1646566 ]

Re: Random Thumbnail Script

On 3 Mar, 20:06, "J.O. Aho" <u... [at] example.net> wrote:
> Fred Atkinson wrote:
> > I need a PHP script that can be invoked refererencing a
> > subdirectory where thumbnails are stored and have it randomly display
> > one of those thumbnails.
>
> > To expand upon my needs, here is the hierarchy of the Web
> > site:
>
> > main pages public_html
>
> > subpages apple banana coconut
>
> > sub-subpages aa bb cc dd ee ff gg
>
> > Each of the aa-gg subdirectories will contain thumbnail .jpg
> > files. I'd like to be able to invoke the script quite a few times
> > within the same PHP page.
> > From the page in /banana, use <?php random_image $dir=aa> to
> > return a thumbnail that it randomly took from sub page /banana/aa. I
> > would invoke a thumbnail from another directory by changing aa to bb,
> > cc, dd, ee, ff, or gg.
>
> Make a file that you can include in your pages
>
> --- randomimg.php ---
> class randomimg {
>
> protected $imgs=array();
> protected $basepath;
>
> function randomimg($base='.') {
> $this->basepath=$base;
> }
>
> function getimg($path) {
> if(empty($this->imgs[$path])) {
> $this->imgs[$path]=scandir($this->basepath.'/'.$path);
> }
>
> /* Older versions need srand to work */
> $version=explode('.',phpversion());
> if((intval($version[0])<4) || ((intval($version[0])==4) &&
> (intval($version[1])<2))) {
> /* We have PHP 4.1 or older, so we need srand() to get random
> numbers */
> list($usec, $sec) = explode(' ', microtime());
> srand((float) $sec + ((float) $usec * 100000));
> }
>
> return $path.'/'.$this->imgs[$path][rand(0,count($this->imgs[$path] )-1)];
> }}
>
> --- eof ---
>
> > From the page in /banana, use <?php random_image $dir=aa> to
> > return a thumbnail that it randomly took from sub page /banana/aa. I
> > would invoke a thumbnail from another directory by changing aa to bb,
> > cc, dd, ee, ff, or gg.
>
> in the page where you need the random images:
>
> --- subpages.php ---
> include_once('randomimg.php')
> $images= new randomimg();
>
> $path_to_the_image=$images->getimg('aa');
> --- eof ---
>
> --- main_pages.php ---
> include_once('randomimg.php')
> $images= new randomimg();
>
> $path_to_the_image=$images->getimg('coconut/bb');
> --- eof ---
>
> It will just read a directory once, store all the files/directories in an
> array and if the same directory is used more than once, it will skip the part
> of scanning the directory.
>
> Of course, I would most likely used a database for the file names/paths,
> classes and so on.
>
> --
>
> //Aho

and then why not use sessions, store 200 in there, job done no further
problem, hmm
are we forgetting the newbie OP here?
shimmyshack [ Sa, 03 März 2007 21:12 ] [ ID #1646567 ]

Re: Random Thumbnail Script

shimmyshack wrote:
> On 3 Mar, 20:06, "J.O. Aho" <u... [at] example.net> wrote:
>> Fred Atkinson wrote:
>>> I need a PHP script that can be invoked refererencing a
>>> subdirectory where thumbnails are stored and have it randomly display
>>> one of those thumbnails.
>>> To expand upon my needs, here is the hierarchy of the Web
>>> site:
>>> main pages public_html
>>> subpages apple banana coconut
>>> sub-subpages aa bb cc dd ee ff gg
>>> Each of the aa-gg subdirectories will contain thumbnail .jpg
>>> files. I'd like to be able to invoke the script quite a few times
>>> within the same PHP page.
>>> From the page in /banana, use <?php random_image $dir=aa> to
>>> return a thumbnail that it randomly took from sub page /banana/aa. I
>>> would invoke a thumbnail from another directory by changing aa to bb,
>>> cc, dd, ee, ff, or gg.
>> Make a file that you can include in your pages
>>
>> --- randomimg.php ---
>> class randomimg {
>>
>> protected $imgs=array();
>> protected $basepath;
>>
>> function randomimg($base='.') {
>> $this->basepath=$base;
>> }
>>
>> function getimg($path) {
>> if(empty($this->imgs[$path])) {
>> $this->imgs[$path]=scandir($this->basepath.'/'.$path);
>> }
>>
>> /* Older versions need srand to work */
>> $version=explode('.',phpversion());
>> if((intval($version[0])<4) || ((intval($version[0])==4) &&
>> (intval($version[1])<2))) {
>> /* We have PHP 4.1 or older, so we need srand() to get random
>> numbers */
>> list($usec, $sec) = explode(' ', microtime());
>> srand((float) $sec + ((float) $usec * 100000));
>> }
>>
>> return $path.'/'.$this->imgs[$path][rand(0,count($this->imgs[$path] )-1)];
>> }}
>>
>> --- eof ---
>>
>> > From the page in /banana, use <?php random_image $dir=aa> to
>> > return a thumbnail that it randomly took from sub page /banana/aa. I
>> > would invoke a thumbnail from another directory by changing aa to bb,
>> > cc, dd, ee, ff, or gg.
>>
>> in the page where you need the random images:
>>
>> --- subpages.php ---
>> include_once('randomimg.php')
>> $images= new randomimg();
>>
>> $path_to_the_image=$images->getimg('aa');
>> --- eof ---
>>
>> --- main_pages.php ---
>> include_once('randomimg.php')
>> $images= new randomimg();
>>
>> $path_to_the_image=$images->getimg('coconut/bb');
>> --- eof ---
>>
>> It will just read a directory once, store all the files/directories in an
>> array and if the same directory is used more than once, it will skip the part
>> of scanning the directory.
>>
>> Of course, I would most likely used a database for the file names/paths,
>> classes and so on.


> and then why not use sessions, store 200 in there, job done no further
> problem, hmm

Sure, sessions would work as storage too as well as a file where all the image
paths are stored. There are quite many different options here.


> are we forgetting the newbie OP here?

Who cares when you have 5 minutes over for some silly coding.


--

//Aho
Shion [ Sa, 03 März 2007 21:21 ] [ ID #1646568 ]
PHP » alt.php » Random Thumbnail Script

Vorheriges Thema: Importing csv file into mysql
Nächstes Thema: Can anyone offer an opinion on Nvu? <----extreme newbie