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

Hi Monks, I'm using a Perl upload script to upload images to a folder on the server. It works, but is there anyway I can grab the dimensions of the photo and set it to a variable while uploading? I'm using some javascript to grab the filename and stuff it in the parent window's form, but I don't have the width and height which I need to stuff in my database. Here is the upload routine:
open(OUTFILE, ">$target_dir\/$Filename") || die "<font color=red><b>Er +ror: </b></font>Cannot open file"; undef $BytesRead; undef $Buffer; while ($read_bytes = read($Full_name,$Buffer,1024)) { #read file for u +ploading one K at a time $total_bytes += $read_bytes; print OUTFILE $Buffer; } $Confirmation{$Filename} = $total_bytes; close($Full_name); close(OUTFILE); chmod (0777, "$target_dir\/$Filename"); } } $max_filesize = "46080"; #45K $File = "$target_dir\/$Filename";

Replies are listed 'Best First'.
Re: Get jpg dimensions while uploading?
by Eliya (Vicar) on Mar 09, 2011 at 02:20 UTC
      Perfect. Thanks, this worked!
Re: Get jpg dimensions while uploading?
by Anonymous Monk on Mar 09, 2011 at 02:21 UTC
Re: Get jpg dimensions while uploading?
by bart (Canon) on Mar 09, 2011 at 08:29 UTC
    Just an aside: you don't use binmode (both on input and output filehandle). Perhaps it works for you now, but on Windows and with the newer Unicode related behavior of Perl (there's more than text and byte mode for file handles, nowadays), you should.
Re: Get jpg dimensions while uploading?
by ww (Archbishop) on Mar 09, 2011 at 02:37 UTC
    Eliya beat me to it + + (by a lot) with an excellent option. See the doc -- first example is basicly your ready-made solution.
Re: Get jpg dimensions while uploading?
by Khen1950fx (Canon) on Mar 09, 2011 at 08:13 UTC
    I tried it with CGI.
    #!/usr/bin/perl use strict; use warnings; use CGI; use Number::Bytes::Human qw(format_bytes); my $filename = '/root/Desktop/moon.jpg'; my $query = CGI->new(-s $filename); my $size = format_bytes($query->keywords); print $size, "\n";