Changeset 2230


Ignore:
Timestamp:
2001-03-29T13:26:42+12:00 (23 years ago)
Author:
paynter
Message:

User can erquest a "Screen" image - essentially a second thumbnail. This
lets you get a reasonable look at very large images.

File:
1 edited

Legend:

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

    r2226 r2230  
    3232}
    3333
    34 use strict;
    3534
    3635sub print_usage {
     
    4039  usage: plugin ImagePlug [options]
    4140
    42    -noscaleup       Don't scale up small images when making thumbnails
    43 
    44    -thumbnailtype s Make thumbnails in format 's'
    45 
    46    -thumbnailsize n Make thumbnails of size nxn
    47 
    48    -convertto s     Convert main inage to (gif|png|jpg)
    49 
    50    -minimumsize n   Ignore images smaller than n bytes
     41   -noscaleup         Don't scale up small images when making thumbnails
     42
     43   -thumbnailsize n   Make thumbnails of size nxn
     44
     45   -thumbnailtype s   Make thumbnails in format 's'
     46
     47   -screenviewsize n  If set, makes an image of size n for screen display
     48                      and sets Screen, ScreenSize, ScrrenWidth and Screeneight
     49                      metadata.  By default it is not set.
     50
     51   -screenviewtype s  If -screenviewsize is set, this sets the screen display
     52                      image type.  Defaults to jpg.
     53
     54   -convertto s       Convert main inage to (gif|png|jpg)
     55
     56   -minimumsize n     Ignore images smaller than n bytes
    5157
    5258"
     
    6066    if (!parsargv::parse(\@_,
    6167             q^noscaleup^, \$self->{'noscaleup'},
     68             q^converttotype/.*/^, \$self->{'converttotype'},
     69             q^minimumsize/[0-9]*/100^, \$self->{'minimumsize'},
     70
     71             q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
    6272             q^thumbnailtype/.*/gif^, \$self->{'thumbnailtype'},
    63              q^converttotype/.*/^, \$self->{'converttotype'},
    64              q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
    65              q^minimumsize/[0-9]*/100^, \$self->{'minimumsize'},
     73             q^screenviewsize/[0-9]*/0^, \$self->{'screenviewsize'},
     74             q^screenviewtype/.*/jpg^, \$self->{'screenviewtype'},
    6675             "allow_extra_options")) {
    6776   
     
    8190}
    8291
     92
     93# Create the thumbnail and screenview images, and discover the Image's
     94# size, width, and height using the convert utility.
     95
    8396sub run_convert {
    8497    my $self = shift (@_);
    85     my $filename = shift (@_);
    86     my $file = shift (@_);
     98    my $filename = shift (@_);   # filename with full path
     99    my $file = shift (@_);       # filename without path
    87100    my $doc_obj = shift (@_);
    88101    my $section = $doc_obj->get_top_section();
    89 
    90     if ($file eq "" || $filename eq "") {
    91     return "";
    92     }
    93 #    if ($filename =~ m/thumbnail/) {
    94 #   return "";
    95 #    }
    96 #    if ($filename =~ m/converted/) {
    97 #   return "";
    98 #    }
     102   
     103    my $verbosity = $self->{'verbosity'};
     104    my $outhandle = $self->{'outhandle'};
     105
     106    # check the filename is okay
     107    return 0 if ($file eq "" || $filename eq "");
     108
    99109    if ($filename =~ m/ /) {
    100     print STDERR "IamgePlug: \"$filename\" contains a space. choking.\n";
    101     return "";
     110    print $outhandle "ImagePlug: \"$filename\" contains a space. choking.\n";
     111    return undef;
    102112    }
    103113
    104114    my $minimumsize = $self->{'minimumsize'};
    105     my $thumbSize = $self->{'thumbnailsize'};
    106     if ($thumbSize eq "") { $thumbSize = 100; };
     115    if (defined $minimumsize && (-s $filename < $minimumsize)) {
     116        print $outhandle "ImagePlug: \"$filename\" too small, skipping\n"
     117        if ($verbosity > 1);
     118    }
     119
     120
     121    # Convert the image to a new type (if required).
    107122    my $converttotype = $self->{'converttotype'};
    108     my $thumbnailtype = $self->{'thumbnailtype'};
    109     if ($thumbnailtype eq "") { $thumbnailtype = "gif"; };
    110     my $originalfilename = ""; # only set if we do a conversion
    111     my $thumbnailfilename = "";
     123    my $originalfilename = "";  # only set if we do a conversion
     124    my $type = "unknown";
     125
     126    if ($converttotype ne "" && $filename =~ m/$converttotype$/) {
     127
     128    $originalfilename = $filename;
     129    $filename = &util::get_tmp_filename() . ".$converttotype";
     130    $self->{'tmp_filename'} = $filename;
     131
     132    my $command = "convert -verbose $originalfilename $filename";
     133    print $outhandle "$command\n" if ($verbosity > 2);
     134    my $result = '';
     135    $result = `$command`;
     136    print $outhandle "$result\n" if ($verbosity > 3);
     137
     138    $type = $converttotype;
     139    }
     140   
     141
     142    # Make the thumbnail image
     143    my $thumbnailsize = $self->{'thumbnailsize'}  || 100;
     144    my $thumbnailtype = $self->{'thumbnailtype'} || 'gif';
    112145   
    113     my $type = "unknown";
    114 
    115     if (defined $minimumsize && (-s $filename < $minimumsize)) {
    116         print STDERR "ImagePlug: \"$filename\" too small, skipping\n"
    117         if $self->{'verbosity'} > 1;
    118     }
    119     #see if we need to convert ...
    120     if ($converttotype ne "" && $filename =~ m/$converttotype$/) {
    121     $originalfilename = $filename;
    122     $filename = &util::get_tmp_filename();
    123     $filename = "$filename.$converttotype";
    124     $self->{'tmp_filename'} = $filename;
    125     if (-e $filename) {
    126         print STDERR "File names to convert from and to are the same. choking in Imageplug on \"$filename\"\n";
    127         return "";
    128         }
    129     my $result = "";
    130     my $command = "convert -verbose $originalfilename $filename";
    131     $result = `$command`;
    132     print STDERR "$command\n"
    133         if $self->{'verbosity'} > 2;
    134     print STDERR "$result\n"
    135         if $self->{'verbosity'} > 3;
    136     $type = $converttotype;
    137     }
    138    
    139     #check that the thumbnail doesn't already exist ...
    140     $thumbnailfilename = &util::get_tmp_filename();
    141     $thumbnailfilename = $thumbnailfilename . ".thumbnail.$thumbnailtype";
     146    my $thumbnailfilename = &util::get_tmp_filename() . ".$thumbnailtype";
    142147    $self->{'tmp_filename2'} = $thumbnailfilename;
    143148
    144     #make the thumbnail
    145     my $result = "";
    146     my $command = "convert -verbose -geometry $thumbSize" . "x$thumbSize $filename $thumbnailfilename";
     149    # Generate the thumbnail with convert
     150    my $command = "convert -verbose -geometry $thumbnailsize"
     151    . "x$thumbnailsize $filename $thumbnailfilename";
     152    print $outhandle "$command\n" if ($verbosity > 2);
     153    my $result = '';
    147154    $result = `$command` ;
    148     print STDERR "$command\n"
    149         if $self->{'verbosity'} > 2;
    150     print STDERR "$result\n"
    151         if $self->{'verbosity'} > 3;
    152    
     155    print $outhandle "$result\n" if ($verbosity > 3);
     156
     157    # Add the thumbnail as an associated file ...
     158    if (-e "$thumbnailfilename") {
     159    $doc_obj->associate_file("$thumbnailfilename", "thumbnail.$thumbnailtype",
     160                 "image/$thumbnailtype",$section);
     161    $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
     162    $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
     163    }
     164
     165
     166    # Extract Image metadata from convert output
    153167    if ($result =~ m/([0-9]+)x([0-9]+)=>([0-9]+)x([0-9]+)/) {
    154168    $doc_obj->add_metadata ($section, "ImageWidth", $1);
     
    161175    if ($result =~ m/^[^\n]* ([0-9]+)b/) {
    162176    $size = $1;
    163     }
    164     if ($result =~ m/^[^\n]* ([0-9]+)kb/) {
     177    } elsif ($result =~ m/^[^\n]* ([0-9]+)kb/) {
    165178    $size = 1024 * $1;
    166179    }
     
    168181    if ($result =~ m/^[^\n]*JPE?G/i) {
    169182    $type = "jpeg";
    170     }
    171     if ($result =~ m/^[^\n]*GIF/i) {
     183    } elsif ($result =~ m/^[^\n]*GIF/i) {
    172184    $type = "gif";
    173     }
    174     if ($result =~ m/^[^\n]*PNG/i) {
     185    } elsif ($result =~ m/^[^\n]*PNG/i) {
    175186    $type = "png";
    176     }
    177     if ($result =~ m/^[^\n]*TIF?F/i) {
     187    } elsif ($result =~ m/^[^\n]*TIF?F/i) {
    178188    $type = "tiff";
    179     }
    180     if ($result =~ m/^[^\n]*BMP/i) {
     189    } elsif ($result =~ m/^[^\n]*BMP/i) {
    181190    $type = "bmp";
    182     }
    183     if ($result =~ m/^[^\n]*XBM?F/i) {
     191    } elsif ($result =~ m/^[^\n]*XBM?F/i) {
    184192    $type = "xbm";
    185193    }
    186194
    187     #if there's a leading directory name, eat it...
    188     $file =~ s/^.*[\/\\]//;
    189    
    190195    $doc_obj->add_metadata ($section, "ImageType", $type);
    191196    $doc_obj->add_metadata ($section, "Image", "$file");
    192197    $doc_obj->add_metadata ($section, "ImageSize", $size);
    193198   
    194     #add the thumbnail as an associated file ...
    195     if (-e "$thumbnailfilename") {
    196     $doc_obj->associate_file("$thumbnailfilename","thumbnail.$thumbnailtype","image/$thumbnailtype",$section);
    197     $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
    198     $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
    199     } else {
    200     print STDERR "ImagePlug: couldn't find \"$thumbnailfilename\"\n";
    201     }
    202    
    203     #add the image as an associated file ...
     199    # Add the image as an associated file ...
    204200    $doc_obj->associate_file($filename,$file,"image/$type",$section);
    205201       
     202
     203    # Make a screen-sized version of the picture if requested
     204    if ($self->{'screenviewsize'}) {
     205
     206    my $screenviewsize = $self->{'screenviewsize'};
     207    my $screenviewtype = $self->{'screenviewtype'} || 'jpeg';
     208    my $screenviewfilename = &util::get_tmp_filename() . ".$screenviewtype";
     209    $self->{'tmp_filename3'} = $screenviewfilename;
     210
     211    # make the screenview image
     212    my $command = "convert -verbose -geometry $screenviewsize"
     213        . "x$screenviewsize $filename $screenviewfilename";
     214    print $outhandle "$command\n" if ($verbosity > 2);
     215    my $result = "";
     216    $result = `$command` ;
     217    print $outhandle "$result\n" if ($verbosity > 3);
     218
     219    # get screenview dimensions, size and type
     220        if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
     221        $doc_obj->add_metadata ($section, "ScreenWidth", $1);
     222        $doc_obj->add_metadata ($section, "ScreenHeight", $2);
     223    }
     224   
     225    #add the screenview as an associated file ...
     226    if (-e "$screenviewfilename") {
     227        $doc_obj->associate_file("$screenviewfilename", "screenview.$screenviewtype",
     228                     "image/$screenviewtype",$section);
     229        $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
     230        $doc_obj->add_metadata ($section, "Screen", "screenview.$screenviewtype");
     231    } else {
     232        print $outhandle "ImagePlug: couldn't find \"$screenviewfilename\"\n";
     233    }
     234    }
     235
    206236    return $type;
     237
     238
    207239}
    208240
     
    225257    my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
    226258
     259    my $outhandle = $self->{'outhandle'};
     260
    227261    my $filename = &util::filename_cat($base_dir, $file);
    228262    return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
     
    230264    return undef;
    231265    }
    232     print STDERR "ImagePlug processing \"$filename\"\n"
     266    print $outhandle "ImagePlug processing \"$filename\"\n"
    233267        if $self->{'verbosity'} > 1;
    234268
    235     $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
     269    #if there's a leading directory name, eat it...
     270    $file =~ s/^.*[\/\\]//;
    236271   
    237272    # create a new document
     
    269304    $processor->process($doc_obj);
    270305
     306    # clean up temporary files - we do this here instead of in 
     307    # run_convert becuase associated files aren't actually copied
     308    # until after process has been run.
    271309    if (defined $self->{'tmp_filename'} &&
    272310    -e $self->{'tmp_filename'}) {
    273       util::rm($self->{'tmp_filename'})
     311      &util::rm($self->{'tmp_filename'})
    274312    }
    275313    if (defined $self->{'tmp_filename2'} &&
    276314    -e $self->{'tmp_filename2'}) {
    277       util::rm($self->{'tmp_filename2'})
     315      &util::rm($self->{'tmp_filename2'})
     316    }
     317    if (defined $self->{'tmp_filename3'} &&
     318    -e $self->{'tmp_filename3'}) {
     319      &util::rm($self->{'tmp_filename3'})
    278320    }
    279321
Note: See TracChangeset for help on using the changeset viewer.