source: trunk/gsdl/perllib/plugins/ImagePlug.pm@ 9144

Last change on this file since 9144 was 9067, checked in by kjdon, 19 years ago

moved smart blocking stuff in htmlplug metadata_read into basplug metadata read. htmlplug now just needs to implement store_block_files. basplug smart block will block a cover image if -cover_images is on. Converttoplug store_block_files needs to call the basplug default one (do nothing) cos it doesn't want to use the html specific one. Image plug needs to implement store_cover_image to do nothing cos it doesn'tmake sense

  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 KB
Line 
1###########################################################################
2#
3# ImagePlug.pm -- simple text plugin
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package ImagePlug;
27
28use BasPlug;
29
30sub BEGIN {
31 @ISA = ('BasPlug');
32}
33
34my $arguments =
35 [ { 'name' => "process_exp",
36 'desc' => "{BasPlug.process_exp}",
37 'type' => "regexp",
38 'deft' => &get_default_process_exp(),
39 'reqd' => "no" },
40 { 'name' => "noscaleup",
41 'desc' => "{ImagePlug.noscaleup}",
42 'type' => "flag",
43 'reqd' => "no" },
44 { 'name' => "thumbnailsize",
45 'desc' => "{ImagePlug.thumbnailsize}",
46 'type' => "int",
47 'deft' => "100",
48 'reqd' => "no" },
49 { 'name' => "thumbnailtype",
50 'desc' => "{ImagePlug.thumbnailtype}",
51 'type' => "string",
52 'deft' => "gif",
53 'reqd' => "no" },
54 { 'name' => "screenviewsize",
55 'desc' => "{ImagePlug.screenviewsize}",
56 'type' => "int",
57 'deft' => "0",
58 'reqd' => "no" },
59 { 'name' => "screenviewtype",
60 'desc' => "{ImagePlug.screenviewtype}",
61 'type' => "string",
62 'deft' => "jpg",
63 'reqd' => "no" },
64 { 'name' => "converttotype",
65 'desc' => "{ImagePlug.converttotype}",
66 'type' => "string",
67 'deft' => "",
68 'reqd' => "no" },
69 { 'name' => "minimumsize",
70 'desc' => "{ImagePlug.minimumsize}",
71 'type' => "int",
72 'deft' => "100",
73 'reqd' => "no" } ];
74
75my $options = { 'name' => "ImagePlug",
76 'desc' => "{ImagePlug.desc}",
77 'abstract' => "no",
78 'inherits' => "yes",
79 'args' => $arguments };
80
81
82
83sub new {
84 my ($class) = @_;
85 my $plugin_name = shift (@_);
86 my $self = new BasPlug ("ImagePlug", @_);
87 $self->{'plugin_type'} = "ImagePlug";
88 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
89 my $option_list = $self->{'option_list'};
90 push( @{$option_list}, $options );
91
92 if (!parsargv::parse(\@_,
93 q^noscaleup^, \$self->{'noscaleup'},
94 q^converttotype/.*/^, \$self->{'converttotype'},
95 q^minimumsize/[0-9]*/100^, \$self->{'minimumsize'},
96
97 q^thumbnailsize/[0-9]*/100^, \$self->{'thumbnailsize'},
98 q^thumbnailtype/.*/gif^, \$self->{'thumbnailtype'},
99 q^screenviewsize/[0-9]*/0^, \$self->{'screenviewsize'},
100 q^screenviewtype/.*/jpg^, \$self->{'screenviewtype'},
101 "allow_extra_options")) {
102
103 print STDERR "\nImagePlug uses an incorrect option.\n";
104 print STDERR "Check your collect.cfg configuration file.\n";
105 $self->print_txt_usage(""); # Use default resource bundle
106 die "\n";
107 }
108
109 # Check that ImageMagick is installed and available on the path
110 my $result = `identify 2>&1`;
111 if ($? == -1 || $? == 256) { # Linux and Windows return different values for "program not found"
112 $self->{'imagemagick_not_installed'} = 1;
113 }
114
115 return bless $self, $class;
116}
117
118sub get_default_process_exp {
119 my $self = shift (@_);
120
121 return q^(?i)(\.jpe?g|\.gif|\.png|\.bmp|\.xbm|\.tif?f)$^;
122}
123
124# this makes no sense for images
125sub block_cover_image
126{
127 my $self =shift (@_);
128 my ($filename) = @_;
129
130 return;
131}
132# Create the thumbnail and screenview images, and discover the Image's
133# size, width, and height using the convert utility.
134
135sub run_convert {
136 my $self = shift (@_);
137 my $filename = shift (@_); # filename with full path
138 my $file = shift (@_); # filename without path
139 my $doc_obj = shift (@_);
140 my $section = $doc_obj->get_top_section();
141
142 my $verbosity = $self->{'verbosity'};
143 my $outhandle = $self->{'outhandle'};
144
145 # None of this is going to work very well without ImageMagick...
146 if ($self->{'imagemagick_not_installed'}) {
147 print $outhandle "ImagePlug: ImageMagick not installed\n" if ($verbosity > 1);
148 return undef;
149 }
150
151 # check the filename is okay
152 return 0 if ($file eq "" || $filename eq "");
153
154# Code now extended to quote filenames in 'convert' commnads
155# Allows spaces in filenames, but note needs spaces to be escaped in URL as well
156# if ($filename =~ m/ /) {
157# print $outhandle "ImagePlug: \"$filename\" contains a space. choking.\n";
158# return undef;
159# }
160
161 my $minimumsize = $self->{'minimumsize'};
162 if (defined $minimumsize && (-s $filename < $minimumsize)) {
163 print $outhandle "ImagePlug: \"$filename\" too small, skipping\n"
164 if ($verbosity > 1);
165 }
166
167
168 # Convert the image to a new type (if required).
169 my $converttotype = $self->{'converttotype'};
170 my $originalfilename = ""; # only set if we do a conversion
171 my $type = "unknown";
172
173
174 if ($converttotype ne "" && $filename =~ m/$converttotype$/) {
175
176 $originalfilename = $filename;
177 $filename = &util::get_tmp_filename() . ".$converttotype";
178 $self->{'tmp_filename'} = $filename;
179
180 my $command = "convert -interlace plane -verbose \"$originalfilename\" \"$filename\"";
181 print $outhandle "$command\n" if ($verbosity > 2);
182 my $result = '';
183 $result = `$command`;
184 print $outhandle "RESULT = $result\n" if ($verbosity > 2);
185
186 $type = $converttotype;
187 }
188
189
190 # Add the image metadata
191 my $url = $file;
192 $url =~ s/ /%20/g;
193
194 $doc_obj->add_metadata ($section, "Image", $url);
195
196 # Also want to set filename as 'Source' metadata to be
197 # consistent with other plugins
198 $doc_obj->add_metadata ($section, "Source", $url);
199
200 my ($image_type, $image_width, $image_height, $image_size)
201 = &identify($filename, $outhandle, $verbosity);
202
203 if ($image_type ne " ") {
204 $type = $image_type;
205 }
206
207 $doc_obj->add_metadata ($section, "FileFormat", $type);
208 $doc_obj->add_metadata ($section, "FileSize", $image_size);
209
210 $doc_obj->add_metadata ($section, "ImageType", $image_type);
211 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
212 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
213 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
214
215 $doc_obj->add_metadata ($section, "srclink",
216 "<a href=\"_httpcollection_/index/assoc/[assocfilepath]/[Image]\">");
217 $doc_obj->add_metadata ($section, "/srclink", "</a>");
218
219 $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpcollection_/index/assoc/[assocfilepath]/[Image]\" width=100>");
220
221
222 # Add the image as an associated file
223 $doc_obj->associate_file($filename,$file,"image/$type",$section);
224
225
226 # Make the thumbnail image
227 my $thumbnailsize = $self->{'thumbnailsize'} || 100;
228 my $thumbnailtype = $self->{'thumbnailtype'} || 'gif';
229
230 my $thumbnailfile = &util::get_tmp_filename() . ".$thumbnailtype";
231 $self->{'tmp_filename2'} = $thumbnailfile;
232
233 # Generate the thumbnail with convert
234 my $command = "convert -interlace plane -verbose -geometry $thumbnailsize"
235 . "x$thumbnailsize \"$filename\" \"$thumbnailfile\"";
236 print $outhandle "THUMBNAIL: $command\n" if ($verbosity > 2);
237 my $result = '';
238 $result = `$command 2>&1` ;
239 print $outhandle "THUMB RESULT: $result\n" if ($verbosity > 2);
240
241 # Add the thumbnail as an associated file ...
242 if (-e "$thumbnailfile") {
243 $doc_obj->associate_file("$thumbnailfile", "thumbnail.$thumbnailtype",
244 "image/$thumbnailtype",$section);
245 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
246 $doc_obj->add_metadata ($section, "Thumb", "thumbnail.$thumbnailtype");
247
248 $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpcollection_/index/assoc/[assocfilepath]/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
249 }
250
251 # Extract Thumnail metadata from convert output
252 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
253 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
254 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
255 }
256
257 # Make a screen-sized version of the picture if requested
258 if ($self->{'screenviewsize'}) {
259
260 # To do: if the actual image smaller than the screenview size,
261 # we should use the original !
262
263 my $screenviewsize = $self->{'screenviewsize'};
264 my $screenviewtype = $self->{'screenviewtype'} || 'jpeg';
265 my $screenviewfilename = &util::get_tmp_filename() . ".$screenviewtype";
266 $self->{'tmp_filename3'} = $screenviewfilename;
267
268 # make the screenview image
269 my $command = "convert -interlace plane -verbose -geometry $screenviewsize"
270 . "x$screenviewsize \"$filename\" \"$screenviewfilename\"";
271 print $outhandle "$command\n" if ($verbosity > 2);
272 my $result = "";
273 $result = `$command 2>&1` ;
274 print $outhandle "$result\n" if ($verbosity > 3);
275
276 # get screenview dimensions, size and type
277 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
278 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
279 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
280 }
281 else {
282 $doc_obj->add_metadata ($section, "ScreenWidth", $image_width);
283 $doc_obj->add_metadata ($section, "ScreenHeight", $image_height);
284 }
285
286 #add the screenview as an associated file ...
287 if (-e "$screenviewfilename") {
288 $doc_obj->associate_file("$screenviewfilename", "screenview.$screenviewtype",
289 "image/$screenviewtype",$section);
290 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
291 $doc_obj->add_metadata ($section, "Screen", "screenview.$screenviewtype");
292
293 $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpcollection_/index/assoc/[assocfilepath]/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
294 } else {
295 print $outhandle "ImagePlug: couldn't find \"$screenviewfilename\"\n";
296 }
297 }
298
299 return $type;
300
301
302}
303
304
305
306# Discover the characteristics of an image file with the ImageMagick
307# "identify" command.
308
309sub identify {
310 my ($image, $outhandle, $verbosity) = @_;
311
312 # Use the ImageMagick "identify" command to get the file specs
313 my $command = "identify \"$image\" 2>&1";
314 print $outhandle "$command\n" if ($verbosity > 2);
315 my $result = '';
316 $result = `$command`;
317 print $outhandle "$result\n" if ($verbosity > 3);
318
319 # Read the type, width, and height
320 my $type = 'unknown';
321 my $width = 'unknown';
322 my $height = 'unknown';
323
324 my $image_safe = quotemeta $image;
325 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
326 $type = $1;
327 $width = $2;
328 $height = $3;
329 }
330
331 # Read the size
332 my $size = "unknown";
333 if ($result =~ m/^.* ([0-9]+)b/) {
334 $size = $1;
335 }
336 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
337 $size = 1024 * $1;
338 if (defined($2)) {
339 $size = $size + (1024 * $2);
340 # Truncate size (it isn't going to be very accurate anyway)
341 $size = int($size);
342 }
343 }
344
345 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
346 if ($verbosity > 2);
347
348 # Return the specs
349 return ($type, $width, $height, $size);
350}
351
352
353# The ImagePlug read() function. This function does all the right things
354# to make general options work for a given plugin. It calls the process()
355# function which does all the work specific to a plugin (like the old
356# read functions used to do). Most plugins should define their own
357# process() function and let this read() function keep control.
358#
359# ImagePlug overrides read() because there is no need to read the actual
360# text of the file in, because the contents of the file is not text...
361#
362# Return number of files processed, undef if can't process
363# Note that $base_dir might be "" and that $file might
364# include directories
365
366sub read {
367 my $self = shift (@_);
368 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $gli) = @_;
369
370 my $outhandle = $self->{'outhandle'};
371
372 my $filename = &util::filename_cat($base_dir, $file);
373 return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
374 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
375 return undef;
376 }
377
378 print STDERR "<Processing n='$file' p='ImagePlug'>\n" if ($gli);
379 print $outhandle "ImagePlug processing $filename\n"
380 if $self->{'verbosity'} > 1;
381
382 #if there's a leading directory name, eat it...
383 $file =~ s/^.*[\/\\]//;
384
385 # create a new document
386 my $doc_obj = new doc ($filename, "indexed_doc");
387 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
388 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
389
390 #run convert to get the thumbnail and extract size and type info
391 my $result = run_convert($self, $filename, $file, $doc_obj);
392
393 if (!defined $result)
394 {
395 print "ImagePlug: couldn't process \"$filename\"\n";
396 return -1; # error during processing
397 }
398
399 #create an empty text string so we don't break downstream plugins
400 my $text = &gsprintf::lookup_string("{BasPlug.dummy_text}");
401 # include any metadata passed in from previous plugins
402 # note that this metadata is associated with the top level section
403 my $section = $doc_obj->get_top_section();
404 $self->extra_metadata ($doc_obj, $section, $metadata);
405
406 # do plugin specific processing of doc_obj
407 return -1 unless defined ($self->process (\$text, $pluginfo, $base_dir,
408 $file, $metadata, $doc_obj));
409
410 # do any automatic metadata extraction
411 $self->auto_extract_metadata ($doc_obj);
412
413 # if we haven't found any Title so far, assign one
414 # this was shifted to here from inside read()
415 $self->title_fallback($doc_obj,$section,$file);
416 # add an OID
417 $doc_obj->set_OID();
418 $doc_obj->add_text($section, $text);
419
420 # process the document
421 $processor->process($doc_obj);
422
423 # clean up temporary files - we do this here instead of in
424 # run_convert becuase associated files aren't actually copied
425 # until after process has been run.
426 if (defined $self->{'tmp_filename'} &&
427 -e $self->{'tmp_filename'}) {
428 &util::rm($self->{'tmp_filename'})
429 }
430 if (defined $self->{'tmp_filename2'} &&
431 -e $self->{'tmp_filename2'}) {
432 &util::rm($self->{'tmp_filename2'})
433 }
434 if (defined $self->{'tmp_filename3'} &&
435 -e $self->{'tmp_filename3'}) {
436 &util::rm($self->{'tmp_filename3'})
437 }
438
439 $self->{'num_processed'}++;
440
441 return 1;
442}
443
444# do plugin specific processing of doc_obj
445sub process {
446 my $self = shift (@_);
447 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
448 my $outhandle = $self->{'outhandle'};
449
450 return 1;
451}
452
4531;
454
455
456
457
458
459
460
461
462
463
464
Note: See TracBrowser for help on using the repository browser.