|
Joined: Nov 2001
Posts: 1,704
Moderator / Da Masta
|
Moderator / Da Masta
Joined: Nov 2001
Posts: 1,704 |
(Perl  ) I've been trying to write a Perl script that randomises image displays. This is my script: #!/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?
|
|
|
|
Joined: May 2000
Posts: 1,356
Addict
|
Addict
Joined: May 2000
Posts: 1,356 |
#!/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
Moderator / Da Masta
|
Moderator / Da Masta
Joined: Nov 2001
Posts: 1,704 |
|
|
|
|
Joined: May 2000
Posts: 1,356
Addict
|
Addict
Joined: May 2000
Posts: 1,356 |
Well... I forgot to escape the '.' meta character  (if (m/.jpg$/i))
|
|
|
|
Joined: Nov 2001
Posts: 1,704
Moderator / Da Masta
|
Moderator / Da Masta
Joined: Nov 2001
Posts: 1,704 |
Done... Thanks again 
|
|
|
|
Joined: Nov 2001
Posts: 1,704
Moderator / Da Masta
|
Moderator / Da Masta
Joined: Nov 2001
Posts: 1,704 |
Okay... Round 2 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
Moderator / Da Masta
|
Moderator / Da Masta
Joined: Nov 2001
Posts: 1,704 |
Okay... Time to look through the config comments and the documentation a little more closely Thanks 
|
|
|
|
Joined: Nov 2001
Posts: 1,704
Moderator / Da Masta
|
Moderator / Da Masta
Joined: Nov 2001
Posts: 1,704 |
Got it working. [EDIT: *** IGNORE THE FOLLOWING, SKIP AHEAD ***]I'm doing the following: # # 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...
|
|
|
|
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
Moderator / Da Masta
|
Moderator / Da Masta
Joined: Nov 2001
Posts: 1,704 |
Okay, taking it from closedir... 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. Thanks again for the help jeo, I appreciate it 
|
|
|
|
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: 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
Moderator / Da Masta
|
Moderator / Da Masta
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 Thanks again jeo... Case closed 
|
|
|
|
Joined: May 2000
Posts: 1,356
Addict
|
Addict
Joined: May 2000
Posts: 1,356 |
ok 
|
|
|
|
Joined: Aug 2000
Posts: 335
Member
|
Member
Joined: Aug 2000
Posts: 335 |
print while ;For a binary file? 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. 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 But you're right... read() can be better... Also, I've used binmode() on the image file...
|
|
|
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.
|
|
Posts: 449
Joined: February 2008
|
|
Forums63
Topics37,575
Posts293,930
Members13,823
|
Most Online6,139 Sep 21st, 2024
|
|
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
|
|
|
|