Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Random Image Generator

by invidious (Initiate)
on Oct 13, 2003 at 17:21 UTC ( [id://298881]=perlquestion: print w/replies, xml ) Need Help??

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

I am writing a random image generator and I have no idea what I am doing wrong. I run the file and it obviously outputs the picture to command prompt but when I try and access the file from <IMG SRC> it will not display the picture. I am quite new at perl itself so please bare with me.
Here is the script:
#! /usr/bin/perl $urldir = "http://jason.yellowsnow.net/images/"; $dir = "."; opendir(DIR,$dir) || die "Unable to open $urldir: $!"; @files = grep { /\.(?:jpg)$/i } readdir DIR; srand (time ^ $$); $file = rand(@files); open(IMG,$files[$file]) or die "Unable to open $files[$file]: $!"; print "Content-type: image/jpg\n\n"; { print <IMG>; } close IMG; closedir DIR;
Thanks in advance for your guys' help.

Replies are listed 'Best First'.
Re: Random Image Generator
by Aristotle (Chancellor) on Oct 13, 2003 at 17:27 UTC
    Check your webserver logs, or if you can't access them put use CGI::Carp 'fatalsToBrowser'; at the top of your script. (Remember to remove the fatalsToBrowser bit when the script is on the live server!) For me, when I test run this code, I get your die's message that the directory could not be opened. Be aware that when called from the webserver, the current working directory of the script is not necessarily the directory the script resides in.

    Makeshifts last the longest.

Re: Random Image Generator
by CombatSquirrel (Hermit) on Oct 13, 2003 at 17:31 UTC
    You might want to set binmode STDOUT; at the beginning of your script, as you are printing a binary file (the image) to STDOUT. ;-)
    Also, the non-capturing parens ((?:)) in your RegEx don't do anything; you might as well remove them.
    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
      Thank you for your advice CombatSquirrel!
      Its the same thing that needed to be done for a script
      I'm writing.

      Live well, eat Perl. My site

Re: Random Image Generator
by blokhead (Monsignor) on Oct 13, 2003 at 17:36 UTC
    { print <IMG>; }
    will only read from IMG until the first "\n". You can't assume the jpeg files don't include this byte. Try changing it to:
    { local $/; print <IMG>; }
    This will read in all of IMG and print it.

    blokhead (200th post)

      Actually print <IMG>; will print the whole file. The "print" provides a list context and readline in a list context reads the whole file.
Re: Random Image Generator
by Cody Pendant (Prior) on Oct 13, 2003 at 22:03 UTC
    when I try and access the file from <IMG SRC> it will not display the picture

    Call me crazy but why not just print out

    <img src="/path/to/$file">
    and let the browser worry about opening your images for you?


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
Re: Random Image Generator
by TVSET (Chaplain) on Oct 13, 2003 at 17:51 UTC
    Additionally to all the above suggestions, you should check that directory that you expect script to run in is actually the one you exptected:

    $dir = "."; opendir(DIR,$dir) || die "Unable to open $urldir: $!";
Re: Random Image Generator
by pg (Canon) on Oct 13, 2003 at 18:03 UTC

    Try to do this simple testing, and help you to narrow down the problem: Write the image to a file, then use IE or whatever you have to open the image file as a local file (c: something something), not over network. If the image does not show up, then it has a bad format, blokhead had a very good guess already; If the image does show up, then the problem would be the way you form your http or html.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://298881]
Approved by Aristotle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-19 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found