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