Ignore:
Timestamp:
2000-12-05T23:20:11+13:00 (23 years ago)
Author:
say1
Message:

about a billion changes to ImagePlug

File:
1 edited

Legend:

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

    r1735 r1744  
    3232}
    3333
     34use strict;
     35sub print_general_usage {
     36    my ($plugin_name) = @_;
     37
     38    print STDERR "\n  usage: plugin $plugin_name [options]\n\n";
     39    print STDERR "   -noscaleup       Don't scale up small images when making thumbnails\n";
     40    print STDERR "   -thumbnailtype s Make thumbnails in format 's'\n";
     41    print STDERR "   -thumbnailsize n Make thumbnails of size nxn\n";
     42    print STDERR "   -convertto s     Convert main inage to (gif|png|jpg)\n";
     43    print STDERR "\n";
     44}
     45
    3446sub new {
    3547    my ($class) = @_;
     48    my $plugin_name = shift (@_);
    3649    my $self = new BasPlug ("ImagePlug", @_);
     50    # general options available to all plugins
     51
     52    if (!parsargv::parse(\@_,
     53             q^noscaleup^, \$self->{'noscaleup'},
     54             q^thumbnailtype/.*/gif^, \$self->{'thumbnailtype'},
     55             q^converttotype/.*/^, \$self->{'converttotype'},
     56             q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
     57             "allow_extra_options")) {
     58   
     59    print STDERR "\nThe $plugin_name plugin uses an incorrect general option (general options are those\n";
     60    print STDERR "available to all plugins). Check your collect.cfg configuration file.\n";
     61        &print_general_usage($plugin_name);
     62    die "\n";
     63    }
    3764
    3865    return bless $self, $class;
     
    4269    my $self = shift (@_);
    4370
    44     return q^(?i)(\.jpe?g|\.gif|\.png|\.tiff?)$^;
     71    return q^(?i)(\.jpe?g|\.gif|\.png|\.bmp|\.tiff?)$^;
    4572}
    4673
     
    5077    my $file = shift (@_);
    5178    my $doc_obj = shift (@_);
    52 
     79    my $section = $doc_obj->get_top_section();
     80
     81    if ($file eq "" || $filename eq "") {
     82    return "";
     83    }
     84    if ($filename =~ m/thumbnail/) {
     85    return "";
     86    }
     87    if ($filename =~ m/converted/) {
     88    return "";
     89    }
     90    if ($filename =~ m/\..*\./) {
     91    return "";
     92    }
     93
     94    my $thumbSize = $self->{'thumbnailsize'};
     95    if ($thumbSize eq "") { $thumbSize = 100; };
     96    my $converttotype = $self->{'converttotype'};
     97    my $thumbnailtype = $self->{'thumbnailtype'};
     98    if ($thumbnailtype eq "") { $thumbnailtype = "gif"; };
     99    my $originalfilename = ""; # only set if we do a conversion
     100    my $thumbnailfilename = "";
     101   
    53102    my $type = "unknown";
    54 
    55     if (-e "$filename.thumbnail.gif") {
    56     print "ImagePlug: both \"$filename\" and \"$filename.thumbnail.gif\" exist.\n";
    57     return undef;
    58     }
    59 
     103   
     104    #see if we need to convert ...
     105    if ($converttotype ne "" && $filename =~ m/$converttotype$/)
     106    {
     107    $originalfilename = $filename;
     108    $filename = &util::get_tmp_filename();
     109    $filename = "$filename.$converttotype";
     110    $self->{'tmp_filename'} = $filename;
     111    if (-e $filename) {
     112        print STDERR "File names to convert from and to are the same. choking in Imageplug on \"$filename\"\n";
     113        return "";
     114        }
     115    my $result = "";
     116    my $command = "convert -verbose $originalfilename $filename";
     117    $result = `$command`;
     118    print STDERR "$command\n"
     119        if $self->{'verbosity'} > 2;
     120    print STDERR "$result\n"
     121        if $self->{'verbosity'} > 3;
     122    $type = $converttotype;
     123    }
     124   
     125    #check that the thumbnail doesn't already exist ...
     126    $thumbnailfilename = &util::get_tmp_filename();
     127    $thumbnailfilename = $thumbnailfilename . ".thumbnail.$thumbnailtype";
     128    $self->{'tmp_filename2'} = $thumbnailfilename;
     129
     130    #make the thumbnail
    60131    my $result = "";
    61     $result = `convert -verbose -loop 1 -delay 1 -geometry 100x100 $filename $filename.thumbnail.gif` ;
    62     my $section = $doc_obj->get_top_section();
    63 
    64     if ($result =~ m/=>.* ([0-9]+)x([0-9]+)=>([0-9]+)x([0-9]+)/) {
     132    my $command = "convert -verbose -geometry $thumbSize" . "x$thumbSize $filename $thumbnailfilename";
     133    $result = `$command` ;
     134    print STDERR "$command\n"
     135        if $self->{'verbosity'} > 2;
     136    print STDERR "$result\n"
     137        if $self->{'verbosity'} > 3;
     138   
     139    if ($result =~ m/=>.* ([0-9]+)x([0-9]+)=>([0-9]+)x([0-9]+)/m) {
    65140    $doc_obj->add_metadata ($section, "ImageHeight", $1);
    66141    $doc_obj->add_metadata ($section, "ImageWidth", $2);
     
    68143    $doc_obj->add_metadata ($section, "ThumbWidth", $4);
    69144    }
    70     my $size = "";
     145   
     146    my $size = "unknown";
    71147    if ($result =~ m/^[^\n]* ([0-9]+)b/) {
    72148    $size = $1;
     
    75151    $size = 1024 * $1;
    76152    }
    77 
    78     if ($result =~ m/^[^\n]*JPE?G/) {
     153   
     154    if ($result =~ m/^[^\n]*JPE?G/i) {
    79155    $type = "jpeg";
    80156    }
    81     if ($result =~ m/^[^\n]*GIF/) {
     157    if ($result =~ m/^[^\n]*GIF/i) {
    82158    $type = "gif";
    83159    }
    84     if ($result =~ m/^[^\n]*PNG/) {
     160    if ($result =~ m/^[^\n]*PNG/i) {
    85161    $type = "png";
    86162    }
    87     if ($result =~ m/^[^\n]*TIFF?/) {
     163    if ($result =~ m/^[^\n]*TIF?F/i) {
    88164    $type = "tiff";
    89165    }
    90    
    91     if ($type ne "unknown") {
    92     $doc_obj->add_metadata ($section, "ImageType", $type);
    93     }
    94     $doc_obj->add_metadata ($section, "ThumbType", "gif");
     166    if ($result =~ m/^[^\n]*BMP/i) {
     167    $type = "bmp";
     168    }
     169   
     170    #if there's a leading directory name, eat it...
     171    $file =~ s/^[^\/\\]*[\/\\]//;
     172   
     173    $doc_obj->add_metadata ($section, "ImageType", $type);
    95174    $doc_obj->add_metadata ($section, "Image", "$file");
    96     $doc_obj->add_metadata ($section, "Thumb", "$file.thumbnail.gif");
    97     if ($size != 0) {
    98     $doc_obj->add_metadata ($section, "ImageSize", $size);
    99     }
    100     print $result;
     175    $doc_obj->add_metadata ($section, "ImageSize", $size);
     176   
     177    #add the thumbnail as an associated file ...
     178    if (-e "$thumbnailfilename") {
     179    $doc_obj->associate_file("$thumbnailfilename","thumbnail.$thumbnailtype","image/$thumbnailtype",$section);
     180    $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
     181    $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
     182    } else {
     183    print STDERR "ImagePlug: couldn't find \"$thumbnailfilename\"\n";
     184    }
     185   
     186    #add the image as an associated file ...
     187    $doc_obj->associate_file($filename,$file,"image/$type",$section);
     188       
    101189    return $type;
    102190}
     
    121209
    122210    my $filename = &util::filename_cat($base_dir, $file);
     211    print STDERR "ImagePlug: called on \"$filename\"\n"
     212        if $self->{'verbosity'} > 1;
    123213    return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
    124214    if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
     
    132222   
    133223    #run convert to get the thumbnail and extract size and type info
    134     my $mimetype = run_convert($self, $filename, $file, $doc_obj);
    135 
    136     #add the image as an associated file ...
    137     if (-e "$filename.thumbnail.gif") {
    138     $doc_obj->associate_file("$filename.thumbnail.gif","$file.thumbnail.gif","image/gif",$section);
    139     } else {
    140     print "ImagePlug: couldn't find \"$filename.thumbnail.gif\"\n";
    141     }
    142 
    143     if (defined $mimetype) {
    144     $doc_obj->associate_file($filename,$file,"image/$mimetype",$section);
     224    my $thumbnailfile = run_convert($self, $filename, $file, $doc_obj);
     225   
     226    if (!defined $thumbnailfile)
     227    {
     228    print "ImagePlug: couldn't process \"$filename\"\n";
     229    return 0;
    145230    }
    146231
     
    150235    # include any metadata passed in from previous plugins
    151236    # note that this metadata is associated with the top level section
     237    my $section = $doc_obj->get_top_section();
    152238    $self->extra_metadata ($doc_obj, $section, $metadata);
    153239
     
    166252    $processor->process($doc_obj);
    167253
    168     if (-e "$filename.thumbnail.gif") {
    169       util::rm("$filename.thumbnail.gif");
     254    if (defined $self->{'tmp_filename'} &&
     255    -e $self->{'tmp_filename'}) {
     256      util::rm($self->{'tmp_filename'})
     257    }
     258    if (defined $self->{'tmp_filename2'} &&
     259    -e $self->{'tmp_filename2'}) {
     260      util::rm($self->{'tmp_filename2'})
    170261    }
    171262
Note: See TracChangeset for help on using the changeset viewer.