Hi All, I know this is a 10 year old thread, but I stumbled across it as I was searching for the same thing... Here is my solution:
sub _getJpegDimensions { my $path = shift; # read first 100kb - this should be enough to get the SOF sec +tion. open my $fh, '<', $path; read $fh, my $data, 102400; close $fh; my @chars = split("", $data); my @asciis = map { ord() } @chars; # convert all chars to ascii v +alues # jpg images start with xff xd8, so check this first and remov +e if found if ((shift @asciis == 0xff) and (shift @asciis == 0xd8)) { # loop through each section until the SOF section is found while(@asciis) { my $val = shift @asciis; # check if $val is possibly the start of a marker (all + markers are 0xffXX) if ( $val != 0xff ) { next; } else { $val = shift @asciis; # Start of frame marker is usually 0xffc0 # check for c0 to c3 to include extended sequentia +l, progressive and lossless markers if (($val >= 0xc0) and ($val <= 0xc3)) { # SOF block is [0xffc0][ushort length][uchar preci +sion][ushort x][ushort y] # [0xffc0][ 2 Bytes ][ 1 Byte + ][2 Bytes ][2 Bytes ] # note that x is height, y is width my $width = ($asciis[5] * 256) + $asciis[6]; # +convert the 2 byte ushort into a decimal number my $height = ($asciis[3] * 256) + $asciis[4]; return ($width, $height); } else { # if this isnt an SOF section, skip the rest # section length is immediately after marker # section length includes marker, hence why 2 is s +ubtracted my $section_length = shift @asciis; if ($section_length - 2 > 0) { splice @asciis, 0, $section_length - 2; } } } } } else { return (0,0); } }
This is called as follows:
my ($width, $height) = &_getJpegDimensions("path to jpg as string");
Note - this code makes the following assumptions: 1. 100kb is enough to get the SOF header. 2. The .jpg is encoded using Baseline, Extended Sequential, Progressive or Lossless schemes. Hope this helps! Jon

In reply to Re: getting the dimensions of a picture (jpeg) by JonBass
in thread getting the dimensions of a picture (jpeg) 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.