Just tried this code in May 2020 works for .gif .png but was returning blank results for .jpg files. The file type was recognised but the size marker string was not found.
$ file sample.jpg sample.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), den +sity 123x123, segment length 16, progressive, precision 8, 1024x1448, + frames 3 $ perl -v This is perl 5, version 22, subversion 1 (v5.22.1) built for darwin-th +read-multi-2level $ hexdump -C sample.jpg | head -30 00000000 ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 7b |......JFI +F.....{| 00000010 00 7b 00 00 ff db 00 43 00 01 01 01 01 01 01 01 |.{.....C. +.......| 00000020 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 |......... +.......|* 00000090 01 01 01 01 01 01 01 01 01 01 01 01 01 01 >>>> ff c2 <<<< + |................| 000000a0 00 11 08 >>> 05 a8 04 00 <<<< 03 01 22 00 02 11 01 03 11 | +........."......| 000000b0 01 ff c4 00 1e 00 01 00 01 04 03 01 01 00 00 00 |......... +.......| 000000c0 00 00 00 00 00 00 00 06 05 07 08 09 03 04 0a 02 |......... +.......|
Looking for size bytes 05 a8 04 00 and a marker a few bytes before shows the marker to be ff c2 . Changing
elsif ( $_[0] =~ m/^^\xFF\xD8.{4}JFIF/s ) { $sig = 'JPEG'; # found JPG signature ($height,$width) = unpack( "nn", $1 ) if $_[0] =~ /\xFF\xC0...(....)/s; to elsif ( $_[0] =~ m/^^\xFF\xD8.{4}JFIF/s ) { $sig = 'JPEG'; ($height,$width) = unpack( "nn", $1 ) if $_[0] =~ /\xFF\xC2...(....)/s; along with the driver for my $img ( qw( ./sample.jpg ./sampleC.jpg ) ) { my $data = get_file($img); my @res = image_size($data); print "\nNative $img $res[2] width $res[0] height $res[1]\n"; ($x, $y) = imgsize($img); print "Image::Size ($x, $y)\n"
gives a working code set. Adding the Image::Size method to test along size gives
Native ./sample.jpg JPEG width 1024 height 1448 Image::Size (1024, 1448) Native ./sampleC.jpg JPEG width 617 height 1220 Image::Size (617, 1220)
Not sure what changed or if this is just a mac thing but wanted to leave this testament for following monks.

In reply to Re: Image size in pure perl by gannett
in thread Image size in pure perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.