|
Posted by shimmyshack on 03/02/07 22:24
On 2 Mar, 19:20, Fred Atkinson <fatkin...@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.
[Back to original message]
|