Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Random Imaging

by appex32 (Initiate)
on Aug 01, 2003 at 14:00 UTC ( [id://279967] : perlquestion . print w/replies, xml ) Need Help??

appex32 has asked for the wisdom of the Perl Monks concerning the following question:

I am a beginner with perl and as a way to get some practice using it I decided to try to do random imaging with it. I have my own apache server set up on windows, which is where I intended to run it. I'm almost 100% sure that I have the server configured correctly because when I run the perl script it shows up on the server, the only problem is that the actual image does not appear. However, the area that it should be in shows up, but it shows up like it would if a image doesn't load on a webpage. I'm not sure if I need something else to allow me to get the images to appear or what. Here is the code that I have, if you have any suggestions or ideas let me know.
#!c:\perl\bin\perl.exe -w use strict; use strict "vars"; my @imgArray = ("./pics/blink.gif","./pics/sum41.jpg","./pics/ul.jpg", +"./pics/ltj.jpg","./pics/mest.jpg","./pics/blink2.jpg","./pics/blink3 +.jpg"); srand(time ^ $$); my $num = rand(@imgArray); print "Content-type: text/html\n\n"; print "Index: $num <br>Image: $imgArray[$num]<br><br>"; print "<center>Random imaging with Perl.....<br><br></center>"; if($num < 1) {print "<body bgcolor=lightgrey><center><img src=$imgArray[$num] bor +der=1 height=320 width=240><br><br><font>Blink 182 (1)</font></center +></body>\n";} elsif($num < 2) {print "<body bgcolor=lightgrey><center><img src=$imgArray[$num] bor +der=1 height=316 width=500><br><br><font>Sum 41</font></center></body +>\n";} elsif($num < 3) {print "<body bgcolor=lightgrey><center><img src=$imgArray[$num] bor +der=1 height=600 width=450><br><br><font>Unwritten Law</font></center +></body>\n";} elsif($num < 4) {print "<body bgcolor=lightgrey><center><img src=$imgArray[$num] bor +der=1 height=320 width=240><br><br><font>Less Than Jake</font></cente +r></body>\n";} elsif($num < 5) {print "<body bgcolor=lightgrey><center><img src=$imgArray[$num] bor +der=1 height=365 width=550><br><br><font>Mest</font></center></body>\ +n";} elsif($num < 6) {print "<body bgcolor=lightgrey><center><img src=$imgArray[$num] bor +der=1 height=322 width=356><br><br><font>Blink 182 (2)</font></center +></body>\n";} elsif($num < 7) {print "<body bgcolor=lightgrey><center><img src=$imgArray[$num] bor +der=1 height=536 width=678><br><br><font>Blink 182 (3)</font></center +></body>\n";} else {print "Error loading picture!";}
Thanks, KP

Replies are listed 'Best First'.
Re: Random Imaging
by Zaxo (Archbishop) on Aug 01, 2003 at 14:14 UTC

    I don't think your image file names are being taken as correct URLs. You've written them as relative host paths. Omitting the '.' may be enough.

    Also, it wouldn't hurt to quote the attribute values in your tags. Try feeding your output to an html checker.

    After Compline,

Re: Random Imaging
by Abigail-II (Bishop) on Aug 01, 2003 at 14:25 UTC
    srand(time ^ $$);

    Eeew! Where did you get this from? Many, many moons ago I wrote a little CGI program that randomly returned an image as well, and I discovered that it would often return the same image twice in a row, if you have seed the random generator with time ^ $$. I posted about this to the newsgroup more than seven years ago. This resulted in the Camel II already warning against this usage, and later versions of Perl using a much better default salt.

    Why, o, why, did you pick such a bad seed?


      many moons ago I wrote a little CGI program ... with time ^ $$ ... I posted about this to the newsgroup more than seven years ago ... Why, o, why, did you pick such a bad seed?

      Perhaps it is a comon mistake for new people to random numbers and perl? It sounds like you learned from your mistake 7 years ago. =)

        I was doing what was common practise back then. Since then common practise has changed, and the documentation explicitely warns against using time ^ $$. So, my question remains, why did you choose time ^ $$?


Re: Random Imaging
by liz (Monsignor) on Aug 01, 2003 at 14:19 UTC
    Unrelated to your imaging problem:

    Don't use srand() unless you really know why you need it.


Re: Random Imaging
by ChrisR (Hermit) on Aug 01, 2003 at 14:16 UTC
    Without knowing the directory structure or file permissions you have setup, it is very hard to say what the problem is. However, there should be a fairly descriptive reason in your web logs. It looks to me like the paths for your image files may be incorrect. You might try changing
    and see if that helps but be sure to check your error log first.
      In response to chrisR and zaxo, the whole ./ to just / makes no difference, I tried that a long time ago but thanks for the comments.

        At the URL you posted, the image links are now something like "../htdocs/img.jpg", which just won't work at all.

        It still seems like "/pics/" is what the paths should start with. Even if you've already tried it, try changing the paths back to "/pics" again, so we can take a look at your running script that way and see what's going on.

Re: Random Imaging
by Cine (Friar) on Aug 01, 2003 at 15:08 UTC
    That is some ugly code. It generates incorrect HTML (it has so many errors, that you should read "HTML for dummies" again), not that my fix will go through a validator...
    The code is completly unmaintainable and gives the impression that you have absolutely no idea of what you are doing.
    #!c:\perl\bin\perl.exe -w use strict; my @imgArray = ( { file => "./pics/blink.gif", h => 320, w => 240, title => "Blink 128 (1)", }, { file => "./pics/sum41.gif", h => 316, w => 500, title => "Sum 41", }, #and so on ... ); #dont use srand, unless you want a PREDICTABLE series of numbers every + time! my $num = rand(@imgArray); my $img = $imgArray[$num]; print "Content-type: text/html\n\n"; print <<HTML; <html> <head> <title></title> </head> <body bgcolor=lightgrey> Index: $num <br>Image: $$img{file} <br> <br> <center>Random imaging with Perl.....</center> <br> <br> <center> <img src="$$img{file}" border=1 height="$$img{h}" width="$$img{w}"> <br> <br> <font>$$img{title}</font> </center> </body> </html> HTML

    T I M T O W T D I
      I think that Cine could be making a good point here; that it is important to seperate the programatic logic from the HTML presentation, even in a small script. His example code does a good job of that. For this reason I would mod parent up ++.

      However, the tone of the his message is a bit off-putting. For this reason I would mod parent down --.

      Nothing is too wonderful to be true
      -- Michael Faraday

        I've only been a member here for like 5 hrs, is mod parent up ++ and mod parent down -- some type of rating thing on here?
      I don't understand how I'm generating all this horrible html code, when the only problem I seem to have is that the picture is not displayed. If you want to see what comes up go here:

        The problem is that you are populating your IMG tags with this path:

        /htdocs/pics/{random image file}

        However, your pics are here:

        http://{your site IP}/pics/{random image file}

        Without knowing your filesystem, it's hard to say exactly where, but somewhere you are introducing "/htdocs/" to the path. My guess is the "./" part of the path, but you've said changing that has no effect.

        Off the top of my head: could this be a config issue with your server? Perhaps you are pointing at the directory above your docroot (ie htdocs) instead of the htdocs directory?


        Update: I copy/pasted your code and ran it on my machine. When I removed the "." before the "./pics" it worked. If you do that and you are still getting "/htdocs/pics" I'd say it's almost certainly a config issue.

        I don't understand how I'm generating all this horrible html code
        No that is obvious.
        when the only problem I seem to have is that the picture is not displayed
        Looks can be deceiving.
        But your problem is obvious, you dont have a directory called /pics on your webserver, yet that is the link you send to the browser! It does seem you have a /cgi-bin/pics/ but that tries to execute the image as a cgi-script, which does not go well.

        T I M T O W T D I
      When you use $$img{...}, what does using two $'s mean...does that signify that you are using an array of hashes rather than just a simple array where you would use only one $ to call a particular value? Thanks
        When you use $$img{...}, what does using two $'s mean...does that signify that you are using an array of hashes rather than just a simple array
        Yes and no. An array of hashes are really an array of references to hashes. Therefore when you fetch something from the array, you get a hash reference back.
        To actually use the hash, you then have to de-reference it. There are two syntactic ways of doing that $reference->{} or $$reference{}
        I like the latter best, mostly because its shorter and because it is coloured yellow in emacs...
        Read the perlref manpage

        T I M T O W T D I