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

Last change on this file since 32183 was 32183, checked in by kjdon, 6 years ago

image height and width might be returned as 'unknown', in particular if something has gone wrong with image magick. so lets check for that so we don't get 'Argument unknown isn't numeric in numeric gt (>)' type errors

  • Property svn:executable set to *
File size: 24.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 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' => "{aspectpad.al}"},
119 {'name' => "ap", 'desc' => "{aspectpad.ap}"},
120 {'name' => "l", 'desc' => "{aspectpad.l}"},
121 {'name' => "p", 'desc' => "{aspectpad.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, $converted_filename_full_path)
266 = $self->convert($filename_full_path, $converttotype, "", "CONVERTTYPE");
267
268 $type = $converttotype;
269 $filename_full_path = $converted_filename_full_path;
270 $filename_no_path = "$filehead.$type";
271 $url_to_filename_no_path = "$url_to_filehead.$type";
272 if ($self->{'store_file_paths'}) {
273 $self->{'orig_file'} = $converted_filename_full_path;
274 }
275 $original_file_was_converted = 1;
276 }
277
278 # Apply aspect padding (if required).
279 my $apply_aspectpad = $self->{'apply_aspectpad'};
280
281 if ($apply_aspectpad eq "true") {
282 my $aspectpad_ratio = $self->{'aspectpad_ratio'};
283 my $aspectpad_mode = $self->{'aspectpad_mode'};
284 my $aspectpad_colour = $self->{'aspectpad_colour'};
285 my $aspectpad_tolerance = $self->{'aspectpad_tolerance'};
286
287 ($type) = ($filename_full_path =~ m/\.(.*?)$/);
288 ##$type = lc($type);
289
290 my ($result, $aspectpad_filename_full_path)
291 = $self->aspectpad($filename_full_path, $type, $aspectpad_ratio, $aspectpad_mode,
292 $aspectpad_colour, $aspectpad_tolerance, "", "ASPECTPAD");
293
294 $filename_full_path = $aspectpad_filename_full_path;
295
296 if ($self->{'store_file_paths'}) {
297 $self->{'orig_file'} = $aspectpad_filename_full_path;
298 }
299 $original_file_was_converted = 1;
300
301 }
302
303 # add Image metadata
304 $doc_obj->add_utf8_metadata($section, "Image", $url_to_filename_no_path); # url to generated image
305
306 # here we overwrite the original with the potentially converted one
307# $doc_obj->set_utf8_metadata_element($section, "Source", &unicode::url_decode($filename_no_path)); # displayname of generated image
308# $doc_obj->set_utf8_metadata_element($section, "SourceFile", $url_to_filename_no_path); # displayname of generated image
309
310# $self->set_Source_metadata($doc_obj,$url_to_filename_no_path,undef);
311
312 my $raw_filename_full_path = &unicode::url_decode($filename_encoded_full_path);
313 $self->set_Source_metadata($doc_obj,$raw_filename_full_path,
314 $filename_encoding, $section);
315
316
317 # use identify to get info about the (possibly converted) image
318 my ($image_type, $image_width, $image_height, $image_size, $size_str)
319 = &identify($filename_full_path, $outhandle, $verbosity);
320
321 if ($image_type ne " ") {
322 $type = $self->correct_mime_type($image_type);
323 }
324
325 #overwrite the ones added in BaseImporter
326 $doc_obj->set_metadata_element ($section, "FileFormat", $type);
327 my $sys_file_size = &FileUtils::fileSize($filename_full_path);
328 $doc_obj->set_metadata_element ($section, "FileSize", $sys_file_size); #$image_size);
329
330 $doc_obj->add_metadata ($section, "ImageType", $image_type);
331 $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
332 $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
333 $doc_obj->add_metadata ($section, "ImageSize", $size_str);
334
335 if ((defined $self->{'MaxImageWidth'}) && ($image_width ne "unknown")
336 && ($image_width > $self->{'MaxImageWidth'})) {
337 $self->{'MaxImageWidth'} = $image_width;
338 }
339 if ((defined $self->{'MaxImageHeight'}) && ($image_height ne "unknown")
340 && ($image_height > $self->{'MaxImageHeight'})) {
341 $self->{'MaxImageHeight'} = $image_height;
342 }
343
344 # srclink_file is now deprecated because of the "_" in the metadataname. Use srclinkFile
345 $doc_obj->add_metadata ($section, "srclink_file", $url_to_filename_no_path);
346 $doc_obj->add_metadata ($section, "srclinkFile", $url_to_filename_no_path);
347 $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[srclinkFile]\" width=\"[ImageWidth]\" height=\"[ImageHeight]\">");
348
349 # Add the image as an associated file
350 $doc_obj->associate_file($filename_full_path, $filename_no_path, "image/$type", $section);
351
352 if ($self->{'store_original_image'} && $original_file_was_converted) {
353
354 # work out the file type
355 # use identify to get info about the original image
356 my ($orig_type, $orig_width, $orig_height, $orig_size, $origsize_str)
357 = &identify($original_filename_full_path, $outhandle, $verbosity);
358
359 if ($orig_type ne " ") {
360 $orig_type = $self->correct_mime_type($orig_type);
361 }
362
363 # add the original image as an associated file
364 $doc_obj->associate_file($original_filename_full_path, $original_filename_no_path, "image/$orig_type", $section);
365 }
366 if ($self->{'create_thumbnail'} eq "true") {
367 $self->create_thumbnail($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead);
368 }
369 if ($self->{'create_screenview'} eq "true") {
370 $self->create_screenview($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height);
371 }
372
373 return 1;
374}
375
376sub create_thumbnail {
377 my $self = shift(@_);
378 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead) = @_;
379 $url_to_filehead = $filehead unless defined $url_to_filehead;
380
381 my $thumbnailsize = $self->{'thumbnailsize'};
382 my $thumbnailtype = $self->correct_mime_type($self->{'thumbnailtype'});
383
384 # Generate the thumbnail with convert
385 my ($result,$thumbnailfile)
386 = $self->convert($original_file, $thumbnailtype, "-geometry $thumbnailsize" . "x$thumbnailsize", "THUMB");
387
388 # Add the thumbnail as an associated file ...
389 if (-e "$thumbnailfile") {
390 $doc_obj->associate_file("$thumbnailfile", $filehead."_thumb.$thumbnailtype",
391 "image/$thumbnailtype",$section); # name of generated image
392 $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
393 $doc_obj->add_utf8_metadata ($section, "Thumb", $url_to_filehead."_thumb.$thumbnailtype"); # url to generated image
394
395 $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Thumb]\" alt=\"[Thumb]\" width=\"[ThumbWidth]\" height=\"[ThumbHeight]\">");
396
397
398 # Extract Thumbnail metadata from convert output
399 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
400 $doc_obj->add_metadata ($section, "ThumbWidth", $1);
401 $doc_obj->add_metadata ($section, "ThumbHeight", $2);
402 }
403 if ($self->{'store_file_paths'}) {
404 $self->{'thumb_file'} = $thumbnailfile;
405 }
406
407 } else {
408 my $outhandle = $self->{'outhandle'};
409 print $outhandle "Couldn't find thumbnail $thumbnailfile\n";
410
411 }
412}
413
414sub create_screenview {
415
416 my $self = shift(@_);
417 my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height) = @_;
418 $url_to_filehead = $filehead unless defined $url_to_filehead;
419
420 my $screenviewsize = $self->{'screenviewsize'};
421 my $screenviewtype = $self->correct_mime_type($self->{'screenviewtype'});
422
423 # Scale the image, unless the original image is smaller than the screenview size and -noscaleup is set
424 my $scale_option = "-geometry $screenviewsize" . "x$screenviewsize";
425 if ($self->{'noscaleup'} && $image_width < $screenviewsize && $image_height < $screenviewsize)
426 {
427 $scale_option = "";
428 }
429
430 # make the screenview image
431 my ($result,$screenviewfilename)
432 = $self->convert($original_file, $screenviewtype, $scale_option, "SCREEN");
433
434 #add the screenview as an associated file ...
435 if (-e "$screenviewfilename") {
436 $doc_obj->associate_file("$screenviewfilename", $filehead."_screen.$screenviewtype", "image/$screenviewtype",$section); # name of generated image
437 $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
438 $doc_obj->add_utf8_metadata ($section, "Screen", $url_to_filehead."_screen.$screenviewtype"); # url to generated image
439
440 $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
441
442 # get screenview dimensions, size and type
443 if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
444 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
445 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
446 } elsif ($result =~ m/([0-9]+)x([0-9]+)/) {
447 #if the image hasn't changed size, the previous regex doesn't match
448 $doc_obj->add_metadata ($section, "ScreenWidth", $1);
449 $doc_obj->add_metadata ($section, "ScreenHeight", $2);
450 }
451
452 if ($self->{'store_file_paths'}) {
453 $self->{'screen_file'} = $screenviewfilename;
454 }
455
456 } else {
457 my $outhandle = $self->{'outhandle'};
458 print $outhandle "Couldn't find screenview file $screenviewfilename\n";
459
460 }
461
462}
463
464
465
466sub convert {
467 my $self = shift(@_);
468 my $source_file_path = shift(@_);
469 my $target_file_type = shift(@_);
470 my $convert_options = shift(@_) || "";
471 my $convert_id = shift(@_) || "";
472 my $cache_mode = shift(@_) || "";
473
474 my $outhandle = $self->{'outhandle'};
475 my $verbosity = $self->{'verbosity'};
476
477 my $source_file_no_path = &File::Basename::basename($source_file_path);
478
479 # Determine the full name and path of the output file
480 my $target_file_path;
481 if ($self->{'enable_cache'}) {
482 my $cached_image_dir = $self->{'cached_dir'};
483 my $image_root = $self->{'cached_file_root'};
484 $image_root .= "_$convert_id" if ($convert_id ne "");
485 my $image_file = "$image_root.$target_file_type";
486 $target_file_path = &FileUtils::filenameConcatenate($cached_image_dir,$image_file);
487 }
488 else {
489 $target_file_path = &util::get_tmp_filename($target_file_type);
490 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
491
492 # Output filename used to be parsed from result line:
493 # my ($ofilename) = ($result =~ m/=>(.*\.$target_file_type)/);
494 # by the function that called 'convert'
495 # but this is no longer needed, as output filename is now
496 # explicitly passed back
497 }
498
499 # Generate and run the convert command
500 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\"";
501
502 my $print_info = { 'message_prefix' => $convert_id,
503 'message' => "Converting image $source_file_no_path to: $convert_id $target_file_type" };
504 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
505
506 my ($regenerated,$result,$had_error)
507 = $self->autorun_general_cmd($convert_command,$source_file_path,$target_file_path,$print_info);
508
509 return ($result,$target_file_path);
510}
511
512
513sub convert_without_result {
514 my $self = shift(@_);
515
516 my $source_file_path = shift(@_);
517 my $target_file_type = shift(@_);
518 my $convert_options = shift(@_) || "";
519 my $convert_id = shift(@_) || "";
520
521 return $self->convert($source_file_path,$target_file_type,
522 $convert_options,$convert_id,"without_result");
523}
524
525
526sub aspectpad {
527 my $self = shift(@_);
528 my $source_file_path = shift(@_);
529 my $target_file_type = shift(@_);
530 my $aspectpad_ratio = shift(@_);
531 my $aspectpad_mode = shift(@_);
532 my $aspectpad_colour = shift(@_);
533 my $aspectpad_tolerance = shift(@_);
534
535 my $aspectpad_options = shift(@_) || "";
536 my $aspectpad_id = shift(@_) || "";
537 my $cache_mode = shift(@_) || "";
538
539 my $outhandle = $self->{'outhandle'};
540 my $verbosity = $self->{'verbosity'};
541
542 my $source_file_no_path = &File::Basename::basename($source_file_path);
543
544 # Determine the full name and path of the output file
545 my $target_file_path;
546 if ($self->{'enable_cache'}) {
547 my $cached_image_dir = $self->{'cached_dir'};
548 my $image_root = $self->{'cached_file_root'};
549 $image_root .= "_$aspectpad_id" if ($aspectpad_id ne "");
550 my $image_file = "$image_root.$target_file_type";
551 $target_file_path = &FileUtils::filenameConcatenate($cached_image_dir,$image_file);
552 }
553 else {
554 $target_file_path = &util::get_tmp_filename($target_file_type);
555 push(@{$self->{'tmp_file_paths'}}, $target_file_path);
556 }
557
558 # Generate and run the aspectpad command
559 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\"";
560
561 my $print_info = { 'message_prefix' => $aspectpad_id,
562 'message' => "Aspect padding image $source_file_no_path to: $aspectpad_id $target_file_type" };
563 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
564
565 my ($regenerated,$result,$had_error)
566 = $self->autorun_general_cmd($aspectpad_command,$source_file_path,$target_file_path,$print_info);
567
568 return ($result,$target_file_path);
569}
570
571
572
573
574
575# Discover the characteristics of an image file with the ImageMagick
576# "identify" command.
577
578sub identify {
579 my ($image, $outhandle, $verbosity) = @_;
580
581 # Use the ImageMagick "identify" command to get the file specs
582 my $command = "\"".&util::get_perl_exec()."\" -S gs-magick.pl identify \"$image\" 2>&1";
583 print $outhandle "$command\n" if ($verbosity > 2);
584 my $result = '';
585 $result = `$command`;
586 print $outhandle "$result\n" if ($verbosity > 3);
587
588 # Read the type, width, and height
589 my $type = 'unknown';
590 my $width = 'unknown';
591 my $height = 'unknown';
592
593 my $image_safe = quotemeta $image;
594 if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
595 $type = $1;
596 $width = $2;
597 $height = $3;
598 }
599
600 # Read the size
601 my $size = "unknown";
602 my $size_str="unknown";
603
604 if ($result =~ m/^.* ([0-9]+)b/i) {
605 $size_str="$1B"; # display string
606 $size = $1;
607 }
608 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/i) {
609 # display string stays about the same
610 $size_str="$1";
611 $size_str.="$2" if defined $2;
612 $size_str.="KB";
613
614 $size = 1024 * $1;
615 if (defined($2)) {
616 $size = $size + (1024 * $2);
617 # Truncate size (it isn't going to be very accurate anyway)
618 $size = int($size);
619 }
620 }
621 elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?mb?/i) {
622 # display string stays about the same
623 $size_str="$1";
624 $size_str.="$2" if defined $2;
625 $size_str.="MB";
626
627 $size = 1024 * 1024 * $1;
628 if (defined($2)) {
629 $size = $size + (1024 * 1024 * $2);
630 # Truncate size (it isn't going to be very accurate anyway)
631 $size = int($size);
632 }
633 }
634 elsif ($result =~ m/^.* ((([0-9]+)(\.([0-9]+))?e\+([0-9]+))(kb|b)?)/i) {
635 # display string stays the same
636 $size_str="$1";
637
638 # Deals with file sizes on Linux of type "3.4e+02kb" where e+02 is 1*10^2.
639 # 3.4e+02 therefore evaluates to 3.4 x 1 x 10^2 = 340kb.
640 # Programming languages including Perl know how that 3.4e+02 is a number,
641 # so we don't need to do any calculations.
642 # $2 is just the number without the kb/b at the end.
643 $size = $2*1; # turn the string into a number by multiplying it by 1
644 #if we did $size = $1; $size would be merely the string "3.4e+02"
645 $size = int($size); # truncate size
646 }
647 print $outhandle "file: $image:\t $type, $width, $height, $size, $size_str\n"
648 if ($verbosity > 2);
649
650 # Return the specs
651 return ($type, $width, $height, $size, $size_str);
652}
653
654sub clean_up_temporary_files {
655 my $self = shift(@_);
656
657 foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}}) {
658 if (-e $tmp_file_path) {
659 &FileUtils::removeFiles($tmp_file_path);
660 }
661 }
662
663}
664
665# image/jpg is not a valid mime-type, it ought to be image/jpeg.
666# Sometimes JPEG is passed in also, want to keep things lowercase just in case.
667sub correct_mime_type {
668 my $self = shift(@_);
669 my ($file_extension) = @_;
670
671 $file_extension = lc($file_extension);
672 $file_extension =~ s/jpg/jpeg/s;
673
674 return $file_extension;
675}
676
6771;
Note: See TracBrowser for help on using the repository browser.