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

I posted a recent SOPW and the code I've been told to use is below. For the life of me I cannot get imageinfo to parse my image file, it constantly errors out about division by zero. My test $h and $w printout are blank.

I can access the image URL in my browser just fine, so it can't be a permission problem. Anyone have any ideas what might be wrong?

use Image::Info qw(image_info dim); my $img_info = image_info("http://www.myurl.org/pictures/$pic_id.jpg") +; my ($h, $w) = dim($img_info); print "$h and $w, check the URL of image at http://www.myurl.org/pictu +res/$pic_id.jpg"; my ($max_width, $max_height ) = (800, 600); my ($width_factor, $height_factor) = ($max_width/$width, $max_height/$ +height); if ($width_factor < 1 || $height_factor < 1) { my $factor = ($width_factor < $height_factor ? $width_factor : $hei +ght_factor ); $width = int($width * $factor + 0.5); $height = int($height * $factor + 0.5); }

Replies are listed 'Best First'.
Re: ImageInfo errors
by Joost (Canon) on Feb 22, 2007 at 20:26 UTC
Re: ImageInfo errors
by zentara (Cardinal) on Feb 22, 2007 at 21:39 UTC
    To give an example of what Joost said:
    #!/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. Cogito ergo sum a bum
      You could save time by just downloading the first 1KB of the image. (IIRC, the location of the image size varies in JPGs, but it's very near the beginning 99.999% of the time.) This can be done using the HTTP Range header field. A sample value would be bytes=0-1023.