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

Hi guys, I am still having problems with parsing an image's size from a URL (not local path).

Sometimes it determines the size, sometimes it doesn't and I can't quite figure out what the problem might be.

Also, the sizer at the bottom adds only images that are TOO BIG or HAVE NO SIZE and I need the opposite, that's sort of a bug, too, that I can't fix.

Can someone help with one or both of these issues?

if ($limit_img_size) { @image_urls = grep { my $this_url = $_; my $image = get($this_url); print "Getting size of image $this_url<br>"; my ($height, $width) = imgsize(\$image) or die "Error $!"; print "IMG $this_url is $height and $width<br>"; $height <= $max_height && $width <= $max_width && $height ne + "" && $width ne "" } @image_urls; } push @found_images, @image_urls; }
Here is some output of my last run. All image URLs do work but I wouldn't suggest users go to them as who knows what they might be.

Getting size of image http://images.imagefap.com/images/full/8/175/175 +3983749.jpg IMG http://images.imagefap.com/images/full/8/175/1753983749.jpg is and Getting size of image http://images.imagefap.com/images/full/6/123/123 +2501810.jpg IMG http://images.imagefap.com/images/full/6/123/1232501810.jpg is and Getting size of image http://images.imagefap.com/images/full/6/750/750 +454169.jpg IMG http://images.imagefap.com/images/full/6/750/750454169.jpg is and Getting size of image http://images.imagefap.com/images/full/7/173/173 +3232860.jpg IMG http://images.imagefap.com/images/full/7/173/1733232860.jpg is 675 + and 900 Getting size of image http://images.imagefap.com/images/full/8/802/802 +952715.jpg IMG http://images.imagefap.com/images/full/8/802/802952715.jpg is 900 +and 675 Getting size of image http://images.imagefap.com/images/full/7/770/770 +728945.jpg IMG http://images.imagefap.com/images/full/7/770/770728945.jpg is 675 +and 900 Getting size of image http://images.imagefap.com/images/full/5/121/121 +2027075.jpg IMG http://images.imagefap.com/images/full/5/121/1212027075.jpg is 675 + and 900 # # this is a printout of @found_images, as you can see # it only stored images that were too large or did not # have sizes # found images follow http://images.imagefap.com/images/full/8/175/1753983749.jpg -http://images.imagefap.com/images/full/6/123/1232501810.jpg -http://images.imagefap.com/images/full/6/750/750454169.jpg -http://images.imagefap.com/images/full/7/173/1733232860.jpg -http://images.imagefap.com/images/full/8/802/802952715.jpg -http://images.imagefap.com/images/full/7/770/770728945.jpg -http://images.imagefap.com/images/full/5/121/1212027075.jpg

Replies are listed 'Best First'.
Re: determining image size by a URL
by jhourcle (Prior) on Jul 07, 2006 at 17:27 UTC

    From the Image::Size documentation:

    imgsize(stream)
    Returns a three-item list of the X and Y dimensions (width and height, in that order) and image type of stream. Errors are noted by undefined (undef) values for the first two elements, and an error string in the third. The third element can be (and usually is) ignored, but is useful when sizing data whose type is unknown.

    So, you're going to want to look in the third element returned to see what went wrong.

      Thanks for that, I passed a third arguement $error and now ALL the errors are the same Data stream is not a known image file format but I don't see how that is. All the images are JPG files with full URLs.

      For example http://images.imagefap.com/images/full/9/144/1448694657.jpg returns with that error (it's a clean picture). What could cause this error then? Could the server possibly loaded too slow and it timed out?

        Print the stream out to a file, and look at it -- it may be that what you see in a web browser is different from what the program sees, due to browser sniffing techniques, content negotiation, etc.

        Just because you get an image in your web browser doesn't mean that your program's going to get an image. (they don't look to have a robots.txt, but it's possible they have some other way of slowing down requests hitting their server too quickly)

        Could be due to a bug in Image::Size or in your code. Is imgsize() always returning three values? If not, only $width and $height are set by the assignment, and $error remains unchanged if there was no error. Try setting $error explicitely to '' before calling imgsize().
Re: determining image size by a URL
by ww (Archbishop) on Jul 07, 2006 at 17:42 UTC
    Re the intermittent reporting of sizes: numerous "standards" (and wanna'-be-standards) call for <img ...> tags to include ... width="nn"... (where nn is a value in pixels) and height (similarly) arguments.

    But, as with most things webbish, many programs and/or people ignore those standards, posting html like this:

    <img src="/graphics/foo.jpg">

    Your introductory line, "...parsing an image's size from a URL (not local path)" implies something very different than what jhourcle (above) discusses. That node may reflect your intent more precisely, but if I take your quoted remark litterally, I don't think it can be done because there's no info about the size(s) of any contained image(s) in a URL itself.

    However, getting sizes from properly formatted <img...> tags at a URL is readily do-able if the code at the address in question actually includes the requisite info.

    The only ready alternative (that I see but Nota Bene: jhourcle 's above) for sites using non-compliant image tags is to feed your program with full_paths_to_images (by name) and pipe the images to a graphics package or program such as ImageMagick or the gimp. (For that, you may want to search here re Tk + image or gtk + image, where you may find help -- pay special attention to anything Zentara wrote).

    You may wish to start with Corion's notes here or -- if you need background on images especially note Merlyn's and thpfft's replies to the referenced node.

    FOOTNOTE, UPDATE With considerable chagrin, I note that even as I toiled over this largely wasted observation, Brother jhourcle correctly ( inferred||intuited||leapt to the correct conclusion ); namely, that your question referred to the module Image::Size. IMO, it would have been nice to mention that explicitly in the OP.