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

Last change on this file was 38478, checked in by anupama, 5 months ago

Related to the previous 2 commits. Removed or commented out most debug statements, only one remains (the final filepath within the cache dir for a file that has been determined by the new subroutine). Adjusted the new subroutine's description.

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