Changeset 2230
- Timestamp:
- 2001-03-29T13:26:42+12:00 (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl/perllib/plugins/ImagePlug.pm
r2226 r2230 32 32 } 33 33 34 use strict;35 34 36 35 sub print_usage { … … 40 39 usage: plugin ImagePlug [options] 41 40 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 51 57 52 58 " … … 60 66 if (!parsargv::parse(\@_, 61 67 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'}, 62 72 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'}, 66 75 "allow_extra_options")) { 67 76 … … 81 90 } 82 91 92 93 # Create the thumbnail and screenview images, and discover the Image's 94 # size, width, and height using the convert utility. 95 83 96 sub run_convert { 84 97 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 87 100 my $doc_obj = shift (@_); 88 101 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 99 109 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; 102 112 } 103 113 104 114 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). 107 122 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'; 112 145 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"; 142 147 $self->{'tmp_filename2'} = $thumbnailfilename; 143 148 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 = ''; 147 154 $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 153 167 if ($result =~ m/([0-9]+)x([0-9]+)=>([0-9]+)x([0-9]+)/) { 154 168 $doc_obj->add_metadata ($section, "ImageWidth", $1); … … 161 175 if ($result =~ m/^[^\n]* ([0-9]+)b/) { 162 176 $size = $1; 163 } 164 if ($result =~ m/^[^\n]* ([0-9]+)kb/) { 177 } elsif ($result =~ m/^[^\n]* ([0-9]+)kb/) { 165 178 $size = 1024 * $1; 166 179 } … … 168 181 if ($result =~ m/^[^\n]*JPE?G/i) { 169 182 $type = "jpeg"; 170 } 171 if ($result =~ m/^[^\n]*GIF/i) { 183 } elsif ($result =~ m/^[^\n]*GIF/i) { 172 184 $type = "gif"; 173 } 174 if ($result =~ m/^[^\n]*PNG/i) { 185 } elsif ($result =~ m/^[^\n]*PNG/i) { 175 186 $type = "png"; 176 } 177 if ($result =~ m/^[^\n]*TIF?F/i) { 187 } elsif ($result =~ m/^[^\n]*TIF?F/i) { 178 188 $type = "tiff"; 179 } 180 if ($result =~ m/^[^\n]*BMP/i) { 189 } elsif ($result =~ m/^[^\n]*BMP/i) { 181 190 $type = "bmp"; 182 } 183 if ($result =~ m/^[^\n]*XBM?F/i) { 191 } elsif ($result =~ m/^[^\n]*XBM?F/i) { 184 192 $type = "xbm"; 185 193 } 186 194 187 #if there's a leading directory name, eat it...188 $file =~ s/^.*[\/\\]//;189 190 195 $doc_obj->add_metadata ($section, "ImageType", $type); 191 196 $doc_obj->add_metadata ($section, "Image", "$file"); 192 197 $doc_obj->add_metadata ($section, "ImageSize", $size); 193 198 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 ... 204 200 $doc_obj->associate_file($filename,$file,"image/$type",$section); 205 201 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 206 236 return $type; 237 238 207 239 } 208 240 … … 225 257 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_; 226 258 259 my $outhandle = $self->{'outhandle'}; 260 227 261 my $filename = &util::filename_cat($base_dir, $file); 228 262 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/; … … 230 264 return undef; 231 265 } 232 print STDERR"ImagePlug processing \"$filename\"\n"266 print $outhandle "ImagePlug processing \"$filename\"\n" 233 267 if $self->{'verbosity'} > 1; 234 268 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/^.*[\/\\]//; 236 271 237 272 # create a new document … … 269 304 $processor->process($doc_obj); 270 305 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. 271 309 if (defined $self->{'tmp_filename'} && 272 310 -e $self->{'tmp_filename'}) { 273 util::rm($self->{'tmp_filename'})311 &util::rm($self->{'tmp_filename'}) 274 312 } 275 313 if (defined $self->{'tmp_filename2'} && 276 314 -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'}) 278 320 } 279 321
Note:
See TracChangeset
for help on using the changeset viewer.