Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Error reading image from perl cgi script into HTML

by grao5 (Initiate)
on Oct 13, 2002 at 03:11 UTC ( [id://204847]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

In order to create and image based web counter using Perl cgi on Windows 98 I am just trying to read an image into an html from a perl cgi script. But it does not work for some reason. I have 3 files in my source directory. mycounter.html, mycounter.cgi and counter.jpg Inside my html file mycounter.html I try to read image from the perl cgi script using the line.

<img src = mycounter.cgi>

where mycounter.cgi is my perl cgi program with the following contents.

#!C:\Progra~1\Perl\bin\perl use CGI; $co=new CGI; $size = stat("counter.jpg")[7]; open (FILEHANDLE,"<counter.jpg"); read FILEHANDLE,$imagedata,$size; close FILEHANDLE print $co->header(-type=>'image/jpeg'), "Below is my counter image file", $imagedata;

Then I try to open mycounter.html from the browser. But all I see on the browser is the string "Below is my counter image file" which it reads from the perl script but then there is no image at all (just a blank image). Why is the html unable to read the image sent from the cgi script. Once again I am running this on Windows 98 OS. Your help will be greatly apprecited
thanks grao5 (grao5@hotmail.com)

Replies are listed 'Best First'.
Re: Error reading image from perl cgi script into HTML
by PodMaster (Abbot) on Oct 13, 2002 at 04:29 UTC
    Why would you expect it to work?

    HTTP is a simple protocol, and printing "text text text text" after you say that what's coming is an "image" of type "jpeg", doesn't sit well with any HTTP compliant application.

    print $co->header(-type=>'image/jpeg'),$imagedata; If you wish to speak HTTP, you have to know how to speak HTTP.

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Error reading image from perl cgi script into HTML
by fsn (Friar) on Oct 13, 2002 at 12:32 UTC
    As podmaster put's it, you have misunderstood some things about browsers, HTML and HTTP. You should make an html page which refers to you counter image like any other image. This could serve as a very simple (and untested) example:

    First, the HTML page:

    <html> <head> <title>Test page</title> </head> <body> <H1>My test page</H1> My counter below:<BR> <IMG SRC="/cgi-bin/my-count.pl"> </body> </html>

    And now, my-count.pl

    #!C:\Progra~1\Perl\bin\perl use CGI; $co=new CGI; print $co->header(-type=>'image/jpeg'); $size = stat("counter.jpg")[7]; open (FILEHANDLE,"<counter.jpg"); read FILEHANDLE,$imagedata,$size; close FILEHANDLE;
    And then you replace my-count.pl with something that actually counts.

      I tried exactly as fsn said. I also added BINMODE before the FILEHANDLE as BUU said. But it still does not work.

      Following is html file contents (mycounter.html)

      <HTML> <head> <title> Test page </title> </head> <BODY> <H> My test page</H> My counter below: <BR> <IMG SRC = "mycounter.cgi"> </BODY> </HTML>
      and Following is the contents of the perl cgi file (mycounter.cgi)
      #!C:\Progra~1\Perl\bin\perl use CGI; $co=new CGI; $size = stat("counter.jpg")[7]; open (FILEHANDLE,"<counter.jpg"); binmode FILEHANDLE; read FILEHANDLE,$imagedata,$size; close FILEHANDLE; print $co->header(-type=>'image/jpeg'), $imagedata;

      But the only thing I get when I open mycounter.html is the following string

      "My test page. My counter below:" followed by a blank image file.

      I have checked and the image file counter.jpg exists in the current directory. But why does the html display a blank image? If you could identify any other possible reasons for this it would be really appreciated.

      thanks grao5

        this happens sporadically with me and windows 2k. 
        I write to model/wrl files and sometimes the browser  
        doesn't render as it should. My fix is the following.
        (Keep your perl script as is with the binmode STDOUT statement in place.)
        In the html page type this:
        <img src="mycounter.cgi?itlooksstupidbutsozwindows.jpg">
        by ending my query string with anything.jpg or anything.wrl
        i was able to convince my browser to stop spordically 
        sending me non-text content as text.
        
Re: Error reading image from perl cgi script into HTML
by BUU (Prior) on Oct 13, 2002 at 16:35 UTC
    And don't forget, since you're on win98,

    BINMODE THE FILE HANDLE.

    (in this case STDOUT)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-03-29 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found