#!/usr/bin/perl use strict; use warnings; use LWP::Simple; my $url = 'http://ec2.images-amazon.com/images/P/4757215339.01.MZZZZZZZ.jpg'; my $content = get($url) or die "Couldn't get $url"; print "$url : ", isjpeg(\$content); if (0){ # test my $fn = shift; my $content; print "$fn:\n"; printf "\tFilename: %d\n", isjpeg($fn); open my $fh, $fn or die "$fn:$!"; printf "\tTypeglob: %d\n", isjpeg($fh); local $/; $content = <$fh>; close $fh; printf "\tContent: %d\n", isjpeg(\$content); } sub isjpeg { my $unknown = shift; my ( $soi, $eoi, $jfif ); if ( ref $unknown eq 'SCALAR' ) { $soi = substr( $$unknown, 0, 2 ); $jfif = substr( $$unknown, 6, 4 ); $eoi = substr( $$unknown, -2, 2 ); } else { require Carp; my $fh; if ( !ref $unknown ) { open $fh, '<', $unknown or Carp::croak("$unknown:$!"); } elsif ( ref $unknown eq 'GLOB' ) { $fh = $unknown; } else { Carp::croak("unknown type: ", ref $unknown); } binmode $fh, ':raw'; read $fh, $soi, 2; seek $fh, 4, 1; read $fh, $jfif, 4; seek $fh, -2, 2; read $fh, $eoi, 2; ref $unknown ? seek $fh, 0, 0 : close $fh; } return $soi ne "\xff\xD8" ? 0 : $eoi ne "\xff\xD9" ? 0 : $jfif ne 'JFIF' ? 1 : 2; }