root/main/trunk/greenstone2/perllib/plugins/ImageConverter.pm @ 31492

Revision 31492, 23.8 KB (checked in by kjdon, 3 years ago)

renamed EncodingUtil? to CommonUtil?, BasePlugin? to BaseImporter?. The idea is that only top level plugins that you can specify in your collection get to have plugin in their name. Modified all other plugins to reflect these name changes

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