in reply to Image size in pure perl

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.

Replies are listed 'Best First'.
Re^2: Image size in pure perl
by gannett (Novice) on May 27, 2020 at 13:51 UTC
    Looking at the Wiki page for jpeg I see that the size marker tag is 0xFF 0xCO for baseline images and 0xFF 0xC2 for progressive images. Also marker for a JFIF is 0xFF 0xD8 0xFF Updating complete sub image_size to ...
    sub image_size { return unless $_[0]; my ($width, $height, $sig); if ( $_[0] =~ m/^GIF8..(....)/s ) { $sig = 'GIF'; # found GIF signature ($width, $height) = unpack( "SS", $1 ); } elsif ( $_[0] =~ m/^^\xFF\xD8\xFF.{3}JFIF/s ) { $sig = 'JPEG'; ($height,$width) = unpack( "nn", $1 ) if $_[0] =~ /\xFF\xC0...(....)/s; #Baseline Image ($height,$width) = unpack( "nn", $1 ) if $_[0] =~ /\xFF\xC2...(....)/s; #Pogressive Images } elsif ( $_[0] =~ /^\x89PNG\x0d\x0a\x1a\x0a/ ) { $sig = 'PNG'; # found PNG signature ($width, $height) = unpack( "NN", $1 ) if $_[0] =~ /IHDR(.{8})/s; } elsif ( $_[0] =~ /BM.{16}(.{8})/s ) { $sig = 'BMP'; # found bitmap sig ($width, $height) = unpack( "LL", $1); } return $width, $height, $sig; }