Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Image from directory CGI

Status
Not open for further replies.

mxw

Programmer
Jul 24, 2001
79
CA
Hello,

I'm looking for a "free" display one random image per day script, that works by calling images from a specified directory. Most scripts I've seen requires to type each image name in ... *rolling eyes (I have over(200)two hundred).

Has anyone seen this script? Would anyone like to write one for me with credits and link to your own site?

thx kindly,
mxw
~Eeegads~
 
Any idea how you would want it to work? I can see an relatively easy method: just build an array of image name in the directory, and take a random number between 1 and the number of image names in the array to decide which one to serve. That way any new image would only have to be copied to the directory, and they would be automatically included in the list. Perhaps less efficient than the usual method, since you would have to get a directory listing each time, but more flexible. Alternatively, you could run a cron job (unix) or scheduled job that would create a new list of image files for the program to process at periodic intervals. Any preference?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Hello Tracy,

Thank you for your reply. I don't know anything about programming so I'll try to explain exactly what I thought *could happen on this page. The main page/table of contents assuming ppl reload this page more than twice.

1. The image would change itself once a week rather than each day as originally requested.
2. All images are in their own directory.
3. All images are .jpg format/same dimensions.
4. They would run from a txt file in the cgi-bin. That way I could add new images to the list.
5. These images are being using else where on the site and I don't wish to consider renaming and relinking those files.
6. No log would be necessary.

Dream On?

thx
mxw

~Eeegads~
 
What you want is doable, but it would actually be easier to have the image randomly selected every time the page is loaded than to have it changed daily or weekly. Then you could just use a cgi script in the image tag to select a random image to be displayed. If you want to change the image daily or weekly, you would most likely want to run a scheduled (i.e. cron) job daily or weekly to select an image. I could create a program that runs every time the page is loaded and still have it change the image only once a day or once a week, but that wouldn't be particularly efficient.

If all you want is to randomly select an image name from a list in a .txt file and display that image, I can write the program for you. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
"If all you want is to randomly select an image name from a list in a .txt file and display that image, I can write the program for you."

Yesss, that doesn't require renaming all the images, can be called from a specific directory and, new images added to the .txt file. (Does not have a log that eats up space.)

Question? Can the images be linked via the txt file?
Not important, just asking.

thank you,
m_x_w@hotmail.com ~Eeegads~
 
"Can the images be linked via the txt file?"

I'm not really sure what you mean by that. Do you want different images to link to different places? That can't be done with the random image server I was talking about, but I can think of a way to generate random image links too. It would be done in a slightly different fashion. Namely using a random link server that would be called via a server side exec.

Do you just need random images, or do they need to be links too? Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Oh,
Image directory and html directory each to their own.
as linked images they are:
images 400a.jpg to 400e.jpg linked to one file 400.html then 500a.jpg to 500f.jpg is linked to file 500.html
(these names represent month and year for me)

So I thought a txt file command referencing the image and link in one liners. Closest thing I saw was in applets.

If its all just too complicated with links then a random image displayed is just dandy, really. And I could use one link below the random display that would take the viewer to a page indexing all the images.

I don't have my own server, but I know I can run cgi scripts. Nor do I do cgiscript that well. But I'd like to try.

Thank you for all your time.
mxw




~Eeegads~
 
Thanks Barbie I will have a look.

mxw ~Eeegads~
 

This is an easy banner rotation script that I have modified slightly on to rotate images with each reload. It seems to fit all the criteria as far as having same size images in their own directory. Adding new images is easy with an admin screen that lets you add, edit quickly. When in doubt, deny all terms and defnitions.
 
Thanks viol8ion for your suggestion.

I had an interesting experience with log files once ... they ate up all my space. When I tried uploading(updating)the site I kept getting "You've exceeding your limit" message. I don't want to worry about that again.

At this point I'm feeling quite intimidated by looking at all of these different scripts. I know they don't bite, they just look like they do. Needing step by step instructions.

thanks again.

~Eeegads~
 
