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

Last change on this file since 32783 was 32783, checked in by kjdon, 2 years ago

adding missing strings and tidying up some mislabelling

  • Property svn:executable set to *
File size: 25.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 util;
35use FileUtils;
36use gsprintf 'gsprintf';
37
38BEGIN {
39 @ImageConverter::ISA = ('BaseMediaConverter');
40}
41
42# When used with multiple builder+buildproc, plugins loaded multiple times
43# => use this 'our' var to ensure only see the warning about ImageMagick once
44our $given_image_conversion_warning = 0;
45
46my $arguments = [
47 { 'name' => "create_thumbnail",
48 'desc' => "{ImageConverter.create_thumbnail}",
49 'type' => "enum",
50 'list' => [{'name' => "true", 'desc' => "{common.true}"},
51 {'name' => "false", 'desc' => "{common.false}"}],
52 'deft' => "true",
53 'reqd' => "no" },
54 { 'name' => "thumbnailsize",
55 'desc' => "{ImageConverter.thumbnailsize}",
56 'type' => "int",
57 'deft' => "100",
58 'range' => "1,",
59 'reqd' => "no" },
60 { 'name' => "thumbnailtype",
61 'desc' => "{ImageConverter.thumbnailtype}",
62 'type' => "string",
63 'deft' => "gif",
64 'reqd' => "no" },
65 { 'name' => "noscaleup",
66 'desc' => "{ImageConverter.noscaleup}",
67 'type' => "flag",
68 'reqd' => "no" },
69 { 'name' => "create_screenview",
70 'desc' => "{ImageConverter.create_screenview}",
71 'type' => "enum",
72 'list' => [{'name' => "true", 'desc' => "{common.true}"},
73 {'name' => "false", 'desc' => "{common.false}"}],
74 'deft' => "true",
75 'reqd' => "no" },
76 { 'name' => "screenviewsize",
77 'desc' => "{ImageConverter.screenviewsize}",
78 'type' => "int",
79 'deft' => "500",
80 'range' => "1,",
81 'reqd' => "no" },
82 { 'name' => "screenviewtype",
83 'desc' => "{ImageConverter.screenviewtype}",
84 'type' => "string",
85 'deft' => "jpg",
86 'reqd' => "no" },
87 { 'name' => "converttotype",
88 'desc' => "{ImageConverter.converttotype}",
89 'type' => "string",
90 'deft' => "",
91 'reqd' => "no" },
92 { 'name' => "minimumsize",
93 'desc' => "{ImageConverter.minimumsize}",
94 'type' => "int",
95 'deft' => "100",
96 'range' => "1,",
97 'reqd' => "no" },
98 { 'name' => "store_original_image",
99 'desc' => "{ImageConverter.store_original_image}",
100 'type' => "flag",
101 'reqd' => "no"},
102 { 'name' => "apply_aspectpad",
103 'desc' => "{ImageConverter.apply_aspectpad}",
104 'type' => "enum",
105 'list' => [{'name' => "true", 'desc' => "{common.true}"},
106 {'name' => "false", 'desc' => "{common.false}"}],
107 'deft' => "false",
108 'reqd' => "no" },
109 { 'name' => "aspectpad_ratio",
110 'desc' => "{ImageConverter.aspectpad_ratio}",
111 'type' => "string",
112 'deft' => "2",
113 'range' => "1,",
114 'reqd' => "no" },
115 { 'name' => "aspectpad_mode",
116 'desc' => "{ImageConverter.aspectpad_mode}",
117 'type' => "enum",
118 'list' => [{'name' => "al", 'desc' => "{ImageConverter.aspectpad_mode.al}"},
119 {'name' => "ap", 'desc' => "{ImageConverter.aspectpad_mode.ap}"},
120 {'name' => "l", 'desc' => "{ImageConverter.aspectpad_mode.l}"},
121 {'name' => "p", 'desc' => "{ImageConverter.aspectpad_mode.p}"}],
122 'deft' => "al",
123 'reqd' => "no" },
124 { 'name' => "aspectpad_colour",
125 'desc' => "{ImageConverter.aspectpad_colour}",
126 'type' => "string",
127 'deft' => "transparent",
128 'reqd' => "no" },
129 { 'name' => "aspectpad_tolerance",
130 'desc' => "{ImageConverter.aspectpad_tolerance}",
131 'type' => "string",
132 'deft' => "0.0",
133 'range' => "0,",
134 'reqd' => "no" },
135
136
137 ];
138
139my $options = { 'name' => "ImageConverter",
140 'desc' => "{ImageConverter.desc}",
141 'abstract' => "yes",
142 'inherits' => "yes",
143 'args' => $arguments };
144
145sub new {
146 my ($class) = shift (@_);
147 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
148 push(@$pluginlist, $class);
149
150 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
151 push(@{$hashArgOptLists->{"OptList"}},$options);
152
153 my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
154
155 return bless $self, $class;
156
157}
158
159# needs to be called after BaseImporter init, so that outhandle is set up.
160sub init {
161 my $self = shift(@_);
162
163 $self->{'tmp_file_paths'} = ();
164
165 # Check that ImageMagick is installed and available on the path
166 my $image_conversion_available = 1;
167 my $no_image_conversion_reason = "";
168 # None of this works very well on Windows 95/98...
169 if ($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT()) {
170 $image_conversion_available = 0;
171 $no_image_conversion_reason = "win95notsupported";
172 } else {
173 my $imagick_cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl";
174 my $result = `$imagick_cmd identify -help 2>&1`;
175 my $return_value = $?;
176
177 # When testing against non-zero return_value ($?), need to shift by 8
178 # and convert it to its signed value. Linux returns -1 and Windows returns
179 # 256 for "program not found". The signed equivalents are -1 and 1 respectively.
180 $return_value >>= 8;
181 $return_value = (($return_value & 0x80) ? -(0x100 - ($return_value & 0xFF)) : $return_value);
182
183 if ( ($ENV{'GSDLOS'} eq "windows" && $return_value == 1) || $return_value == -1) { # Linux and Windows return different values for "program not found"
184 $image_conversion_available = 0;
185 $no_image_conversion_reason = "imagemagicknotinstalled";
186 }
187 }
188 $self->{'image_conversion_available'} = $image_conversion_available;
189 $self->{'no_image_conversion_reason'} = $no_image_conversion_reason;
190
191 if ($self->{'image_conversion_available'} == 0) {
192 if (!$given_image_conversion_warning) {
193 my $outhandle = $self->{'outhandle'};
194 &gsprintf($outhandle, "ImageConverter: {ImageConverter.noconversionavailable} ({ImageConverter.".$self->{'no_image_conversion_reason'}."})\n");
195 $given_image_conversion_warning = 1;
196 }
197 }
198
199}
200
201
202# convert image to new type if converttotype is set
203# generate thumbnails if required
204# generate screenview if required
205# discover image metadata
206# filename_no_path must be in utf8 and url-encoded
207sub generate_images {
208 my $self = shift(@_);
209 my ($filename_full_path, $filename_encoded_full_path, $doc_obj, $section, $filename_encoding) = @_;
210
211 my ($unused_fefp,$filename_encoded_no_path)
212 = &util::get_full_filenames("",$filename_encoded_full_path);
213
214 # The following is potentially very muddled thinking (but currently seems to work)
215 # generate_images currently called from ImagePlugin and PagedImagePlugin
216 my $filename_no_path = $filename_encoded_no_path;
217 my $original_filename_full_path = $filename_full_path;
218 my $original_filename_no_path = $filename_no_path;
219 my $original_file_was_converted = 0;
220
221 # check image magick status
222 return 0 if $self->{'image_conversion_available'} == 0;
223
224 # check the filenames
225 return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
226
227 if ($self->{'enable_cache'}) {
228 $self->init_cache_for_file($filename_full_path);
229 }
230 if ($self->{'store_file_paths'}) {
231 $self->{'orig_file'} = "";
232 $self->{'thumb_file'} = "";
233 $self->{'screen_file'} = "";
234 }
235 my $verbosity = $self->{'verbosity'};
236 my $outhandle = $self->{'outhandle'};
237
238 # check the size of the image against minimum size if specified
239 my $minimumsize = $self->{'minimumsize'};
240 if (defined $minimumsize && (&FileUtils::fileSize($filename_full_path) < $minimumsize)) {
241 print $outhandle "ImageConverter: \"$filename_full_path\" too small, skipping\n"
242 if ($verbosity > 1);
243 return 0; # or is there a better return value??
244 }
245
246 my $filehead = $filename_no_path;
247 $filehead =~ s/\.([^\.]*)$//; # filename with no extension
248 my $assocfilemeta = "[assocfilepath]";
249 if ($section ne $doc_obj->get_top_section()) {
250 $assocfilemeta = "[parent(Top):assocfilepath]";
251 }
252
253 # The images that will get generated may contain percent signs in their src filenames
254 # Encode those percent signs themselves so that urls to the imgs refer to them correctly
255 my $url_to_filehead = &unicode::filename_to_url($filehead);
256 my $url_to_filename_no_path = &unicode::filename_to_url($filename_no_path);
257
258 my $type = "unknown";
259
260 # Convert the image to a new type (if required).
261 my $converttotype = $self->{'converttotype'};
262
263 if ($converttotype ne "" && $filename_full_path !~ m/$converttotype$/) {
264# # $doc_obj->add_utf8_metadata($section, "Image", $utf8_filename_meta);
265 my ($result, $result_str, $converted_filename_full_path)
266 = $self->convert($filename_full_path, $converttotype, "", "CONVERTTYPE");
267
268 if (!defined $result || $result == 0) {
269 # conversion error
270 print $outhandle "ImageConverter:convert error\n";
271 print $outhandle "$@\n";
272 if (defined $result_str) {
273 print $outhandle "$result_str\n";
274 }
275 return 0;
276 }
277
278 $type = $converttotype;
279 $filename_full_path = $converted_filename_full_path;
280 $filename_no_path = "$filehead.$type";
281 $url_to_filename_no_path = "$url_to_filehead.$type";
282 if ($self->{'store_file_paths'}) {
283 $self->{'orig_file'} = $converted_filename_full_path;
284 }
285 $original_file_was_converted = 1;
286 }
287
288 # Apply aspect padding (if required).
289 my $apply_aspectpad = $self->{'apply_aspectpad'};
290
291 if ($apply_aspectpad eq "true") {
292 my $aspectpad_ratio = $self->{'aspectpad_ratio'};
293 my $aspectpad_mode = $self->{'aspectpad_mode'};
294 my $aspectpad_colour = $self->{'aspectpad_colour'};
295 my $aspectpad_tolerance = $self->{'aspectpad_tolerance'};
296
297 ($type) = ($filename_full_path =~ m/\.(.*?)$/);
298 ##$type = lc($type);
299
300 my ($result, $aspectpad_filename_full_path)
301 = $self->aspectpad($filename_full_path, $type, $aspectpad_ratio, $aspectpad_mode,
302 $aspectpad_colour, $aspectpad_tolerance, "", "ASPECTPAD");
303
304 $filename_full_path = $aspectpad_filename_full_path;
305
306 if ($self->{'store_file_paths'}) {
307 $self->{'orig_file'} = $aspectpad_filename_full_path;
308 }
309 $original_file_was_converted = 1;
310
311 }
312
313 # add Image metadata
314 $doc_obj->add_utf8_metadata($section, "Image", $url_to_filename_no_path); # url to generated image
315
316 # here we overwrite the original with the potentially converted one
317# $doc_obj->set_utf8_metadata_element($section, "Source", &unicode::url_decode($filename_no_path)); # displayname of generated image
318# $doc_obj->set_utf8_metadata_element($section, "SourceFile", $url_to_filename_no_path); # displayname of generated image
319
320# $self->set_Source_metadata($doc_obj,$url_to_filename_no_path,undef);
321
322 my $raw_filename_full_path = &unicode::url_decode($filename_encoded_full_path);
323 $self->set_Source_metadata($doc_obj,$raw_filename_full_path,
324 $filename_encoding, $section);
325
326
327 # use identify to get info about the (possibly converted) image
328 my ($image_type, $image_width, $image_height, $image_size, $size_str)
329 = &identify($filename_full_path, $outhandle, $verbosity);
330
331 if ($image_type ne " ") {
332 $type = $self->correct_mime_type($image_type);
333 }
334
335 #overwrite the ones added in BaseImporter
336 $doc_obj->set_metadata_element ($section, "FileFormat", $type);
337 my $sys_file_size = &FileUtils::fileSize($filename_full_path);
338 $doc_obj->set_metadata_element ($section, "FileSize", $sys_file_size); #$image_size);
339
340 $doc_obj->add_metadata ($section, "ImageType", $image_type);
341 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
342 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
343 $doc_obj->add_metadata ($section, "ImageSize", $size_str);
344
345 if ((defined $self->{'MaxImageWidth'}) && ($image_width ne "unknown")
346 && ($image_width > $self->{'MaxImageWidth'})) {
347 $self->{'MaxImageWidth'} = $image_width;
348 }
349 if ((defined $self->{'MaxImageHeight'}) && ($image_height ne "unknown")
350 && ($image_height > $self->{'MaxImageHeight'})) {
351 $self->{'MaxImageHeight'} = $image_height;
352 }
353
354 # srclink_file is now deprecated because of the "_" in the metadataname. Use srclinkFile
355 $doc_obj->add_metadata ($section, "srclink_file", $url_to_filename_no_path);
356 $doc_obj->add_metadata ($section, "srclinkFile", $url_to_filename_no_path);
357 $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[srclinkFile]\" width=\"[ImageWidth]\" height=\"[ImageHeight]\">");
358
359 # Add the image as an associated file
360 $doc_obj->associate_file($filename_full_path, $filename_no_path, "image/$type", $section);
361
362 if ($self->{'store_original_image'} && $original_file_was_converted) {
363
364 # work out the file type
365 # use identify to get info about the original image
366 my ($orig_type, $orig_width, $orig_height, $orig_size, $origsize_str)
367 = &identify($original_filename_full_path, $outhandle, $verbosity);
368
369 if ($orig_type ne " ") {
370 $orig_type = $self->correct_mime_type($orig_type);
371 }
372
373 # add the original image as an associated file
374 $doc_obj->associate_file($original_filename_full_path, $original_filename_no_path, "image/$orig_type", $section);
375 }
376 if ($self->{'create_thumbnail'} eq "true") {
377 $self->create_thumbnail($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead);
378 }
379 if ($self->{'create_screenview'} eq "true") {
380 $self->create_screenview($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height);
381 }
382
383 return 1;
384}
385
386sub create_thumbnail {
387 my $self = shift(@_);
388 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead) = @_;
389 $url_to_filehead = $filehead unless defined $url_to_filehead;
390
391 my $thumbnailsize = $self->{'thumbnailsize'};
392 my $thumbnailtype = $self->correct_mime_type($self->{'thumbnailtype'});
393
394 # Generate the thumbnail with convert
395 my ($result, $result_str,$thumbnailfile)
396 = $self->convert($original_file, $thumbnailtype, "-geometry $thumbnailsize" . "x$thumbnailsize", "THUMB");
397
398 # Add the thumbnail as an associated file ...
399 if (-e "$thumbnailfile") {
400 $doc_obj->associate_file("$thumbnailfile", $filehead."_thumb.$thumbnailtype",
401 "image/$thumbnailtype",$section); # name of generated image
402 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
403 $doc_obj->add_utf8_metadata ($section, "Thumb", $url_to_filehead."_thumb.$thumbnailtype"); # url to generated image
404
405 $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Thumb]\" alt=\"[Thumb]\" width=\"[ThumbWidth]\" height=\"[ThumbHeight]\">");
406
407
408 # Extract Thumbnail metadata from convert output
409 if ($result_str =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
410 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
411 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
412 }
413 if ($self->{'store_file_paths'}) {
414 $self->{'thumb_file'} = $thumbnailfile;
415 }
416
417 } else {
418 my $outhandle = $self->{'outhandle'};
419 print $outhandle "Couldn't find thumbnail $thumbnailfile\n";
420
421 }
422}
423
424sub create_screenview {
425
426 my $self = shift(@_);
427 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height) = @_;
428 $url_to_filehead = $filehead unless defined $url_to_filehead;
429
430 my $screenviewsize = $self->{'screenviewsize'};
431 my $screenviewtype = $self->correct_mime_type($self->{'screenviewtype'});
432
433 # Scale the image, unless the original image is smaller than the screenview size and -noscaleup is set
434 my $scale_option = "-geometry $screenviewsize" . "x$screenviewsize";
435 if ($self->{'noscaleup'} && $image_width < $screenviewsize && $image_height < $screenviewsize)
436 {
437 $scale_option = "";
438 }
439
440 # make the screenview image
441 my ($result,$result_str, $screenviewfilename)
442 = $self->convert($original_file, $screenviewtype, $scale_option, "SCREEN");
443
444 #add the screenview as an associated file ...
445 if (-e "$screenviewfilename") {
446 $doc_obj->associate_file("$screenviewfilename", $filehead."_screen.$screenviewtype", "image/$screenviewtype",$section); # name of generated image
447 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
448 $doc_obj->add_utf8_metadata ($section, "Screen", $url_to_filehead."_screen.$screenviewtype"); # url to generated image
449
450 $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
451
452 # get screenview dimensions, size and type
453 if ($result_str =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
454 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
455 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
456 } elsif ($result_str =~ m/([0-9]+)x([0-9]+)/) {
457 #if the image hasn't changed size, the previous regex doesn't match
458 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
459 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
460 }
461
462 if ($self->{'store_file_paths'}) {
463 $self->{'screen_file'} = $screenviewfilename;
464 }
465
466 } else {
467 my $outhandle = $self->{'outhandle'};
468 print $outhandle "Couldn't find screenview file $screenviewfilename\n";
469
470 }
471
472}
473
474
475
476sub convert {
477 my $self = shift(@_);
478 my $source_file_path = shift(@_);
479 my $target_file_type = shift(@_);
480 my $convert_options = shift(@_) || "";
481 my $convert_id = shift(@_) || "";
482 my $cache_mode = shift(@_) || "";
483
484 my $outhandle = $self->{'outhandle'};
485 my $verbosity = $self->{'verbosity'};
486
487 my $source_file_no_path = &File::Basename::basename($source_file_path);
488
489 # Determine the full name and path of the output file
490 my $target_file_path;
491 if ($self->{'enable_cache'}) {
492 my $cached_image_dir = $self->{'cached_dir'};
493 my $image_root = $self->{'cached_file_root'};
494 $image_root .= "_$convert_id" if ($convert_id ne "");
495 my $image_file = "$image_root.$target_file_type";
496 $target_file_path = &FileUtils::filenameConcatenate($cached_image_dir,$image_file);
497 }
498 else {
499 $target_file_path = &util::get_tmp_filename($target_file_type);
500 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
501
502 # Output filename used to be parsed from result line:
503 # my ($ofilename) = ($result =~ m/=>(.*\.$target_file_type)/);
504 # by the function that called 'convert'
505 # but this is no longer needed, as output filename is now
506 # explicitly passed back
507 }
508
509 # Generate and run the convert command
510 my $convert_command = "\"".&util::get_perl_exec()."\" -S gs-magick.pl --verbosity=".$self->{'verbosity'}." convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
511
512 my $print_info = { 'message_prefix' => $convert_id,
513 'message' => "Converting image $source_file_no_path to: $convert_id $target_file_type" };
514 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
515
516 my ($regenerated,$result,$had_error)
517 = $self->autorun_general_cmd($convert_command,$source_file_path,$target_file_path,$print_info);
518
519 if ($had_error) {
520 return (0, $result, $target_file_path);
521 }
522
523 return (1, $result,$target_file_path);
524}
525
526
527sub convert_without_result {
528 my $self = shift(@_);
529
530 my $source_file_path = shift(@_);
531 my $target_file_type = shift(@_);
532 my $convert_options = shift(@_) || "";
533 my $convert_id = shift(@_) || "";
534
535 return $self->convert($source_file_path,$target_file_type,
536 $convert_options,$convert_id,"without_result");
537}
538
539
540sub aspectpad {
541 my $self = shift(@_);
542 my $source_file_path = shift(@_);
543 my $target_file_type = shift(@_);
544 my $aspectpad_ratio = shift(@_);
545 my $aspectpad_mode = shift(@_);
546 my $aspectpad_colour = shift(@_);
547 my $aspectpad_tolerance = shift(@_);
548
549 my $aspectpad_options = shift(@_) || "";
550 my $aspectpad_id = shift(@_) || "";
551 my $cache_mode = shift(@_) || "";
552
553 my $outhandle = $self->{'outhandle'};
554 my $verbosity = $self->{'verbosity'};
555
556 my $source_file_no_path = &File::Basename::basename($source_file_path);
557
558 # Determine the full name and path of the output file
559 my $target_file_path;
560 if ($self->{'enable_cache'}) {
561 my $cached_image_dir = $self->{'cached_dir'};
562 my $image_root = $self->{'cached_file_root'};
563 $image_root .= "_$aspectpad_id" if ($aspectpad_id ne "");
564 my $image_file = "$image_root.$target_file_type";
565 $target_file_path = &FileUtils::filenameConcatenate($cached_image_dir,$image_file);
566 }
567 else {
568 $target_file_path = &util::get_tmp_filename($target_file_type);
569 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
570 }
571
572 # Generate and run the aspectpad command
573 my $aspectpad_command = "\"".&util::get_perl_exec()."\" -S gs-magick.pl --verbosity=".$self->{'verbosity'}." aspectpad.sh -a $aspectpad_ratio -m $aspectpad_mode -p \"$aspectpad_colour\" -t $aspectpad_tolerance $aspectpad_options \"$source_file_path\" \"$target_file_path\"";
574
575 my $print_info = { 'message_prefix' => $aspectpad_id,
576 'message' => "Aspect padding image $source_file_no_path to: $aspectpad_id $target_file_type" };
577 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
578
579 my ($regenerated,$result,$had_error)
580 = $self->autorun_general_cmd($aspectpad_command,$source_file_path,$target_file_path,$print_info);
581
582 return ($result,$target_file_path);
583}
584
585
586
587
588
589# Discover the characteristics of an image file with the ImageMagick
590# "identify" command.
591
592sub identify {
593 my ($image, $outhandle, $verbosity) = @_;
594
595 # Use the ImageMagick "identify" command to get the file specs
596 my $command = "\"".&util::get_perl_exec()."\" -S gs-magick.pl identify \"$image\" 2>&1";
597 print $outhandle "$command\n" if ($verbosity > 2);
598 my $result = '';
599 $result = `$command`;
600 print $outhandle "$result\n" if ($verbosity > 3);
601
602 # Read the type, width, and height
603 my $type = 'unknown';
604 my $width = 'unknown';
605 my $height = 'unknown';
606
607 my $image_safe = quotemeta $image;
608 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
609 $type = $1;
610 $width = $2;
611 $height = $3;
612 }
613
614 # Read the size
615 my $size = "unknown";
616 my $size_str="unknown";
617
618 if ($result =~ m/^.* ([0-9]+)b/i) {
619 $size_str="$1B"; # display string
620 $size = $1;
621 }
622 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/i) {
623 # display string stays about the same
624 $size_str="$1";
625 $size_str.="$2" if defined $2;
626 $size_str.="KB";
627
628 $size = 1024 * $1;
629 if (defined($2)) {
630 $size = $size + (1024 * $2);
631 # Truncate size (it isn't going to be very accurate anyway)
632 $size = int($size);
633 }
634 }
635 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?mb?/i) {
636 # display string stays about the same
637 $size_str="$1";
638 $size_str.="$2" if defined $2;
639 $size_str.="MB";
640
641 $size = 1024 * 1024 * $1;
642 if (defined($2)) {
643 $size = $size + (1024 * 1024 * $2);
644 # Truncate size (it isn't going to be very accurate anyway)
645 $size = int($size);
646 }
647 }
648 elsif ($result =~ m/^.* ((([0-9]+)(\.([0-9]+))?e\+([0-9]+))(kb|b)?)/i) {
649 # display string stays the same
650 $size_str="$1";
651
652 # Deals with file sizes on Linux of type "3.4e+02kb" where e+02 is 1*10^2.
653 # 3.4e+02 therefore evaluates to 3.4 x 1 x 10^2 = 340kb.
654 # Programming languages including Perl know how that 3.4e+02 is a number,
655 # so we don't need to do any calculations.
656 # $2 is just the number without the kb/b at the end.
657 $size = $2*1; # turn the string into a number by multiplying it by 1
658 #if we did $size = $1; $size would be merely the string "3.4e+02"
659 $size = int($size); # truncate size
660 }
661 print $outhandle "file: $image:\t $type, $width, $height, $size, $size_str\n"
662 if ($verbosity > 2);
663
664 # Return the specs
665 return ($type, $width, $height, $size, $size_str);
666}
667
668sub clean_up_temporary_files {
669 my $self = shift(@_);
670
671 foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}}) {
672 if (-e $tmp_file_path) {
673 &FileUtils::removeFiles($tmp_file_path);
674 }
675 }
676
677}
678
679# image/jpg is not a valid mime-type, it ought to be image/jpeg.
680# Sometimes JPEG is passed in also, want to keep things lowercase just in case.
681sub correct_mime_type {
682 my $self = shift(@_);
683 my ($file_extension) = @_;
684
685 $file_extension = lc($file_extension);
686 $file_extension =~ s/jpg/jpeg/s;
687
688 return $file_extension;
689}
690
6911;
Note: See TracBrowser for help on using the repository browser.