[16025] | 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 | ###########################################################################
|
---|
[15866] | 26 | package ImageConverter;
|
---|
| 27 |
|
---|
[16981] | 28 | use BaseMediaConverter;
|
---|
[15866] | 29 |
|
---|
[16825] | 30 |
|
---|
[15866] | 31 | use strict;
|
---|
[16382] | 32 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 33 |
|
---|
[16008] | 34 | use gsprintf 'gsprintf';
|
---|
[15866] | 35 |
|
---|
| 36 | BEGIN {
|
---|
[16981] | 37 | @ImageConverter::ISA = ('BaseMediaConverter');
|
---|
[15866] | 38 | }
|
---|
| 39 |
|
---|
| 40 | my $arguments = [
|
---|
| 41 | { 'name' => "create_thumbnail",
|
---|
[16008] | 42 | 'desc' => "{ImageConverter.create_thumbnail}",
|
---|
| 43 | 'type' => "enum",
|
---|
[16016] | 44 | 'list' => [{'name' => "true", 'desc' => "{common.true}"},
|
---|
| 45 | {'name' => "false", 'desc' => "{common.false}"}],
|
---|
[15866] | 46 | 'deft' => "true",
|
---|
| 47 | 'reqd' => "no" },
|
---|
| 48 | { 'name' => "thumbnailsize",
|
---|
| 49 | 'desc' => "{ImageConverter.thumbnailsize}",
|
---|
| 50 | 'type' => "int",
|
---|
| 51 | 'deft' => "100",
|
---|
| 52 | 'range' => "1,",
|
---|
| 53 | 'reqd' => "no" },
|
---|
| 54 | { 'name' => "thumbnailtype",
|
---|
| 55 | 'desc' => "{ImageConverter.thumbnailtype}",
|
---|
| 56 | 'type' => "string",
|
---|
| 57 | 'deft' => "gif",
|
---|
| 58 | 'reqd' => "no" },
|
---|
[16008] | 59 | { 'name' => "noscaleup",
|
---|
| 60 | 'desc' => "{ImageConverter.noscaleup}",
|
---|
| 61 | 'type' => "flag",
|
---|
| 62 | 'reqd' => "no" },
|
---|
[15866] | 63 | { 'name' => "create_screenview",
|
---|
[16008] | 64 | 'desc' => "{ImageConverter.create_screenview}",
|
---|
| 65 | 'type' => "enum",
|
---|
[16016] | 66 | 'list' => [{'name' => "true", 'desc' => "{common.true}"},
|
---|
| 67 | {'name' => "false", 'desc' => "{common.false}"}],
|
---|
[15866] | 68 | 'deft' => "true",
|
---|
| 69 | 'reqd' => "no" },
|
---|
| 70 | { 'name' => "screenviewsize",
|
---|
| 71 | 'desc' => "{ImageConverter.screenviewsize}",
|
---|
| 72 | 'type' => "int",
|
---|
| 73 | 'deft' => "500",
|
---|
| 74 | 'range' => "1,",
|
---|
| 75 | 'reqd' => "no" },
|
---|
| 76 | { 'name' => "screenviewtype",
|
---|
| 77 | 'desc' => "{ImageConverter.screenviewtype}",
|
---|
| 78 | 'type' => "string",
|
---|
| 79 | 'deft' => "jpg",
|
---|
| 80 | 'reqd' => "no" },
|
---|
| 81 | { 'name' => "converttotype",
|
---|
| 82 | 'desc' => "{ImageConverter.converttotype}",
|
---|
| 83 | 'type' => "string",
|
---|
| 84 | 'deft' => "",
|
---|
| 85 | 'reqd' => "no" },
|
---|
| 86 | { 'name' => "minimumsize",
|
---|
| 87 | 'desc' => "{ImageConverter.minimumsize}",
|
---|
| 88 | 'type' => "int",
|
---|
| 89 | 'deft' => "100",
|
---|
| 90 | 'range' => "1,",
|
---|
[18555] | 91 | 'reqd' => "no" }
|
---|
[15866] | 92 | ];
|
---|
| 93 |
|
---|
| 94 | my $options = { 'name' => "ImageConverter",
|
---|
| 95 | 'desc' => "{ImageConverter.desc}",
|
---|
| 96 | 'abstract' => "yes",
|
---|
| 97 | 'inherits' => "yes",
|
---|
| 98 | 'args' => $arguments };
|
---|
| 99 |
|
---|
| 100 | sub new {
|
---|
| 101 | my ($class) = shift (@_);
|
---|
| 102 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 103 | push(@$pluginlist, $class);
|
---|
| 104 |
|
---|
| 105 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 106 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
| 107 |
|
---|
[16981] | 108 | my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists, 1);
|
---|
[15866] | 109 |
|
---|
[16825] | 110 |
|
---|
[15866] | 111 | return bless $self, $class;
|
---|
| 112 |
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[16008] | 115 | # needs to be called after BasePlugin init, so that outhandle is set up.
|
---|
[15866] | 116 | sub init {
|
---|
| 117 | my $self = shift(@_);
|
---|
| 118 |
|
---|
| 119 | $self->{'tmp_file_paths'} = ();
|
---|
| 120 |
|
---|
[16008] | 121 | # Check that ImageMagick is installed and available on the path
|
---|
| 122 | my $image_conversion_available = 1;
|
---|
| 123 | my $no_image_conversion_reason = "";
|
---|
[15866] | 124 | # None of this works very well on Windows 95/98...
|
---|
| 125 | if ($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT()) {
|
---|
[16008] | 126 | $image_conversion_available = 0;
|
---|
| 127 | $no_image_conversion_reason = "win95notsupported";
|
---|
[15866] | 128 | } else {
|
---|
[19483] | 129 | my $result = `identify -help 2>&1`;
|
---|
[19420] | 130 | my $return_value = $?;
|
---|
| 131 | if ( ($ENV{'GSDLOS'} eq "windows" && $return_value == 256) || $return_value == -1) { # Linux and Windows return different values for "program not found"
|
---|
[16008] | 132 | $image_conversion_available = 0;
|
---|
| 133 | $no_image_conversion_reason = "imagemagicknotinstalled";
|
---|
[15866] | 134 | }
|
---|
| 135 | }
|
---|
[16008] | 136 | $self->{'image_conversion_available'} = $image_conversion_available;
|
---|
| 137 | $self->{'no_image_conversion_reason'} = $no_image_conversion_reason;
|
---|
| 138 |
|
---|
| 139 | if ($self->{'image_conversion_available'} == 0) {
|
---|
| 140 | my $outhandle = $self->{'outhandle'};
|
---|
| 141 | &gsprintf($outhandle, "ImageConverter: {ImageConverter.noconversionavailable} ({ImageConverter.".$self->{'no_image_conversion_reason'}."})\n");
|
---|
| 142 | }
|
---|
[16382] | 143 |
|
---|
[15866] | 144 | }
|
---|
[16008] | 145 |
|
---|
[15866] | 146 |
|
---|
| 147 | # convert image to new type if converttotype is set
|
---|
| 148 | # generate thumbnails if required
|
---|
| 149 | # generate screenview if required
|
---|
| 150 | # discover image metadata
|
---|
[16958] | 151 | # filename_no_path must be in utf8 and url-encoded
|
---|
[15866] | 152 | sub generate_images {
|
---|
| 153 | my $self = shift(@_);
|
---|
[23352] | 154 | my ($filename_full_path, $filename_encoded_full_path, $doc_obj, $section, $filename_encoding) = @_;
|
---|
[15866] | 155 |
|
---|
[23352] | 156 | my ($unused_fefp,$filename_encoded_no_path)
|
---|
| 157 | = util::get_full_filenames("",$filename_encoded_full_path);
|
---|
| 158 |
|
---|
| 159 | # The following is potentially very muddled thinking (but currently seems to work)
|
---|
| 160 | # generate_images currently called from ImagePlugin and PagedImagePlugin
|
---|
| 161 | my $filename_no_path = $filename_encoded_no_path;
|
---|
| 162 |
|
---|
[16008] | 163 | # check image magick status
|
---|
| 164 | return 0 if $self->{'image_conversion_available'} == 0;
|
---|
[18466] | 165 |
|
---|
[15866] | 166 | # check the filenames
|
---|
| 167 | return 0 if ($filename_no_path eq "" || !-f $filename_full_path);
|
---|
| 168 |
|
---|
[19054] | 169 | if ($self->{'enable_cache'}) {
|
---|
[16825] | 170 | $self->init_cache_for_file($filename_full_path);
|
---|
| 171 | }
|
---|
[21344] | 172 | if ($self->{'store_file_paths'}) {
|
---|
| 173 | $self->{'orig_file'} = "";
|
---|
| 174 | $self->{'thumb_file'} = "";
|
---|
| 175 | $self->{'screen_file'} = "";
|
---|
| 176 | }
|
---|
[15866] | 177 | my $verbosity = $self->{'verbosity'};
|
---|
| 178 | my $outhandle = $self->{'outhandle'};
|
---|
| 179 |
|
---|
| 180 | # check the size of the image against minimum size if specified
|
---|
| 181 | my $minimumsize = $self->{'minimumsize'};
|
---|
| 182 | if (defined $minimumsize && (-s $filename_full_path < $minimumsize)) {
|
---|
| 183 | print $outhandle "ImageConverter: \"$filename_full_path\" too small, skipping\n"
|
---|
| 184 | if ($verbosity > 1);
|
---|
| 185 | return 0; # or is there a better return value??
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | my $filehead = $filename_no_path;
|
---|
| 189 | $filehead =~ s/\.([^\.]*)$//; # filename with no extension
|
---|
| 190 | my $assocfilemeta = "[assocfilepath]";
|
---|
| 191 | if ($section ne $doc_obj->get_top_section()) {
|
---|
| 192 | $assocfilemeta = "[parent(Top):assocfilepath]";
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[16901] | 195 | # The images that will get generated may contain percent signs in their src filenames
|
---|
[16771] | 196 | # Encode those percent signs themselves so that urls to the imgs refer to them correctly
|
---|
[18404] | 197 | my $url_to_filehead = &unicode::filename_to_url($filehead);
|
---|
| 198 | my $url_to_filename_no_path = &unicode::filename_to_url($filename_no_path);
|
---|
[16901] | 199 |
|
---|
[15866] | 200 | # Convert the image to a new type (if required).
|
---|
| 201 | my $converttotype = $self->{'converttotype'};
|
---|
| 202 | my $type = "unknown";
|
---|
| 203 |
|
---|
| 204 | if ($converttotype ne "" && $filename_full_path !~ m/$converttotype$/) {
|
---|
[23335] | 205 | # # $doc_obj->add_utf8_metadata($section, "Image", $utf8_filename_meta);
|
---|
[16771] | 206 |
|
---|
[19540] | 207 | my ($result, $converted_filename_full_path)
|
---|
[16825] | 208 | = $self->convert($filename_full_path, $converttotype, "", "CONVERTTYPE");
|
---|
[15866] | 209 |
|
---|
| 210 | $type = $converttotype;
|
---|
[19540] | 211 | $filename_full_path = $converted_filename_full_path;
|
---|
[15866] | 212 | $filename_no_path = "$filehead.$type";
|
---|
[16771] | 213 | $url_to_filename_no_path = "$url_to_filehead.$type";
|
---|
[21344] | 214 | if ($self->{'store_file_paths'}) {
|
---|
| 215 | $self->{'orig_file'} = $converted_filename_full_path;
|
---|
| 216 | }
|
---|
[15866] | 217 | }
|
---|
| 218 |
|
---|
| 219 | # add Image metadata
|
---|
[16771] | 220 | $doc_obj->add_utf8_metadata($section, "Image", $url_to_filename_no_path); # url to generated image
|
---|
[15866] | 221 |
|
---|
[16382] | 222 | # here we overwrite the original with the potentially converted one
|
---|
[23335] | 223 | # $doc_obj->set_utf8_metadata_element($section, "Source", &unicode::url_decode($filename_no_path)); # displayname of generated image
|
---|
| 224 | # $doc_obj->set_utf8_metadata_element($section, "SourceFile", $url_to_filename_no_path); # displayname of generated image
|
---|
[15866] | 225 |
|
---|
[23335] | 226 | # $self->set_Source_metadata($doc_obj,$url_to_filename_no_path,undef);
|
---|
| 227 |
|
---|
[23352] | 228 | my $raw_filename_full_path = &unicode::url_decode($filename_encoded_full_path);
|
---|
| 229 | $self->set_Source_metadata($doc_obj,$raw_filename_full_path,
|
---|
| 230 | $filename_encoding);
|
---|
[23335] | 231 |
|
---|
| 232 |
|
---|
[15866] | 233 | # use identify to get info about the (possibly converted) image
|
---|
| 234 | my ($image_type, $image_width, $image_height, $image_size)
|
---|
| 235 | = &identify($filename_full_path, $outhandle, $verbosity);
|
---|
| 236 |
|
---|
| 237 | if ($image_type ne " ") {
|
---|
[17059] | 238 | $type = $self->correct_mime_type($image_type);
|
---|
[15866] | 239 | }
|
---|
| 240 |
|
---|
| 241 | #overwrite the ones added in BasePlugin
|
---|
| 242 | $doc_obj->set_metadata_element ($section, "FileFormat", $type);
|
---|
| 243 | $doc_obj->set_metadata_element ($section, "FileSize", $image_size);
|
---|
| 244 |
|
---|
| 245 | $doc_obj->add_metadata ($section, "ImageType", $image_type);
|
---|
| 246 | $doc_obj->add_metadata ($section, "ImageWidth", $image_width);
|
---|
| 247 | $doc_obj->add_metadata ($section, "ImageHeight", $image_height);
|
---|
| 248 | $doc_obj->add_metadata ($section, "ImageSize", $image_size);
|
---|
| 249 |
|
---|
[19054] | 250 | if ((defined $self->{'MaxImageWidth'})
|
---|
| 251 | && ($image_width > $self->{'MaxImageWidth'})) {
|
---|
| 252 | $self->{'MaxImageWidth'} = $image_width;
|
---|
| 253 | }
|
---|
| 254 | if ((defined $self->{'MaxImageHeight'})
|
---|
| 255 | && ($image_height > $self->{'MaxImageHeight'})) {
|
---|
| 256 | $self->{'MaxImageHeight'} = $image_height;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[22663] | 259 | $doc_obj->add_metadata ($section, "srclink_file", $url_to_filename_no_path);
|
---|
[22658] | 260 | $doc_obj->add_metadata ($section, "srcicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[srclink_file]\" width=\"[ImageWidth]\" height=\"[ImageHeight]\">");
|
---|
[15866] | 261 |
|
---|
| 262 | # Add the image as an associated file
|
---|
| 263 | $doc_obj->associate_file($filename_full_path, $filename_no_path, "image/$type", $section);
|
---|
| 264 |
|
---|
| 265 | if ($self->{'create_thumbnail'} eq "true") {
|
---|
[16771] | 266 | $self->create_thumbnail($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead);
|
---|
[15866] | 267 | }
|
---|
| 268 | if ($self->{'create_screenview'} eq "true") {
|
---|
[18466] | 269 | $self->create_screenview($filename_full_path, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height);
|
---|
[15866] | 270 | }
|
---|
[21722] | 271 |
|
---|
| 272 | return 1;
|
---|
[15866] | 273 | }
|
---|
| 274 |
|
---|
| 275 | sub create_thumbnail {
|
---|
| 276 | my $self = shift(@_);
|
---|
[16771] | 277 | my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead) = @_;
|
---|
| 278 | $url_to_filehead = $filehead unless defined $url_to_filehead;
|
---|
[15866] | 279 |
|
---|
| 280 | my $thumbnailsize = $self->{'thumbnailsize'};
|
---|
[17059] | 281 | my $thumbnailtype = $self->correct_mime_type($self->{'thumbnailtype'});
|
---|
[18466] | 282 |
|
---|
[15866] | 283 | # Generate the thumbnail with convert
|
---|
[16825] | 284 | my ($result,$thumbnailfile)
|
---|
| 285 | = $self->convert($original_file, $thumbnailtype, "-geometry $thumbnailsize" . "x$thumbnailsize", "THUMB");
|
---|
[15866] | 286 |
|
---|
| 287 | # Add the thumbnail as an associated file ...
|
---|
| 288 | if (-e "$thumbnailfile") {
|
---|
| 289 | $doc_obj->associate_file("$thumbnailfile", $filehead."_thumb.$thumbnailtype",
|
---|
[16771] | 290 | "image/$thumbnailtype",$section); # name of generated image
|
---|
[15866] | 291 | $doc_obj->add_metadata ($section, "ThumbType", $thumbnailtype);
|
---|
[16771] | 292 | $doc_obj->add_utf8_metadata ($section, "Thumb", $url_to_filehead."_thumb.$thumbnailtype"); # url to generated image
|
---|
[15866] | 293 |
|
---|
| 294 | $doc_obj->add_metadata ($section, "thumbicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Thumb]\" width=[ThumbWidth] height=[ThumbHeight]>");
|
---|
| 295 |
|
---|
| 296 |
|
---|
| 297 | # Extract Thumbnail metadata from convert output
|
---|
| 298 | if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
|
---|
| 299 | $doc_obj->add_metadata ($section, "ThumbWidth", $1);
|
---|
| 300 | $doc_obj->add_metadata ($section, "ThumbHeight", $2);
|
---|
| 301 | }
|
---|
[21344] | 302 | if ($self->{'store_file_paths'}) {
|
---|
| 303 | $self->{'thumb_file'} = $thumbnailfile;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[15866] | 306 | } else {
|
---|
| 307 | my $outhandle = $self->{'outhandle'};
|
---|
| 308 | print $outhandle "Couldn't find thumbnail $thumbnailfile\n";
|
---|
| 309 |
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | sub create_screenview {
|
---|
| 314 |
|
---|
| 315 | my $self = shift(@_);
|
---|
[18466] | 316 | my ($original_file, $filehead, $doc_obj, $section, $assocfilemeta, $url_to_filehead, $image_width, $image_height) = @_;
|
---|
[16771] | 317 | $url_to_filehead = $filehead unless defined $url_to_filehead;
|
---|
[15866] | 318 |
|
---|
| 319 | my $screenviewsize = $self->{'screenviewsize'};
|
---|
[17059] | 320 | my $screenviewtype = $self->correct_mime_type($self->{'screenviewtype'});
|
---|
[18466] | 321 |
|
---|
| 322 | # Scale the image, unless the original image is smaller than the screenview size and -noscaleup is set
|
---|
| 323 | my $scale_option = "-geometry $screenviewsize" . "x$screenviewsize";
|
---|
| 324 | if ($self->{'noscaleup'} && $image_width < $screenviewsize && $image_height < $screenviewsize)
|
---|
| 325 | {
|
---|
| 326 | $scale_option = "";
|
---|
| 327 | }
|
---|
[15866] | 328 |
|
---|
| 329 | # make the screenview image
|
---|
[16825] | 330 | my ($result,$screenviewfilename)
|
---|
[18466] | 331 | = $self->convert($original_file, $screenviewtype, $scale_option, "SCREEN");
|
---|
[15866] | 332 |
|
---|
| 333 | #add the screenview as an associated file ...
|
---|
| 334 | if (-e "$screenviewfilename") {
|
---|
[16771] | 335 | $doc_obj->associate_file("$screenviewfilename", $filehead."_screen.$screenviewtype", "image/$screenviewtype",$section); # name of generated image
|
---|
[15866] | 336 | $doc_obj->add_metadata ($section, "ScreenType", $screenviewtype);
|
---|
[16771] | 337 | $doc_obj->add_utf8_metadata ($section, "Screen", $url_to_filehead."_screen.$screenviewtype"); # url to generated image
|
---|
[15866] | 338 |
|
---|
| 339 | $doc_obj->add_metadata ($section, "screenicon", "<img src=\"_httpprefix_/collect/[collection]/index/assoc/$assocfilemeta/[Screen]\" width=[ScreenWidth] height=[ScreenHeight]>");
|
---|
| 340 |
|
---|
| 341 | # get screenview dimensions, size and type
|
---|
| 342 | if ($result =~ m/[0-9]+x[0-9]+=>([0-9]+)x([0-9]+)/) {
|
---|
| 343 | $doc_obj->add_metadata ($section, "ScreenWidth", $1);
|
---|
| 344 | $doc_obj->add_metadata ($section, "ScreenHeight", $2);
|
---|
| 345 | } elsif ($result =~ m/([0-9]+)x([0-9]+)/) {
|
---|
| 346 | #if the image hasn't changed size, the previous regex doesn't match
|
---|
| 347 | $doc_obj->add_metadata ($section, "ScreenWidth", $1);
|
---|
| 348 | $doc_obj->add_metadata ($section, "ScreenHeight", $2);
|
---|
| 349 | }
|
---|
[21344] | 350 |
|
---|
| 351 | if ($self->{'store_file_paths'}) {
|
---|
| 352 | $self->{'screen_file'} = $screenviewfilename;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[15866] | 355 | } else {
|
---|
| 356 | my $outhandle = $self->{'outhandle'};
|
---|
| 357 | print $outhandle "Couldn't find screenview file $screenviewfilename\n";
|
---|
| 358 |
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 |
|
---|
| 364 |
|
---|
| 365 | sub convert {
|
---|
| 366 | my $self = shift(@_);
|
---|
| 367 | my $source_file_path = shift(@_);
|
---|
| 368 | my $target_file_type = shift(@_);
|
---|
[16825] | 369 | my $convert_options = shift(@_) || "";
|
---|
| 370 | my $convert_id = shift(@_) || "";
|
---|
[20540] | 371 | my $cache_mode = shift(@_) || "";
|
---|
[15866] | 372 |
|
---|
| 373 | my $outhandle = $self->{'outhandle'};
|
---|
| 374 | my $verbosity = $self->{'verbosity'};
|
---|
| 375 |
|
---|
[16825] | 376 | my $source_file_no_path = &File::Basename::basename($source_file_path);
|
---|
| 377 |
|
---|
[15866] | 378 | # Determine the full name and path of the output file
|
---|
[16825] | 379 | my $target_file_path;
|
---|
[19054] | 380 | if ($self->{'enable_cache'}) {
|
---|
[16825] | 381 | my $cached_image_dir = $self->{'cached_dir'};
|
---|
| 382 | my $image_root = $self->{'cached_file_root'};
|
---|
| 383 | $image_root .= "_$convert_id" if ($convert_id ne "");
|
---|
| 384 | my $image_file = "$image_root.$target_file_type";
|
---|
| 385 | $target_file_path = &util::filename_cat($cached_image_dir,$image_file);
|
---|
| 386 | }
|
---|
| 387 | else {
|
---|
| 388 | $target_file_path = &util::get_tmp_filename($target_file_type);
|
---|
| 389 | push(@{$self->{'tmp_file_paths'}}, $target_file_path);
|
---|
[15866] | 390 |
|
---|
[16825] | 391 | # Output filename used to be parsed from result line:
|
---|
| 392 | # my ($ofilename) = ($result =~ m/=>(.*\.$target_file_type)/);
|
---|
| 393 | # by the function that called 'convert'
|
---|
| 394 | # but this is no longer needed, as output filename is now
|
---|
| 395 | # explicitly passed back
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[15866] | 398 | # Generate and run the convert command
|
---|
| 399 | my $convert_command = "convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
|
---|
| 400 |
|
---|
[16825] | 401 | my $print_info = { 'message_prefix' => $convert_id,
|
---|
| 402 | 'message' => "Converting image $source_file_no_path to: $convert_id $target_file_type" };
|
---|
[20540] | 403 | $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
|
---|
| 404 |
|
---|
[16825] | 405 | my ($regenerated,$result,$had_error)
|
---|
[22450] | 406 | = $self->autorun_general_cmd($convert_command,$source_file_path,$target_file_path,$print_info);
|
---|
[16825] | 407 |
|
---|
| 408 | return ($result,$target_file_path);
|
---|
[15866] | 409 | }
|
---|
| 410 |
|
---|
[20540] | 411 | sub convert_without_result {
|
---|
| 412 | my $self = shift(@_);
|
---|
[15866] | 413 |
|
---|
[20540] | 414 | my $source_file_path = shift(@_);
|
---|
| 415 | my $target_file_type = shift(@_);
|
---|
| 416 | my $convert_options = shift(@_) || "";
|
---|
| 417 | my $convert_id = shift(@_) || "";
|
---|
| 418 |
|
---|
| 419 | return $self->convert($source_file_path,$target_file_type,
|
---|
| 420 | $convert_options,$convert_id,"without_result");
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 |
|
---|
[15866] | 424 | # Discover the characteristics of an image file with the ImageMagick
|
---|
| 425 | # "identify" command.
|
---|
| 426 |
|
---|
| 427 | sub identify {
|
---|
| 428 | my ($image, $outhandle, $verbosity) = @_;
|
---|
| 429 |
|
---|
| 430 | # Use the ImageMagick "identify" command to get the file specs
|
---|
| 431 | my $command = "identify \"$image\" 2>&1";
|
---|
| 432 | print $outhandle "$command\n" if ($verbosity > 2);
|
---|
| 433 | my $result = '';
|
---|
| 434 | $result = `$command`;
|
---|
| 435 | print $outhandle "$result\n" if ($verbosity > 3);
|
---|
| 436 |
|
---|
| 437 | # Read the type, width, and height
|
---|
| 438 | my $type = 'unknown';
|
---|
| 439 | my $width = 'unknown';
|
---|
| 440 | my $height = 'unknown';
|
---|
| 441 |
|
---|
| 442 | my $image_safe = quotemeta $image;
|
---|
| 443 | if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
|
---|
| 444 | $type = $1;
|
---|
| 445 | $width = $2;
|
---|
| 446 | $height = $3;
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | # Read the size
|
---|
| 450 | my $size = "unknown";
|
---|
| 451 | if ($result =~ m/^.* ([0-9]+)b/) {
|
---|
| 452 | $size = $1;
|
---|
| 453 | }
|
---|
| 454 | elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?kb?/) {
|
---|
| 455 | $size = 1024 * $1;
|
---|
| 456 | if (defined($2)) {
|
---|
| 457 | $size = $size + (1024 * $2);
|
---|
| 458 | # Truncate size (it isn't going to be very accurate anyway)
|
---|
| 459 | $size = int($size);
|
---|
| 460 | }
|
---|
| 461 | }
|
---|
[21866] | 462 | elsif ($result =~ m/^.* ([0-9]+)(\.([0-9]+))?mb?/) {
|
---|
| 463 | $size = 1024 * 1024 * $1;
|
---|
| 464 | if (defined($2)) {
|
---|
| 465 | $size = $size + (1024 * 1024 * $2);
|
---|
| 466 | # Truncate size (it isn't going to be very accurate anyway)
|
---|
| 467 | $size = int($size);
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
[15866] | 470 | elsif ($result =~ m/^.* (([0-9]+)(\.([0-9]+))?e\+([0-9]+))(kb|b)?/) {
|
---|
| 471 | # Deals with file sizes on Linux of type "3.4e+02kb" where e+02 is 1*10^2.
|
---|
| 472 | # 3.4e+02 therefore evaluates to 3.4 x 1 x 10^2 = 340kb.
|
---|
| 473 | # Programming languages including Perl know how that 3.4e+02 is a number,
|
---|
| 474 | # so we don't need to do any calculations.
|
---|
| 475 | $size = $1*1; # turn the string into a number by multiplying it by 1
|
---|
| 476 | #if we did $size = $1; $size would be merely the string "3.4e+02"
|
---|
| 477 | $size = int($size); # truncate size
|
---|
| 478 | }
|
---|
| 479 | print $outhandle "file: $image:\t $type, $width, $height, $size\n"
|
---|
| 480 | if ($verbosity > 2);
|
---|
| 481 |
|
---|
| 482 | # Return the specs
|
---|
| 483 | return ($type, $width, $height, $size);
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | sub clean_up_temporary_files {
|
---|
| 487 | my $self = shift(@_);
|
---|
| 488 |
|
---|
| 489 | foreach my $tmp_file_path (@{$self->{'tmp_file_paths'}}) {
|
---|
| 490 | if (-e $tmp_file_path) {
|
---|
| 491 | &util::rm($tmp_file_path);
|
---|
| 492 | }
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | }
|
---|
| 496 |
|
---|
[17059] | 497 | # image/jpg is not a valid mime-type, it ought to be image/jpeg.
|
---|
| 498 | # Sometimes JPEG is passed in also, want to keep things lowercase just in case.
|
---|
| 499 | sub correct_mime_type {
|
---|
| 500 | my $self = shift(@_);
|
---|
| 501 | my ($file_extension) = @_;
|
---|
| 502 |
|
---|
| 503 | $file_extension = lc($file_extension);
|
---|
| 504 | $file_extension =~ s/jpg/jpeg/s;
|
---|
| 505 |
|
---|
| 506 | return $file_extension;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
[15866] | 509 | 1;
|
---|