>>I had an interesting experience with log files once<<

Log files are just that, a log. They are meant to be looked at every once in a while, then either archived or emptied once in a while. If you are not using them, then limit their size in the script, or disable them (not recommended, you just may need them sometime). When in doubt, deny all terms and defnitions.
 
If your worried about log file size and your on a *nix machine you can use logrotate to rotate your log files
 
Well ... whilst I waited for tracy to write a script. I looked over the suggestions here and, I passed the ServerSide Includes script suggested by viol8ion for the one suggested by Barbie at ....
rand_image.pl

Three things of concern looking/testing the codes here for rand_image.pl:

------------------------------------------------------------
# $DEBUGGING must be set in a BEGIN block in order to have it be set before
# the program is fully compiled.
# This should almost certainly be set to 0 when the program is 'live'
#

BEGIN
{
$DEBUGGING = 1;
}

>The test page is working. I should have reset that
= 1; to = 0; ?????
------------------------------------------------------------

my $basedir = '
my @files = qw(100.jpg
100e.jpg
100p.jpg
100r.jpg
1000.jpg
1000z.jpg
1000e.jpg
1000p.jpg
1000r.jpg
1000s.jpg);

>The &quot;my @files&quot; Can't this be a wild card * ? I have over 200 images. Which is why I thought a txt file with the .pl file in the cgi-bin would make updating alot easier. I wouldn't have to chmod that txt file would I?
------------------------------------------------------------

my $uselog = 1; # 1 = YES; 0 = NO
my $logfile = '/path/to/piclog';

# End configuration

>$uselog = 0; I really don't need to know how many times each image was viewed setting this to zero won't be a bad thing would it? I don't know how to modify this code to change anything to do with the log files. As yauncin suggested to rotate 'em. I thought:

my $uselog = 0;
Delete this line -> my $logfile = '/path/to/piclog';

# End configuration

I couldn't find info in the zip about what the chmod command was on the .pl, seeking answers on the internet and found 755 seemed to be the one used most often.

I'm not too far OFF am I?
appreciating all the help here ...
mxw ~Eeegads~
 
Pt1:

Yes once you have a working version you can safe set $DEBUGGING to 0. It's purely there to help you in the event of your configuration not working correctly.

Pt2:

You can read in the list of images from the text file if you wish, as in:

open FH, &quot;myimages.txt&quot; or die &quot;Cannot open file: $!\n&quot;;
my @files = <FH>;
chomp(@files);
close FH;

You just chmod the text file to ensure that the file is readable.

pt3:

If you don't want to have a log, then just set $uselog to 0 as you rightly supposed.

The chmod on the script should be 755 so as to allow just the file creator (yourself) write access, but everyone else read and execute access.

HTH,
Barbie. Leader of Birmingham Perl Mongers
 
&quot;Thanking for your time tracy&quot; I'm thinking modifications to the script Barbie suggested will do me.

Okay then, I've set the $DEBUGGING to 0.
----------------------------------------------
Instead of using this:

my $basedir = 'my @files = qw(100.jpg
100e.jpg
100p.jpg
100r.jpg);

Include/modify this part as:

my $basedir = '
open FH, &quot;myimages.txt&quot; or die &quot;Cannot open file: $!\n&quot;;
my @files = <FH>;
chomp(@files);
close FH;


----------------------------------------------
chmod the text file - 755??
----------------------------------------------
Okay - done.
my $uselog = 0;

Delete this line?? -> my $logfile = '/path/to/piclog';

Do I have it right?
Thank you for your instructions Barbie.

mxw

~Eeegads~
 
Thanks for thinking of me, but I really haven't had any time to spend on your project lately anyway, plus the last few days it looked like you already had a solution. Let me know if it doesn't work out for you and I'll do the program you need. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Hello everyone,

I must say that I have really appreciated the help and suggestions offered here. I have modified the rand_image script to do me and I happy with that. Now ... I've got a new question but I feel it deserves a New Thread. ;)

Thanks again,
mxw

~Eeegads~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top