source: main/trunk/greenstone2/perllib/plugins/ImageConverter.pm@ 21866

Last change on this file since 21866 was 21866, checked in by kjdon, 11 years ago

added some code for if identify returns size in mb.

  • Property svn:executable set to *
File size: 17.9 KB
Line 
1###########################################################################
2#
3# ImageConverter - helper plugin that does image conversion using ImageMagick
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2008 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26package ImageConverter;
27
28use BaseMediaConverter;
29
30
31use strict;
32no strict 'refs'; # allow filehandles to be variables and viceversa
33
34use gsprintf 'gsprintf';
35
36BEGIN {
37 @ImageConverter::ISA = ('BaseMediaConverter');
38}
39
40my $arguments = [
41 { 'name' => "create_thumbnail",
42 'desc' => "{ImageConverter.create_thumbnail}",
43 'type' => "enum",
44 'list' => [{'name' => "true", 'desc' => "{common.true}"},
45 {'name' => "false", 'desc' => "{common.false}"}],
46 'deft' => "true",
47 'reqd' => "no" },
48 { 'name' => "thumbnailsize",
49 'desc' => "{ImageConverter.thumbnailsize}",
50 'type' => "int",
51 'deft' => "100",
52 'range' => "1,",
53 'reqd' => "no" },
54 { 'name' => "thumbnailtype",
55 'desc' => "{ImageConverter.thumbnailtype}",
56 'type' => "string",
57 'deft' => "gif",
58 'reqd' => "no" },
59 { 'name' => "noscaleup",
60 'desc' => "{ImageConverter.noscaleup}",
61 'type' => "flag",
62 'reqd' => "no" },
63 { 'name' => "create_screenview",
64 'desc' => "{ImageConverter.create_screenview}",
65 'type' => "enum",
66 'list' => [{'name' => "true", 'desc' => "{common.true}"},
67 {'name' => "false", 'desc' => "{common.false}"}],
68 'deft' => "true",
69 'reqd' => "no" },
70 { 'name' => "screenviewsize",
71 'desc' => "{ImageConverter.screenviewsize}",
72 'type' => "int",
73 'deft' => "500",
74 'range' => "1,",
75 'reqd' => "no" },
76 { 'name' => "screenviewtype",
77 'desc' => "{ImageConverter.screenviewtype}",
78 'type' => "string",
79 'deft' => "jpg",
80 'reqd' => "no" },
81 { 'name' => "converttotype",
82 'desc' => "{ImageConverter.converttotype}",
83 'type' => "string",
84 'deft' => "",
85 'reqd' => "no" },
86 { 'name' => "minimumsize",
87 'desc' => "{ImageConverter.minimumsize}",
88 'type' => "int",
89 'deft' => "100",
90 'range' => "1,",
91 'reqd' => "no" }
92 ];
93
94my $options = { 'name' => "ImageConverter",
95 'desc' => "{ImageConverter.desc}",
96 'abstract' => "yes",
97 'inherits' => "yes",
98 'args' => $arguments };
99
100sub new {
101 my ($class) = shift (@_);
102 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
103 push(@$pluginlist, $class);
104
105 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
106 push(@{$hashArgOptLists->{"OptList"}},$options);
107
108 my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
109
110
111 return bless $self, $class;
112
113}
114
115# needs to be called after BasePlugin init, so that outhandle is set up.
116sub init {
117 my $self = shift(@_);
118
119 $self->{'tmp_file_paths'} = ();
120
121 # Check that ImageMagick is installed and available on the path
122 my $image_conversion_available = 1;
123 my $no_image_conversion_reason = "";
124 # None of this works very well on Windows 95/98...
125 if ($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT()) {
126 $image_conversion_available = 0;
127 $no_image_conversion_reason = "win95notsupported";
128 } else {
129 my $result = `identify -help 2>&1`;
130 my $return_value = $?;
131 if ( ($ENV{'GSDLOS'} eq "windows" && $return_value == 256) || $return_value == -1) { # Linux and Windows return different values for "program not found"
132 $image_conversion_available = 0;
133 $no_image_conversion_reason = "imagemagicknotinstalled";
134 }
135 }
136 $self->{'image_conversion_available'} = $image_conversion_available;
137 $self->{'no_image_conversion_reason'} = $no_image_conversion_reason;
138
139 if ($self->{'image_conversion_available'} == 0) {
140 my $outhandle = $self->{'outhandle'};
141 &gsprintf($outhandle, "ImageConverter: {ImageConverter.noconversionavailable} ({ImageConverter.".$self->{'no_image_conversion_reason'}."})\n");
142 }
143
144}
145
146
147# convert image to new type if converttotype is set
148# generate thumbnails if required
149# generate screenview if required
150# discover image metadata
151# filename_no_path must be in utf8 and url-encoded
152sub generate_images {
153 my $self = shift(@_);
154 my ($filename_full_path, $filename_no_path, $doc_obj, $section) = @_;
155
156 # check image magick status
157 return 0 if $self->{'image_conversion_available'} == 0;
158
159 # check the filenames
160 return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
161
162 if ($self->{'enable_cache'}) {
163 $self->init_cache_for_file($filename_full_path);
164 }
165 if ($self->{'store_file_paths'}) {
166 $self->{'orig_file'} = "";
167 $self->{'thumb_file'} = "";
168 $self->{'screen_file'} = "";
169 }
170 my $verbosity = $self->{'verbosity'};
171 my $outhandle = $self->{'outhandle'};
172
173 # check the size of the image against minimum size if specified
174 my $minimumsize = $self->{'minimumsize'};
175 if (defined $minimumsize && (-s $filename_full_path < $minimumsize)) {
176 print $outhandle "ImageConverter: \"$filename_full_path\" too small, skipping\n"
177 if ($verbosity > 1);
178 return 0; # or is there a better return value??
179 }
180
181 my $filehead = $filename_no_path;
182 $filehead =~ s/\.([^\.]*)$//; # filename with no extension
183 my $assocfilemeta = "[assocfilepath]";
184 if ($section ne $doc_obj->get_top_section()) {
185 $assocfilemeta = "[parent(Top):assocfilepath]";
186 }
187
188 # The images that will get generated may contain percent signs in their src filenames
189 # Encode those percent signs themselves so that urls to the imgs refer to them correctly
190 my $url_to_filehead = &unicode::filename_to_url($filehead);
191 my $url_to_filename_no_path = &unicode::filename_to_url($filename_no_path);
192
193 # Convert the image to a new type (if required).
194 my $converttotype = $self->{'converttotype'};
195 my $type = "unknown";
196
197 if ($converttotype ne "" && $filename_full_path !~ m/$converttotype$/) {
198 # $doc_obj->add_utf8_metadata($section, "Image", $utf8_filename_meta);
199
200 my ($result, $converted_filename_full_path)
201 = $self->convert($filename_full_path, $converttotype, "", "CONVERTTYPE");
202
203 $type = $converttotype;
204 $filename_full_path = $converted_filename_full_path;
205 $filename_no_path = "$filehead.$type";
206 $url_to_filename_no_path = "$url_to_filehead.$type";
207 if ($self->{'store_file_paths'}) {
208 $self->{'orig_file'} = $converted_filename_full_path;
209 }
210 }
211
212 # add Image metadata
213 $doc_obj->add_utf8_metadata($section, "Image", $url_to_filename_no_path); # url to generated image
214
215 # here we overwrite the original with the potentially converted one
216 $doc_obj->set_utf8_metadata_element($section, "Source", &unicode::url_decode($filename_no_path)); # displayname of generated image
217 $doc_obj->set_utf8_metadata_element($section, "SourceFile", $url_to_filename_no_path); # displayname of generated image
218
219 # use identify to get info about the (possibly converted) image
220 my ($image_type, $image_width, $image_height, $image_size)
221 = &identify($filename_full_path, $outhandle, $verbosity);
222
223 if ($image_type ne " ") {
224 $type = $self->correct_mime_type($image_type);
225 }
226
227 #overwrite the ones added in BasePlugin
228 $doc_obj->set_metadata_element ($section, "FileFormat", $type);
229 $doc_obj->set_metadata_element ($section, "FileSize", $image_size);
230
231 $doc_obj->add_metadata ($section, "ImageType", $image_type);
232 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
233 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
234 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
235
236 if ((defined $self->{'MaxImageWidth'})
237 && ($image_width > $self->{'MaxImageWidth'})) {
238 $self->{'MaxImageWidth'} = $image_width;
239 }
240 if ((defined $self->{'MaxImageHeight'})
241 && ($image_height > $self->{'MaxImageHeight'})) {
242 $self->{'MaxImageHeight'} = $image_height;
243 }
244
245
246 #$doc_obj->add_metadata ($section, "srclink", "<a href=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Image]\">");
247 #$doc_obj->add_metadata ($section, "/srclink", "</a>");
248 $doc_obj->add_metadata ($section, "srclink_file", "[Image]");
249 $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Image]\" width=\"[ImageWidth]\" height=\"[ImageHeight]\">");
250
251 # Add the image as an associated file
252 $doc_obj->associate_file($filename_full_path, $filename_no_path, "image/$type", $section);
253
254 if ($self->{'create_thumbnail'} eq "true") {
255 $self->create_thumbnail($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead);
256 }
257 if ($self->{'create_screenview'} eq "true") {
258 $self->create_screenview($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height);
259 }
260
261 return 1;
262}
263
264sub create_thumbnail {
265 my $self = shift(@_);
266 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead) = @_;
267 $url_to_filehead = $filehead unless defined $url_to_filehead;
268
269 my $thumbnailsize = $self->{'thumbnailsize'};
270 my $thumbnailtype = $self->correct_mime_type($self->{'thumbnailtype'});
271
272 # Generate the thumbnail with convert
273 my ($result,$thumbnailfile)
274 = $self->convert($original_file, $thumbnailtype, "-geometry $thumbnailsize" . "x$thumbnailsize", "THUMB");
275
276 # Add the thumbnail as an associated file ...
277 if (-e "$thumbnailfile") {
278 $doc_obj->associate_file("$thumbnailfile", $filehead."_thumb.$thumbnailtype",
279 "image/$thumbnailtype",$section); # name of generated image
280 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
281 $doc_obj->add_utf8_metadata ($section, "Thumb", $url_to_filehead."_thumb.$thumbnailtype"); # url to generated image
282
283 $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
284
285
286 # Extract Thumbnail metadata from convert output
287 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
288 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
289 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
290 }
291 if ($self->{'store_file_paths'}) {
292 $self->{'thumb_file'} = $thumbnailfile;
293 }
294
295 } else {
296 my $outhandle = $self->{'outhandle'};
297 print $outhandle "Couldn't find thumbnail $thumbnailfile\n";
298
299 }
300}
301
302sub create_screenview {
303
304 my $self = shift(@_);
305 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height) = @_;
306 $url_to_filehead = $filehead unless defined $url_to_filehead;
307
308 my $screenviewsize = $self->{'screenviewsize'};
309 my $screenviewtype = $self->correct_mime_type($self->{'screenviewtype'});
310
311 # Scale the image, unless the original image is smaller than the screenview size and -noscaleup is set
312 my $scale_option = "-geometry $screenviewsize" . "x$screenviewsize";
313 if ($self->{'noscaleup'} && $image_width < $screenviewsize && $image_height < $screenviewsize)
314 {
315 $scale_option = "";
316 }
317
318 # make the screenview image
319 my ($result,$screenviewfilename)
320 = $self->convert($original_file, $screenviewtype, $scale_option, "SCREEN");
321
322 #add the screenview as an associated file ...
323 if (-e "$screenviewfilename") {
324 $doc_obj->associate_file("$screenviewfilename", $filehead."_screen.$screenviewtype", "image/$screenviewtype",$section); # name of generated image
325 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
326 $doc_obj->add_utf8_metadata ($section, "Screen", $url_to_filehead."_screen.$screenviewtype"); # url to generated image
327
328 $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
329
330 # get screenview dimensions, size and type
331 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
332 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
333 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
334 } elsif ($result =~ m/([0-9]+)x([0-9]+)/) {
335 #if the image hasn't changed size, the previous regex doesn't match
336 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
337 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
338 }
339
340 if ($self->{'store_file_paths'}) {
341 $self->{'screen_file'} = $screenviewfilename;
342 }
343
344 } else {
345 my $outhandle = $self->{'outhandle'};
346 print $outhandle "Couldn't find screenview file $screenviewfilename\n";
347
348 }
349
350}
351
352
353
354sub convert {
355 my $self = shift(@_);
356 my $source_file_path = shift(@_);
357 my $target_file_type = shift(@_);
358 my $convert_options = shift(@_) || "";
359 my $convert_id = shift(@_) || "";
360 my $cache_mode = shift(@_) || "";
361
362 my $outhandle = $self->{'outhandle'};
363 my $verbosity = $self->{'verbosity'};
364
365 my $source_file_no_path = &File::Basename::basename($source_file_path);
366
367 # Determine the full name and path of the output file
368 my $target_file_path;
369 if ($self->{'enable_cache'}) {
370 my $cached_image_dir = $self->{'cached_dir'};
371 my $image_root = $self->{'cached_file_root'};
372 $image_root .= "_$convert_id" if ($convert_id ne "");
373 my $image_file = "$image_root.$target_file_type";
374 $target_file_path = &util::filename_cat($cached_image_dir,$image_file);
375 }
376 else {
377 $target_file_path = &util::get_tmp_filename($target_file_type);
378 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
379
380 # Output filename used to be parsed from result line:
381 # my ($ofilename) = ($result =~ m/=>(.*\.$target_file_type)/);
382 # by the function that called 'convert'
383 # but this is no longer needed, as output filename is now
384 # explicitly passed back
385 }
386
387 # Generate and run the convert command
388 my $convert_command = "convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
389
390 my $print_info = { 'message_prefix' => $convert_id,
391 'message' => "Converting image $source_file_no_path to: $convert_id $target_file_type" };
392 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
393
394 my ($regenerated,$result,$had_error)
395 = $self->autorun_general_cmd($convert_command,$target_file_path,$print_info);
396
397 return ($result,$target_file_path);
398}
399
400sub convert_without_result {
401 my $self = shift(@_);
402
403 my $source_file_path = shift(@_);
404 my $target_file_type = shift(@_);
405 my $convert_options = shift(@_) || "";
406 my $convert_id = shift(@_) || "";
407
408 return $self->convert($source_file_path,$target_file_type,
409 $convert_options,$convert_id,"without_result");
410}
411
412
413# Discover the characteristics of an image file with the ImageMagick
414# "identify" command.
415
416sub identify {
417 my ($image, $outhandle, $verbosity) = @_;
418
419 # Use the ImageMagick "identify" command to get the file specs
420 my $command = "identify \"$image\" 2>&1";
421 print $outhandle "$command\n" if ($verbosity > 2);
422 my $result = '';
423 $result = `$command`;
424 print $outhandle "$result\n" if ($verbosity > 3);
425
426 # Read the type, width, and height
427 my $type = 'unknown';
428 my $width = 'unknown';
429 my $height = 'unknown';
430
431 my $image_safe = quotemeta $image;
432 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
433 $type = $1;
434 $width = $2;
435 $height = $3;
436 }
437
438 # Read the size
439 my $size = "unknown";
440 if ($result =~ m/^.* ([0-9]+)b/) {
441 $size = $1;
442 }
443 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
444 $size = 1024 * $1;
445 if (defined($2)) {
446 $size = $size + (1024 * $2);
447 # Truncate size (it isn't going to be very accurate anyway)
448 $size = int($size);
449 }
450 }
451 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?mb?/) {
452 $size = 1024 * 1024 * $1;
453 if (defined($2)) {
454 $size = $size + (1024 * 1024 * $2);
455 # Truncate size (it isn't going to be very accurate anyway)
456 $size = int($size);
457 }
458 }
459 elsif ($result =~ m/^.* (([0-9]+)(\.([0-9]+))?e\+([0-9]+))(kb|b)?/) {
460 # Deals with file sizes on Linux of type "3.4e+02kb" where e+02 is 1*10^2.
461 # 3.4e+02 therefore evaluates to 3.4 x 1 x 10^2 = 340kb.
462 # Programming languages including Perl know how that 3.4e+02 is a number,
463 # so we don't need to do any calculations.
464 $size = $1*1; # turn the string into a number by multiplying it by 1
465 #if we did $size = $1; $size would be merely the string "3.4e+02"
466 $size = int($size); # truncate size
467 }
468 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
469 if ($verbosity > 2);
470
471 # Return the specs
472 return ($type, $width, $height, $size);
473}
474
475sub clean_up_temporary_files {
476 my $self = shift(@_);
477
478 foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}}) {
479 if (-e $tmp_file_path) {
480 &util::rm($tmp_file_path);
481 }
482 }
483
484}
485
486# image/jpg is not a valid mime-type, it ought to be image/jpeg.
487# Sometimes JPEG is passed in also, want to keep things lowercase just in case.
488sub correct_mime_type {
489 my $self = shift(@_);
490 my ($file_extension) = @_;
491
492 $file_extension = lc($file_extension);
493 $file_extension =~ s/jpg/jpeg/s;
494
495 return $file_extension;
496}
497
4981;
Note: See TracBrowser for help on using the repository browser.