Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Unexpected Image::Info failure

by sulfericacid (Deacon)
on Feb 25, 2006 at 15:48 UTC ( [id://532773]=perlquestion: print w/replies, xml ) Need Help??

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

I've been having this problem on one of my web sites yesterday, but I thought it may have to do with my hotlink protection/direct url requests for images settings so I moved it over to another site and the following script fails.

It fails saying "Can't parse image info: Can't open http://www.learntogoogle.com/images/addform.jpg: No such file or directory". For those who want to see for sure, the image loads perfectly in the browser with this path.

I can't get Image::Info to open the image and I can't figure out why.

#!/usr/bin/perl use warnings; use strict; use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; print header, start_html; my $img_src = "http://www.learntogoogle.com/images/addform.jpg"; use Image::Info qw(image_info dim); my $info = image_info($img_src); if ( my $error = $info->{error} ) { die "Can't parse image info: $error\n"; } my ( $width, $height ) = dim($info);
What could cause this to report about not being able to find the image when it can? Would it die like this if it didn't have permission to use the image off my server?

UPDATE I put some more tests in (as seen below). The image prints on page successfully but the -e test fails. Now I'm assuming it has to be some server issue.

#!/usr/bin/perl use warnings; use strict; use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; print header, start_html; my $img_src = "http://www.learntogoogle.com/images/addform.jpg"; print qq(<img src="$img_src">); use Image::Info qw(image_info dim); my $info = image_info($img_src); if (-e $img_src) { print "Image was found."; } else { print "Image is not available"; } if ( my $error = $info->{error} ) { die "Can't parse image info: $error\n"; } my ( $width, $height ) = dim($info);


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Unexpected Image::Info failure
by zentara (Archbishop) on Feb 25, 2006 at 16:41 UTC
    I think you are assuming that because you can see an image included in an html link
    print qq(<img src="$img_src">);
    that you actually have the file in your filesystem. You don't. To run file info on the file, you need to get it with lwp.

    Even when you can see it in your browser, due to your cgi making a link to it, it is buried somewhere in the browser's cache.


    I'm not really a human, but I play one on earth. flash japh
      Good point. Not familiar with Image::Info. Figured it took urls as a parameter. Guess not.


      -Lee
      "To be civilized is to deny one's nature."
      Are you saying you can't load the image then?

      I used LWP on $img_src and it prints back lots of ooblygoop. So it looks like it finds the image.



      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid

        Yes, because LWP makes an HTTP request and retrieves the contents. Perl's open will not open an URL. URLs are not local paths, hence the open and -e fail.

        What is happening is some html magic. The browser will automatically retreive an imageURL if it's in the html tag for the image, and it will end up in the browser's cache. So your cgi program is telling the client's browser to "look at this url" for an image, and the browser does it. So if you open this html file on your local machine, and open it in a browser, you will see the image, but the image is buried deep in the browser's catch.
        <HTML> <BODY> <IMG SRC="http://www.zentara.net/anim_a_s_r.gif" HEIGHT=65 WIDTH=75 a +lt="[zentara]" align=left> </BODY> </HTML>
        So your cgi script does nothing but pass that url to the browser, it does not get the file.

        The gobbldeeGook which you get when you use lwp to retreive the file, is the actual binary data of the image. You need to save it to a scalar( or a temp file) then run your image info routine on it.

        #!/usr/bin/perl use warnings; use strict; use LWP::Simple; use Image::Info qw(image_info dim); my $data = get("http://www.learntogoogle.com/images/addform.jpg"); # this will print binary junk #print "$data\n"; my $info = image_info( \$data ); if (my $error = $info->{error}) { die "Can't parse image info: $error\n"; } my($w, $h) = dim($info); print "$w $h\n";

        I'm not really a human, but I play one on earth. flash japh
Re: Unexpected Image::Info failure
by shotgunefx (Parson) on Feb 25, 2006 at 15:56 UTC
    Not sure, but in Firefox, if you display the alt-text (seeing their's no html, I'm assuming the alt-text is coming from the rendering of the jpg)

    The image “http://www.learntogoogle.com/images/addform.jpg” cannot be displayed, because it contains errors.


    -Lee
    "To be civilized is to deny one's nature."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found