source: gsdl/trunk/perllib/plugins/ImageConverter.pm@ 19025

Last change on this file since 19025 was 19025, checked in by davidb, 15 years ago

Newer versions of 'convert' and 'identify' return an exist status of 1 if run with no arguments. This is seens as an error by Greenstone. Changed to run 'identify -version' which still returns an exit value of 0 if successfully run

  • Property svn:executable set to *
File size: 16.3 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 -version 2>&1`;
130 if ($? == -1 || $? == 256) { # Linux and Windows return different values for "program not found"
131 $image_conversion_available = 0;
132 $no_image_conversion_reason = "imagemagicknotinstalled";
133 }
134 }
135 $self->{'image_conversion_available'} = $image_conversion_available;
136 $self->{'no_image_conversion_reason'} = $no_image_conversion_reason;
137
138 if ($self->{'image_conversion_available'} == 0) {
139 my $outhandle = $self->{'outhandle'};
140 &gsprintf($outhandle, "ImageConverter: {ImageConverter.noconversionavailable} ({ImageConverter.".$self->{'no_image_conversion_reason'}."})\n");
141 }
142
143}
144
145
146# convert image to new type if converttotype is set
147# generate thumbnails if required
148# generate screenview if required
149# discover image metadata
150# filename_no_path must be in utf8 and url-encoded
151sub generate_images {
152 my $self = shift(@_);
153 my ($filename_full_path, $filename_no_path, $doc_obj, $section) = @_;
154
155 # check image magick status
156 return 0 if $self->{'image_conversion_available'} == 0;
157
158 # check the filenames
159 return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
160
161 if ($self->{'cache_generated_images'}) {
162 $self->init_cache_for_file($filename_full_path);
163 }
164
165 my $verbosity = $self->{'verbosity'};
166 my $outhandle = $self->{'outhandle'};
167
168 # check the size of the image against minimum size if specified
169 my $minimumsize = $self->{'minimumsize'};
170 if (defined $minimumsize && (-s $filename_full_path < $minimumsize)) {
171 print $outhandle "ImageConverter: \"$filename_full_path\" too small, skipping\n"
172 if ($verbosity > 1);
173 return 0; # or is there a better return value??
174 }
175
176 my $filehead = $filename_no_path;
177 $filehead =~ s/\.([^\.]*)$//; # filename with no extension
178 my $assocfilemeta = "[assocfilepath]";
179 if ($section ne $doc_obj->get_top_section()) {
180 $assocfilemeta = "[parent(Top):assocfilepath]";
181 }
182
183 # The images that will get generated may contain percent signs in their src filenames
184 # Encode those percent signs themselves so that urls to the imgs refer to them correctly
185 my $url_to_filehead = &unicode::filename_to_url($filehead);
186 my $url_to_filename_no_path = &unicode::filename_to_url($filename_no_path);
187
188 # Convert the image to a new type (if required).
189 my $converttotype = $self->{'converttotype'};
190 my $type = "unknown";
191
192 if ($converttotype ne "" && $filename_full_path !~ m/$converttotype$/) {
193 # $doc_obj->add_utf8_metadata($section, "Image", $utf8_filename_meta);
194
195 my ($result,$filename_full_path)
196 = $self->convert($filename_full_path, $converttotype, "", "CONVERTTYPE");
197
198 $type = $converttotype;
199 $filename_no_path = "$filehead.$type";
200 $url_to_filename_no_path = "$url_to_filehead.$type";
201 }
202
203 # add Image metadata
204 $doc_obj->add_utf8_metadata($section, "Image", $url_to_filename_no_path); # url to generated image
205
206 # here we overwrite the original with the potentially converted one
207 $doc_obj->set_utf8_metadata_element($section, "Source", &unicode::url_decode($filename_no_path)); # displayname of generated image
208 $doc_obj->set_utf8_metadata_element($section, "SourceFile", $url_to_filename_no_path); # displayname of generated image
209
210 # use identify to get info about the (possibly converted) image
211 my ($image_type, $image_width, $image_height, $image_size)
212 = &identify($filename_full_path, $outhandle, $verbosity);
213
214 if ($image_type ne " ") {
215 $type = $self->correct_mime_type($image_type);
216 }
217
218 #overwrite the ones added in BasePlugin
219 $doc_obj->set_metadata_element ($section, "FileFormat", $type);
220 $doc_obj->set_metadata_element ($section, "FileSize", $image_size);
221
222 $doc_obj->add_metadata ($section, "ImageType", $image_type);
223 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
224 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
225 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
226
227 $doc_obj->add_metadata ($section, "srclink", "<a href=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Image]\">");
228 $doc_obj->add_metadata ($section, "/srclink", "</a>");
229 $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Image]\" width=\"[ImageWidth]\" height=\"[ImageHeight]\">");
230
231 # Add the image as an associated file
232 $doc_obj->associate_file($filename_full_path, $filename_no_path, "image/$type", $section);
233
234 if ($self->{'create_thumbnail'} eq "true") {
235 $self->create_thumbnail($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead);
236 }
237 if ($self->{'create_screenview'} eq "true") {
238 $self->create_screenview($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height);
239 }
240}
241
242sub create_thumbnail {
243 my $self = shift(@_);
244 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead) = @_;
245 $url_to_filehead = $filehead unless defined $url_to_filehead;
246
247 my $thumbnailsize = $self->{'thumbnailsize'};
248 my $thumbnailtype = $self->correct_mime_type($self->{'thumbnailtype'});
249
250 # Generate the thumbnail with convert
251 my ($result,$thumbnailfile)
252 = $self->convert($original_file, $thumbnailtype, "-geometry $thumbnailsize" . "x$thumbnailsize", "THUMB");
253
254 # Add the thumbnail as an associated file ...
255 if (-e "$thumbnailfile") {
256 $doc_obj->associate_file("$thumbnailfile", $filehead."_thumb.$thumbnailtype",
257 "image/$thumbnailtype",$section); # name of generated image
258 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
259 $doc_obj->add_utf8_metadata ($section, "Thumb", $url_to_filehead."_thumb.$thumbnailtype"); # url to generated image
260
261 $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
262
263
264 # Extract Thumbnail metadata from convert output
265 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
266 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
267 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
268 }
269 } else {
270 my $outhandle = $self->{'outhandle'};
271 print $outhandle "Couldn't find thumbnail $thumbnailfile\n";
272
273 }
274}
275
276sub create_screenview {
277
278 my $self = shift(@_);
279 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height) = @_;
280 $url_to_filehead = $filehead unless defined $url_to_filehead;
281
282 my $screenviewsize = $self->{'screenviewsize'};
283 my $screenviewtype = $self->correct_mime_type($self->{'screenviewtype'});
284
285 # Scale the image, unless the original image is smaller than the screenview size and -noscaleup is set
286 my $scale_option = "-geometry $screenviewsize" . "x$screenviewsize";
287 if ($self->{'noscaleup'} && $image_width < $screenviewsize && $image_height < $screenviewsize)
288 {
289 $scale_option = "";
290 }
291
292 # make the screenview image
293 my ($result,$screenviewfilename)
294 = $self->convert($original_file, $screenviewtype, $scale_option, "SCREEN");
295
296 #add the screenview as an associated file ...
297 if (-e "$screenviewfilename") {
298 $doc_obj->associate_file("$screenviewfilename", $filehead."_screen.$screenviewtype", "image/$screenviewtype",$section); # name of generated image
299 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
300 $doc_obj->add_utf8_metadata ($section, "Screen", $url_to_filehead."_screen.$screenviewtype"); # url to generated image
301
302 $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
303
304 # get screenview dimensions, size and type
305 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
306 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
307 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
308 } elsif ($result =~ m/([0-9]+)x([0-9]+)/) {
309 #if the image hasn't changed size, the previous regex doesn't match
310 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
311 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
312 }
313 } else {
314 my $outhandle = $self->{'outhandle'};
315 print $outhandle "Couldn't find screenview file $screenviewfilename\n";
316
317 }
318
319}
320
321
322
323sub convert {
324 my $self = shift(@_);
325 my $source_file_path = shift(@_);
326 my $target_file_type = shift(@_);
327 my $convert_options = shift(@_) || "";
328 my $convert_id = shift(@_) || "";
329
330 my $outhandle = $self->{'outhandle'};
331 my $verbosity = $self->{'verbosity'};
332
333 my $source_file_no_path = &File::Basename::basename($source_file_path);
334
335 # Determine the full name and path of the output file
336 my $target_file_path;
337 if ($self->{'cache_generated_images'}) {
338 my $cached_image_dir = $self->{'cached_dir'};
339 my $image_root = $self->{'cached_file_root'};
340 $image_root .= "_$convert_id" if ($convert_id ne "");
341 my $image_file = "$image_root.$target_file_type";
342 $target_file_path = &util::filename_cat($cached_image_dir,$image_file);
343 }
344 else {
345 $target_file_path = &util::get_tmp_filename($target_file_type);
346 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
347
348 # Output filename used to be parsed from result line:
349 # my ($ofilename) = ($result =~ m/=>(.*\.$target_file_type)/);
350 # by the function that called 'convert'
351 # but this is no longer needed, as output filename is now
352 # explicitly passed back
353 }
354
355 # Generate and run the convert command
356 my $convert_command = "convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
357
358 my $print_info = { 'message_prefix' => $convert_id,
359 'message' => "Converting image $source_file_no_path to: $convert_id $target_file_type" };
360
361 my ($regenerated,$result,$had_error)
362 = $self->autorun_general_cmd($convert_command,$target_file_path,$print_info);
363
364 return ($result,$target_file_path);
365}
366
367
368# Discover the characteristics of an image file with the ImageMagick
369# "identify" command.
370
371sub identify {
372 my ($image, $outhandle, $verbosity) = @_;
373
374 # Use the ImageMagick "identify" command to get the file specs
375 my $command = "identify \"$image\" 2>&1";
376 print $outhandle "$command\n" if ($verbosity > 2);
377 my $result = '';
378 $result = `$command`;
379 print $outhandle "$result\n" if ($verbosity > 3);
380
381 # Read the type, width, and height
382 my $type = 'unknown';
383 my $width = 'unknown';
384 my $height = 'unknown';
385
386 my $image_safe = quotemeta $image;
387 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
388 $type = $1;
389 $width = $2;
390 $height = $3;
391 }
392
393 # Read the size
394 my $size = "unknown";
395 if ($result =~ m/^.* ([0-9]+)b/) {
396 $size = $1;
397 }
398 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
399 $size = 1024 * $1;
400 if (defined($2)) {
401 $size = $size + (1024 * $2);
402 # Truncate size (it isn't going to be very accurate anyway)
403 $size = int($size);
404 }
405 }
406 elsif ($result =~ m/^.* (([0-9]+)(\.([0-9]+))?e\+([0-9]+))(kb|b)?/) {
407 # Deals with file sizes on Linux of type "3.4e+02kb" where e+02 is 1*10^2.
408 # 3.4e+02 therefore evaluates to 3.4 x 1 x 10^2 = 340kb.
409 # Programming languages including Perl know how that 3.4e+02 is a number,
410 # so we don't need to do any calculations.
411 $size = $1*1; # turn the string into a number by multiplying it by 1
412 #if we did $size = $1; $size would be merely the string "3.4e+02"
413 $size = int($size); # truncate size
414 }
415 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
416 if ($verbosity > 2);
417
418 # Return the specs
419 return ($type, $width, $height, $size);
420}
421
422sub clean_up_temporary_files {
423 my $self = shift(@_);
424
425 foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}}) {
426 if (-e $tmp_file_path) {
427 &util::rm($tmp_file_path);
428 }
429 }
430
431}
432
433# image/jpg is not a valid mime-type, it ought to be image/jpeg.
434# Sometimes JPEG is passed in also, want to keep things lowercase just in case.
435sub correct_mime_type {
436 my $self = shift(@_);
437 my ($file_extension) = @_;
438
439 $file_extension = lc($file_extension);
440 $file_extension =~ s/jpg/jpeg/s;
441
442 return $file_extension;
443}
444
4451;
Note: See TracBrowser for help on using the repository browser.