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

Last change on this file since 16821 was 16771, checked in by ak19, 16 years ago

Changes to make it compatible with multilingual filenames. Uses URL encoding for this.

  • Property svn:executable set to *
File size: 14.8 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 PrintInfo;
29
30use strict;
31no strict 'refs'; # allow filehandles to be variables and viceversa
32
33use gsprintf 'gsprintf';
34
35BEGIN {
36 @ImageConverter::ISA = ('PrintInfo');
37}
38
39my $arguments = [
40 { 'name' => "create_thumbnail",
41 'desc' => "{ImageConverter.create_thumbnail}",
42 'type' => "enum",
43 'list' => [{'name' => "true", 'desc' => "{common.true}"},
44 {'name' => "false", 'desc' => "{common.false}"}],
45 'deft' => "true",
46 'reqd' => "no" },
47 { 'name' => "thumbnailsize",
48 'desc' => "{ImageConverter.thumbnailsize}",
49 'type' => "int",
50 'deft' => "100",
51 'range' => "1,",
52 'reqd' => "no" },
53 { 'name' => "thumbnailtype",
54 'desc' => "{ImageConverter.thumbnailtype}",
55 'type' => "string",
56 'deft' => "gif",
57 'reqd' => "no" },
58 { 'name' => "noscaleup",
59 'desc' => "{ImageConverter.noscaleup}",
60 'type' => "flag",
61 'reqd' => "no" },
62 { 'name' => "create_screenview",
63 'desc' => "{ImageConverter.create_screenview}",
64 'type' => "enum",
65 'list' => [{'name' => "true", 'desc' => "{common.true}"},
66 {'name' => "false", 'desc' => "{common.false}"}],
67 'deft' => "true",
68 'reqd' => "no" },
69 { 'name' => "screenviewsize",
70 'desc' => "{ImageConverter.screenviewsize}",
71 'type' => "int",
72 'deft' => "500",
73 'range' => "1,",
74 'reqd' => "no" },
75 { 'name' => "screenviewtype",
76 'desc' => "{ImageConverter.screenviewtype}",
77 'type' => "string",
78 'deft' => "jpg",
79 'reqd' => "no" },
80 { 'name' => "converttotype",
81 'desc' => "{ImageConverter.converttotype}",
82 'type' => "string",
83 'deft' => "",
84 'reqd' => "no" },
85 { 'name' => "minimumsize",
86 'desc' => "{ImageConverter.minimumsize}",
87 'type' => "int",
88 'deft' => "100",
89 'range' => "1,",
90 'reqd' => "no" },
91 { 'name' => "cache_generated_images",
92 'desc' => "{ImageConverter.cache_generated_images}",
93 'type' => "flag",
94 'reqd' => "no",
95 'hiddengli' => "yes" # not yet implemented
96 }
97 ];
98
99my $options = { 'name' => "ImageConverter",
100 'desc' => "{ImageConverter.desc}",
101 'abstract' => "yes",
102 'inherits' => "yes",
103 'args' => $arguments };
104
105sub new {
106 my ($class) = shift (@_);
107 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
108 push(@$pluginlist, $class);
109
110 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
111 push(@{$hashArgOptLists->{"OptList"}},$options);
112
113 my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists, 1);
114
115 return bless $self, $class;
116
117}
118
119# needs to be called after BasePlugin init, so that outhandle is set up.
120sub init {
121 my $self = shift(@_);
122
123 $self->{'tmp_file_paths'} = ();
124
125 # Check that ImageMagick is installed and available on the path
126 my $image_conversion_available = 1;
127 my $no_image_conversion_reason = "";
128 # None of this works very well on Windows 95/98...
129 if ($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT()) {
130 $image_conversion_available = 0;
131 $no_image_conversion_reason = "win95notsupported";
132 } else {
133 my $result = `identify 2>&1`;
134 if ($? == -1 || $? == 256) { # Linux and Windows return different values for "program not found"
135 $image_conversion_available = 0;
136 $no_image_conversion_reason = "imagemagicknotinstalled";
137 }
138 }
139 $self->{'image_conversion_available'} = $image_conversion_available;
140 $self->{'no_image_conversion_reason'} = $no_image_conversion_reason;
141
142 if ($self->{'image_conversion_available'} == 0) {
143 my $outhandle = $self->{'outhandle'};
144 &gsprintf($outhandle, "ImageConverter: {ImageConverter.noconversionavailable} ({ImageConverter.".$self->{'no_image_conversion_reason'}."})\n");
145 }
146
147}
148
149
150# convert image to new type if converttotype is set
151# generate thumbnails if required
152# generate screenview if required
153# discover image metadata
154# filename_no_path must be in utf8
155sub generate_images {
156 my $self = shift(@_);
157
158 my ($filename_full_path, $filename_no_path, $doc_obj, $section) = @_;
159
160 # check image magick status
161 return 0 if $self->{'image_conversion_available'} == 0;
162 # check the filenames
163 return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
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 # 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 = $filehead;
186 $url_to_filehead =~ s/%/%25/g;
187 my $url_to_filename_no_path = $filename_no_path;
188 $url_to_filename_no_path =~ s/%/%25/g;
189
190 # Convert the image to a new type (if required).
191 my $converttotype = $self->{'converttotype'};
192 my $type = "unknown";
193
194 if ($converttotype ne "" && $filename_full_path !~ m/$converttotype$/) {
195 # $doc_obj->add_utf8_metadata($section, "Image", $utf8_filename_meta);
196
197 my $result = $self->convert($filename_full_path, $converttotype, "", "");
198 ($filename_full_path) = ($result =~ /=>(.*\.$converttotype)/);
199
200 $type = $converttotype;
201 $filename_no_path = "$filehead.$type";
202 $url_to_filename_no_path = "$url_to_filehead.$type";
203 }
204
205 # add Image metadata
206 $doc_obj->add_utf8_metadata($section, "Image", $url_to_filename_no_path); # url to generated image
207
208 # here we overwrite the original with the potentially converted one
209 $doc_obj->set_utf8_metadata_element($section, "Source", $filename_no_path); # name of generated image
210
211 # use identify to get info about the (possibly converted) image
212 my ($image_type, $image_width, $image_height, $image_size)
213 = &identify($filename_full_path, $outhandle, $verbosity);
214
215 if ($image_type ne " ") {
216 $type = $image_type;
217 }
218
219 #overwrite the ones added in BasePlugin
220 $doc_obj->set_metadata_element ($section, "FileFormat", $type);
221 $doc_obj->set_metadata_element ($section, "FileSize", $image_size);
222
223 $doc_obj->add_metadata ($section, "ImageType", $image_type);
224 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
225 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
226 $doc_obj->add_metadata ($section, "ImageSize", $image_size);
227
228 $doc_obj->add_metadata ($section, "srclink", "<a href=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Image]\">");
229 $doc_obj->add_metadata ($section, "/srclink", "</a>");
230 $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Image]\" width=\"100\">");
231
232 # Add the image as an associated file
233 $doc_obj->associate_file($filename_full_path, $filename_no_path, "image/$type", $section);
234
235 if ($self->{'create_thumbnail'} eq "true") {
236 $self->create_thumbnail($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead);
237 }
238 if ($self->{'create_screenview'} eq "true") {
239 $self->create_screenview($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead);
240 }
241}
242
243sub create_thumbnail {
244 my $self = shift(@_);
245 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead) = @_;
246 $url_to_filehead = $filehead unless defined $url_to_filehead;
247
248 my $thumbnailsize = $self->{'thumbnailsize'};
249 my $thumbnailtype = $self->{'thumbnailtype'};
250
251 # Generate the thumbnail with convert
252 my $result = $self->convert($original_file, $thumbnailtype, "-geometry $thumbnailsize" . "x$thumbnailsize", "THUMB");
253 my ($thumbnailfile) = ($result =~ /=>(.*\.$thumbnailtype)/);
254
255 # Add the thumbnail as an associated file ...
256 if (-e "$thumbnailfile") {
257 $doc_obj->associate_file("$thumbnailfile", $filehead."_thumb.$thumbnailtype",
258 "image/$thumbnailtype",$section); # name of generated image
259 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
260 $doc_obj->add_utf8_metadata ($section, "Thumb", $url_to_filehead."_thumb.$thumbnailtype"); # url to generated image
261
262 $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
263
264
265 # Extract Thumbnail metadata from convert output
266 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
267 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
268 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
269 }
270 } else {
271 my $outhandle = $self->{'outhandle'};
272 print $outhandle "Couldn't find thumbnail $thumbnailfile\n";
273
274 }
275}
276
277sub create_screenview {
278
279 my $self = shift(@_);
280 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead) = @_;
281 $url_to_filehead = $filehead unless defined $url_to_filehead;
282
283 # To do: if the actual image smaller than the screenview size,
284 # we should use the original !
285
286 my $screenviewsize = $self->{'screenviewsize'};
287 my $screenviewtype = $self->{'screenviewtype'};
288
289 # make the screenview image
290 my $result = $self->convert($original_file, $screenviewtype, "-geometry $screenviewsize" . "x$screenviewsize", "SCREEN");
291 my ($screenviewfilename) = ($result =~ /=>(.*\.$screenviewtype)/);
292
293
294 #add the screenview as an associated file ...
295 if (-e "$screenviewfilename") {
296 $doc_obj->associate_file("$screenviewfilename", $filehead."_screen.$screenviewtype", "image/$screenviewtype",$section); # name of generated image
297 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
298 $doc_obj->add_utf8_metadata ($section, "Screen", $url_to_filehead."_screen.$screenviewtype"); # url to generated image
299
300 $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
301
302 # get screenview dimensions, size and type
303 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
304 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
305 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
306 } elsif ($result =~ m/([0-9]+)x([0-9]+)/) {
307 #if the image hasn't changed size, the previous regex doesn't match
308 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
309 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
310 }
311 } else {
312 my $outhandle = $self->{'outhandle'};
313 print $outhandle "Couldn't find screenview file $screenviewfilename\n";
314
315 }
316
317}
318
319
320
321sub convert {
322 my $self = shift(@_);
323 my $source_file_path = shift(@_);
324 my $target_file_type = shift(@_);
325 my $convert_options = shift(@_) || "";
326 my $convert_type = shift(@_) || "";
327
328 my $outhandle = $self->{'outhandle'};
329 my $verbosity = $self->{'verbosity'};
330
331 # Determine the full name and path of the output file
332 my $target_file_path = &util::get_tmp_filename($target_file_type);
333 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
334
335 # Generate and run the convert command
336 my $convert_command = "convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
337 print $outhandle "$convert_type $convert_command\n" if ($verbosity > 2);
338 my $result = `$convert_command 2>&1`;
339 print $outhandle "$convert_type RESULT = $result\n" if ($verbosity > 2);
340
341 return $result;
342}
343
344
345# Discover the characteristics of an image file with the ImageMagick
346# "identify" command.
347
348sub identify {
349 my ($image, $outhandle, $verbosity) = @_;
350
351 # Use the ImageMagick "identify" command to get the file specs
352 my $command = "identify \"$image\" 2>&1";
353 print $outhandle "$command\n" if ($verbosity > 2);
354 my $result = '';
355 $result = `$command`;
356 print $outhandle "$result\n" if ($verbosity > 3);
357
358 # Read the type, width, and height
359 my $type = 'unknown';
360 my $width = 'unknown';
361 my $height = 'unknown';
362
363 my $image_safe = quotemeta $image;
364 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
365 $type = $1;
366 $width = $2;
367 $height = $3;
368 }
369
370 # Read the size
371 my $size = "unknown";
372 if ($result =~ m/^.* ([0-9]+)b/) {
373 $size = $1;
374 }
375 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
376 $size = 1024 * $1;
377 if (defined($2)) {
378 $size = $size + (1024 * $2);
379 # Truncate size (it isn't going to be very accurate anyway)
380 $size = int($size);
381 }
382 }
383 elsif ($result =~ m/^.* (([0-9]+)(\.([0-9]+))?e\+([0-9]+))(kb|b)?/) {
384 # Deals with file sizes on Linux of type "3.4e+02kb" where e+02 is 1*10^2.
385 # 3.4e+02 therefore evaluates to 3.4 x 1 x 10^2 = 340kb.
386 # Programming languages including Perl know how that 3.4e+02 is a number,
387 # so we don't need to do any calculations.
388 $size = $1*1; # turn the string into a number by multiplying it by 1
389 #if we did $size = $1; $size would be merely the string "3.4e+02"
390 $size = int($size); # truncate size
391 }
392 print $outhandle "file: $image:\t $type, $width, $height, $size\n"
393 if ($verbosity > 2);
394
395 # Return the specs
396 return ($type, $width, $height, $size);
397}
398
399sub clean_up_temporary_files {
400 my $self = shift(@_);
401
402 foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}}) {
403 if (-e $tmp_file_path) {
404 &util::rm($tmp_file_path);
405 }
406 }
407
408}
409
4101;
Note: See TracBrowser for help on using the repository browser.