Changeset 3137


Ignore:
Timestamp:
2002-06-07T12:32:11+12:00 (20 years ago)
Author:
paynter
Message:

Changed the way Width, Height, Size and Type metadata is calculated.
Previously, we trapped the STDERR output of convert, which included
this stuff, and parsed it with a complex regexp. The problem is the
format of this output keeps changing. This version stops trying to do
that, and uses the ImageMajick "identify" program instead. It's
slower but more reliable. (That said, I've not tested it on non-Linux
platforms.)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/perllib/plugins/ImagePlug.pm

    r2882 r3137  
    5858"
    5959}
     60
    6061
    6162sub new {
     
    8485}
    8586
     87
    8688sub get_default_process_exp {
    8789    my $self = shift (@_);
     
    118120    }
    119121
    120 
    121122    # Convert the image to a new type (if required).
    122123    my $converttotype = $self->{'converttotype'};
     
    139140    }
    140141   
     142    # Add the image metadata
     143    $doc_obj->add_metadata ($section, "Image", "$file");
     144    my ($image_type, $image_width, $image_height, $image_size)
     145    = &identify($filename, $outhandle, $verbosity);
     146
     147    $doc_obj->add_metadata ($section, "ImageType",   $image_type);
     148    $doc_obj->add_metadata ($section, "ImageWidth",  $image_width);
     149    $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
     150    $doc_obj->add_metadata ($section, "ImageSize",   $image_size);
     151
     152    # Add the image as an associated file
     153    $doc_obj->associate_file($filename,$file,"image/$type",$section);
    141154
    142155    # Make the thumbnail image
    143     my $thumbnailsize = $self->{'thumbnailsize'}  || 100;
     156    my $thumbnailsize = $self->{'thumbnailsize'} || 100;
    144157    my $thumbnailtype = $self->{'thumbnailtype'} || 'gif';
    145    
    146     my $thumbnailfilename = &util::get_tmp_filename() . ".$thumbnailtype";
    147     $self->{'tmp_filename2'} = $thumbnailfilename;
     158    my $thumbnailfile = &util::get_tmp_filename() . ".$thumbnailtype";
     159    $self->{'tmp_filename2'} = $thumbnailfile;
    148160
    149161    # Generate the thumbnail with convert
    150162    my $command = "convert -verbose -geometry $thumbnailsize"
    151     . "x$thumbnailsize $filename $thumbnailfilename";
     163    . "x$thumbnailsize $filename $thumbnailfile";
    152164    print $outhandle "$command\n" if ($verbosity > 2);
    153165    my $result = '';
     
    156168
    157169    # Add the thumbnail as an associated file ...
    158     if (-e "$thumbnailfilename") {
    159     $doc_obj->associate_file("$thumbnailfilename", "thumbnail.$thumbnailtype",
     170    if (-e "$thumbnailfile") {
     171    $doc_obj->associate_file("$thumbnailfile", "thumbnail.$thumbnailtype",
    160172                 "image/$thumbnailtype",$section);
    161173    $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
     
    163175    }
    164176
    165 
    166     # Extract Image metadata from convert output
    167     if ($result =~ m/([0-9]+)x([0-9]+)=>([0-9]+)x([0-9]+)/) {
    168     $doc_obj->add_metadata ($section, "ImageWidth", $1);
    169     $doc_obj->add_metadata ($section, "ImageHeight", $2);
    170     $doc_obj->add_metadata ($section, "ThumbWidth", $3);
    171     $doc_obj->add_metadata ($section, "ThumbHeight", $4);
    172     }
    173    
    174     my $size = "unknown";
    175     if ($result =~ m/^[^\n]* ([0-9]+)b/) {
    176     $size = $1;
    177     } elsif ($result =~ m/^[^\n]* ([0-9]+)kb/) {
    178     $size = 1024 * $1;
    179     }
    180    
    181     if ($result =~ m/^[^\n]*JPE?G/i) {
    182     $type = "jpeg";
    183     } elsif ($result =~ m/^[^\n]*GIF/i) {
    184     $type = "gif";
    185     } elsif ($result =~ m/^[^\n]*PNG/i) {
    186     $type = "png";
    187     } elsif ($result =~ m/^[^\n]*TIF?F/i) {
    188     $type = "tiff";
    189     } elsif ($result =~ m/^[^\n]*BMP/i) {
    190     $type = "bmp";
    191     } elsif ($result =~ m/^[^\n]*XBM?F/i) {
    192     $type = "xbm";
    193     }
    194 
    195     $doc_obj->add_metadata ($section, "ImageType", $type);
    196     $doc_obj->add_metadata ($section, "Image", "$file");
    197     $doc_obj->add_metadata ($section, "ImageSize", $size);
    198    
    199     # Add the image as an associated file ...
    200     $doc_obj->associate_file($filename,$file,"image/$type",$section);
    201        
     177    # Extract Thumnail metadata from convert output
     178    if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
     179    $doc_obj->add_metadata ($section, "ThumbWidth", $1);
     180    $doc_obj->add_metadata ($section, "ThumbHeight", $2);
     181    }
    202182
    203183    # Make a screen-sized version of the picture if requested
    204184    if ($self->{'screenviewsize'}) {
     185
     186    # To do: if the actual image smaller than the screenview size,
     187    # we should use the original !
    205188
    206189    my $screenviewsize = $self->{'screenviewsize'};
     
    235218
    236219    return $type;
    237 
    238 
     220}
     221
     222
     223# Discover the characteristics of an image file with the ImageMagick
     224# "identify" command.
     225
     226sub identify {
     227    my ($image, $outhandle, $verbosity) = @_;
     228
     229    # Use the ImageMagick "identify" command to get the file specs
     230    my $command = "identify $image 2>&1";
     231    print $outhandle "$command\n" if ($verbosity > 2);
     232    my $result = '';
     233    $result = `$command`;
     234    print $outhandle "$result\n" if ($verbosity > 3);
     235
     236    # Read the type, width, and height
     237    my $type =   'unknown';
     238    my $width =  'unknown';
     239    my $height = 'unknown';
     240
     241    if ($result =~ /^$image (\w+) (\d+)x(\d+)/) {
     242    $type = $1;
     243    $width = $2;
     244    $height = $3;
     245    }
     246   
     247    # Read the size
     248    my $size = "unknown";
     249    if ($result =~ m/^.* ([0-9]+)b/) {
     250    $size = $1;
     251    } elsif ($result =~ m/^.* ([0-9]+)kb/) {
     252    $size = 1024 * $1;
     253    }
     254
     255    print $outhandle "file: $image:\t $type, $width, $height, $size\n"
     256    if ($verbosity > 2);
     257
     258    # Return the specs
     259    return ($type, $width, $height, $size);
    239260}
    240261
     
    334355
    3353561;
    336 
    337 
    338 
    339 
    340 
    341 
    342 
    343 
    344 
    345 
    346 
Note: See TracChangeset for help on using the changeset viewer.