PDA

View Full Version : Swiss Army Slide Show - skip missing images?



steamfreak
10-24-2007, 04:32 AM
1) Script Title: Swiss Army Slide Show

2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...army/index.htm

3) Describe problem:
I have a series of slide shows on different pages, with up to 20 images on each show. I'm wondering if there is a way to make the script skip missing images if they are not all there, and just continue on to the next one.

Thanks,
steamfreak

steamfreak
10-24-2007, 06:01 AM
I'm replying to my own post here...

I found a way around my problem. Instead of hardcoding the
slides[0] = ["slide1.jpg", ""];
slides[1] = ["slide2.jpg", ""];
:
:
entries, I wrote a simple php script to generate this based on the contents of the folder where the images are stored. Now I can store images of any name and any number of them and it should work.

Cheers,
steamfreak.

fabiospark
11-24-2007, 11:32 AM
Do you mind sharing your php script, please?

Thanks

jscheuer1
11-24-2007, 02:32 PM
My PHP is not the best, but you can do something like so on most PHP servers (filename - get_slides.php):


<?php header('Content-Type: text/javascript');
function returnimages($dirname="./images/") {
$pattern="\.(jpg|jpeg|png|gif|bmp)$";
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){
echo 'slides[' . $curimage .']=["' . $dirname . $file . '", "", ""];' . "\n";
$curimage++;
}
}

closedir($handle);
}
return($files);
}

echo "var slides=new Array();" . "\n";
returnimages();
?>

That will pick up the images in a folder called images of off the folder in which the above file is located, and define them as elements in the slides array. Then on a regular HTML or PHP page in the same folder as get_slides.php (not in the images folder), instead of defining the slides array in your code, link first to the external script:


<script type="text/javascript" src="get_slides.php"></script>

Remember not to redefine the array named slides on your page or in other scripts linked to it.

fabiospark
11-24-2007, 10:01 PM
Would you please give me a good link where I can find very basic info on how to use a php file like yours, without wasting your time for that?

Thanks.

jscheuer1
11-25-2007, 01:51 PM
I'm not sure I understand your question. To use it you just save it in a text editor as:

get_slides.php

Upload it to a PHP enabled server. Put your page with swiss army on it in the same folder but don't define the slides array, and place this in the head:


<script type="text/javascript" src="get_slides.php"></script>

before any of the swiss army script code.

Put your images in a folder named:

images

off of the folder that contains the above files.

Also, if you want to get a better idea of the principals involved, you should have a look at this script:

http://www.dynamicdrive.com/dynamicindex4/php-photoalbum.htm

it uses a similar sort of separate PHP file as an external javascript to gather the image names from a folder. The main differences are the name of the array generated and the purposes it is put to in the main script.

If on the other hand you want to learn how to code in PHP, I like this site:

http://us.php.net/manual/en/index.php

But I'm no PHP expert, not even all that experienced. There could be better resources out there.

fabiospark
11-27-2007, 05:36 PM
I asked my web provider and they said the hosting of my site is not PHP enabled. I could buy the PHP + MySQL enabling for 250 euro a year: is there anything else I can do about it?

Should I be interested in this script all the same, will I have to install anything on my PC to make it work at least locally?
If yes (as I think), which interpreter/compiler would be better? (Win2k or XP)

Thanks and sorry for the dumb questions.

jscheuer1
11-27-2007, 07:00 PM
There are some free hosts that support PHP, perhaps some even with MySQL support as well. You don't need MySQL (a popular data base program) for this code.

There are PHP and server emulators for local windows machines, that should allow (some of these emulations are, as I understand it, far from perfect) you to test your PHP code locally. More than one program may be required, at least for certain implementations. I have no experience with any of them, so cannot make any recommendations, or even speak with any real authority about any of that.

When testing (I do so little of this sort of coding anyway), I just upload to a PHP enabled server and see what happens.

fabiospark
11-30-2007, 09:13 PM
Thanks to your answer, as usual.

After some thinking, I realized I don't really need a PHP script because my only need is to automatically build the array, pointing to a folder, locally.
Then I will upload the files to my hosting service.
I will rarely, if ever, change the folder content and if I will, I'll be happy to run the "array building" local script again to update the array.
Trouble is: how to make this script? Or: is there such a script not involving a specific server side service?

Thanks.

jscheuer1
12-01-2007, 04:27 AM
http://www.dynamicdrive.com/forums/showthread.php?p=54946#post54946

grantiago
12-30-2016, 11:09 PM
My PHP is not the best, but you can do something like so on most PHP servers (filename - get_slides.php):


<?php header('Content-Type: text/javascript');
function returnimages($dirname="./images/") {
$pattern="\.(jpg|jpeg|png|gif|bmp)$";
$files = array();
$curimage=0;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){
echo 'slides[' . $curimage .']=["' . $dirname . $file . '", "", ""];' . "\n";
$curimage++;
}
}

closedir($handle);
}
return($files);
}

echo "var slides=new Array();" . "\n";
returnimages();
?>

That will pick up the images in a folder called images of off the folder in which the above file is located, and define them as elements in the slides array. Then on a regular HTML or PHP page in the same folder as get_slides.php (not in the images folder), instead of defining the slides array in your code, link first to the external script:


<script type="text/javascript" src="get_slides.php"></script>

Remember not to redefine the array named slides on your page or in other scripts linked to it.

I tried that php script. I needed my images loaded in order and this one just picked them up randomly. Couldn't figure out how to sort in the above script. In addition, eregi is deprecated. Didn't want to deal with that either.

In my case it was faster and easier to understand by filling the js imagearray[ ...
with a quick escape to php in the javascript call:

imagearray: [
<?
$curimage=0;
chdir('../images/slide/');
foreach (glob("*.png") as $filename) {
echo '["../images/slide/' . $filename . '"],';
echo "\n";
$curimage++;
}
// added below to skip the , on the last array element for lack of a better solution;-)
echo '["../images/slide/dummyimage.png"]';
echo "\n],";
?>
displaymode: {type ...

jscheuer1
12-30-2016, 11:56 PM
I would use glob now as well (that said, preg_match with just a little tweaking is a drop in replacement for either ereg or eregi, and is what I was using for a time before I settled on glob). Once you have the array of images, you can sort them in quite a number of ways, either in javascript or in PHP. But if they're already coming out in the order you want, or you're going to shuffle them anyway, then there's no need. Also - I generally now join (implode) the array rather than for each it.