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

Last change on this file since 26866 was 26866, checked in by davidb, 11 years ago

Introduction of -aspectpad... options. Useful when working with images that will feed into a 3D panorama

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