Previous Thread
Next Thread
Print Thread
Rate Thread
#104601 11/03/2002 10:10 AM
Joined: Nov 2001
Posts: 1,704
CTM
Offline
Moderator / Da Masta
Moderator / Da Masta
Offline
Joined: Nov 2001
Posts: 1,704
(Perl wink )

I've been trying to write a Perl script that randomises image displays. This is my script:

Code
#!/usr/bin/perl

$photo_path = "D:webgta3_images";
$photo_url = "http://chris.iphq.co.uk/gta3_images";

my(@dirs, $file, $number);
opendir(DIR,"$photo_path");
foreach $file (sort readdir(DIR)) {
if ($file =~ /.jpg/) {
push (@dirs, "$file");
}
}
closedir(DIR);
srand(time ^ $$);
$number = rand(@dirs);
$image = qq!<img src="$photo_url/$dirs[$number]" alt="Random GTA3 Screenshot" border="0" />!;
print "Content-type: text/htmlnn";
print "$image";
exit;
But every time I do that I end up with a page trying to display the image "http://chris.iphq.co.uk/gta3_images/", which obviously doesn't exist. I think the problem lies with my @dirs array... But where?

Sponsored Links
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
Code
#!/usr/bin/perl -w
use strict;

my $photo_path = "D:/web/gta3_images"; # use forward slash or escape directory seperator => "D:\dir"
my $photo_url = "http://chris.iphq.co.uk/gta3_images";

my @dirs;

opendir(DIR,"$photo_path");

foreach(sort readdir(DIR)) {
if (m/.jpg$/i) {
push @dirs, $_;
}
}

closedir(DIR);

my $image = $dirs[rand(@dirs)];
$image = qq!<img src="$photo_url/$image" alt="Random GTA3 Screenshot" border="0" />!;

print "Content-type: text/htmlnn";
print "$image";

__END__;

Joined: Nov 2001
Posts: 1,704
CTM
Offline
Moderator / Da Masta
Moderator / Da Masta
Offline
Joined: Nov 2001
Posts: 1,704

Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
smile

Well... I forgot to escape the '.' meta character shocked (if (m/.jpg$/i))

Joined: Nov 2001
Posts: 1,704
CTM
Offline
Moderator / Da Masta
Moderator / Da Masta
Offline
Joined: Nov 2001
Posts: 1,704
Done... Thanks again smile

Sponsored Links
Joined: Nov 2001
Posts: 1,704
CTM
Offline
Moderator / Da Masta
Moderator / Da Masta
Offline
Joined: Nov 2001
Posts: 1,704
Okay... Round 2 wink

I've copied this script to my web folder (D:web, I'm using Apache locally) and I want to make it display inside an img tag. Trouble is, it won't execute (I'm using the UBBDev tutorial that Matt wrote). I link to http://chris.iphq.co.uk/gta3_random.gif and make Apache execute gifs... This works, but Apache also tries to execute every other gif on my server, resulting in a 500 Error. The only reason I'm making Apache execute gifs is because vB does a security check inside img tags: they only allow JPEGs and GIFs to be displayed.

Is it possible JUST to execute my random image script as a GIF?

Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
Umm... I dont know apache configuration too much, but it may be possible with a local .htaccess file...

Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
also, if you can edit the httpd.conf on the server, you can give the execute rights to image extensions only to a directory, I believe... But I dont remember how to do this...

Joined: Nov 2001
Posts: 1,704
CTM
Offline
Moderator / Da Masta
Moderator / Da Masta
Offline
Joined: Nov 2001
Posts: 1,704
Okay... Time to look through the config comments and the documentation a little more closely wink

Thanks smile

Joined: Nov 2001
Posts: 1,704
CTM
Offline
Moderator / Da Masta
Moderator / Da Masta
Offline
Joined: Nov 2001
Posts: 1,704
Got it working. smile

[EDIT: *** IGNORE THE FOLLOWING, SKIP AHEAD ***]

I'm doing the following:

Code
#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ "D:/web/cgi-bin/"
ScriptAlias /webspace/ "D:/web/webspace/"
ScriptAlias /irc/news/ "D:/web/irc/news/"
In other words, I've ScriptAliased all my CGI-content directories. There must be an easier way of doing this, surely...

[EDIT: *** START HERE ***]

Okay, it doesn't matter about that stuff above. As long as it works, that's the important thing.

Another problem: Linking to it on another page. The HTML image tag won't display it, even when I use print "Location:nn"... Any way of solving this? SSI isn't an option...

Sponsored Links
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
do you mean something like this?

print "Content-type:image/gifnn";
open IMG, "< $img";
binmode IMG;
print while ;

for jpeg, it can be Content-type:image/jpeg, but I'm not sure...

Joined: Nov 2001
Posts: 1,704
CTM
Offline
Moderator / Da Masta
Moderator / Da Masta
Offline
Joined: Nov 2001
Posts: 1,704
Okay, taking it from closedir...

Code
closedir(DIR);

my $image = $dirs[rand(@dirs)];
#$image = qq!<img src="$photo_url/$image" alt="Random GTA3 Screenshot" border="0" />!;
$image = "$photo_url/$image";

print "Content-type: image/gifnn";
open IMG, "< $image";
binmode IMG;
print while <IMG>;

__END__;
Outputs an image trying to link to the name of the script. frown

Thanks again for the help jeo, I appreciate it smile

Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
you cant open() an url...
it must be:
open IMG, "< $photo_path/$image";

This time I've checked the code => you can use this sample code:
Code
my $header = "Content-Type:image/jpegnCache-Control:no-cachenn";

print $header;

open(IMG,"$dir/your.jpg");

print while <IMG>;
use "image/jpeg" for .jpg/.jpeg/jpe
use "image/gif" for .gif

extensions...

Joined: Nov 2001
Posts: 1,704
CTM
Offline
Moderator / Da Masta
Moderator / Da Masta
Offline
Joined: Nov 2001
Posts: 1,704
Hurrah!

Using jeo's script and some of LK's base knowledge, I finally got it to work on another page smile

Thanks again jeo... Case closed smile

Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
ok smile

Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
print while ;

For a binary file? tipsy

That may work, but it's more rational to use read. Also, I think you should use binmode STDOUT, since you're outputting binary data.

Code
binmode STDOUT;
print $buf while read(IMG, $buf, 16384);

Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
well... I've tested it and it works fine smile

But you're right... read() can be better...

Also, I've used binmode() on the image file...


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
isaac
isaac
California
Posts: 1,157
Joined: July 2001
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)