| 1 | |||
| 2 | ##-------------------------------------------------------------------------- | ||
| 3 | ## | ||
| 4 | ## Copyright (c) 2002 Gerald Richter / ecos gmbh www.ecos.de | ||
| 5 | ## parts (c) Lincoln Stein & Doug MacEachern | ||
| 6 | ## | ||
| 7 | ## You may distribute under the terms of either the GNU General Public | ||
| 8 | ## License or the Artistic License, as specified in the Perl README file. | ||
| 9 | ## | ||
| 10 | ## THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
| 11 | ## WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
| 12 | ## MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
| 13 | ## | ||
| 14 | ## $Id: ImageMagick.pm,v 1.13 2002/09/20 08:07:51 richter Exp $ | ||
| 15 | ## | ||
| 16 | ##-------------------------------------------------------------------------- | ||
| 17 | |||
| 18 | |||
| 19 | package Apache::ImageMagick ; | ||
| 20 | |||
| 21 | |||
| 22 | use strict; | ||
| 23 | use vars qw{$VERSION %scriptmtime %scriptsub $packnum $debug} ; | ||
| 24 | |||
| 25 | use Image::Magick (); | ||
| 26 | use File::Basename qw(fileparse); | ||
| 27 | use DirHandle (); | ||
| 28 | use Digest::MD5 ; | ||
| 29 | use Text::ParseWords ; | ||
| 30 | |||
| 31 | $VERSION = '2.0b7' ; | ||
| 32 | |||
| 33 | # define constants here instead of use Apache::Constants qw(:common);to remove dependency | ||
| 34 | use constant OK => 0 ; | ||
| 35 | use constant NOT_FOUND => 404 ; | ||
| 36 | use constant SERVER_ERROR => 500 ; | ||
| 37 | use constant DECLINED => -1 ; | ||
| 38 | |||
| 39 | $packnum = 1 ; | ||
| 40 | $debug = 0 ; | ||
| 41 | |||
| 42 | |||
| 43 | my %LegalArguments = map { $_ => 1 } | ||
| 44 | qw (antialias adjoin authenticate | ||
| 45 | background blue-primary bordercolor | ||
| 46 | cache-threshold colormap colorspace compression | ||
| 47 | debug delay density depth dispose dither display | ||
| 48 | endian | ||
| 49 | filename file fill font fuzz | ||
| 50 | gravity green-primary | ||
| 51 | index iterations interlace | ||
| 52 | loop | ||
| 53 | magick mattecolor matte monochrome | ||
| 54 | page pen pixel pointsize preview | ||
| 55 | quality | ||
| 56 | red_primary render | ||
| 57 | sampling-factor scene subimage subrange server size stroke | ||
| 58 | tile texture type | ||
| 59 | unit | ||
| 60 | verbose | ||
| 61 | white-primary | ||
| 62 | ); | ||
| 63 | |||
| 64 | my %LegalFilters = map { $_ => 1 } | ||
| 65 | qw(Annotate AddNoise Blur Border Charcoal Chop | ||
| 66 | Contrast Crop ColorFloodfill Colorize Comment CycleColormap | ||
| 67 | Despeckle Draw Edge Emboss Enhance Equalize Flip Flop | ||
| 68 | Frame Gamma Implode Label Layer Magnify Map Minify | ||
| 69 | MatteFloodfill MedianFilter Modulate MotionBlur | ||
| 70 | Modulate Negate Normalize OilPaint Opaque Quantize | ||
| 71 | Raise ReduceNoise Roll Rotate Resize Sample Scale Segment Shade | ||
| 72 | Sharpen Shave Shear Signature Stereo Solarize Spread Swirl Texture Transparent | ||
| 73 | Threshold Trim Wave UnsharpMask Zoom); | ||
| 74 | |||
| 75 | my %QueryFilters = map { $_ => 1 } | ||
| 76 | qw(QueryFormat QueryFont QueryColor) ; | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | my %Attributes = map { $_ => 1 } | ||
| 81 | qw( | ||
| 82 | adjoin | ||
| 83 | antialias | ||
| 84 | background | ||
| 85 | blue-primary | ||
| 86 | bordercolor | ||
| 87 | cache-threshold | ||
| 88 | colormap | ||
| 89 | colorspace | ||
| 90 | compression | ||
| 91 | delay | ||
| 92 | density | ||
| 93 | dispose | ||
| 94 | dither | ||
| 95 | display | ||
| 96 | file | ||
| 97 | filename | ||
| 98 | font | ||
| 99 | fuzz | ||
| 100 | green-primary | ||
| 101 | index | ||
| 102 | interlace | ||
| 103 | iterations | ||
| 104 | loop | ||
| 105 | magick | ||
| 106 | matte | ||
| 107 | mattecolor | ||
| 108 | monochrome | ||
| 109 | page | ||
| 110 | pixel | ||
| 111 | pointsize | ||
| 112 | preview | ||
| 113 | quality | ||
| 114 | red-primary | ||
| 115 | rendering-intent | ||
| 116 | scene | ||
| 117 | subimage | ||
| 118 | subrange | ||
| 119 | server | ||
| 120 | size | ||
| 121 | tile | ||
| 122 | texture | ||
| 123 | type | ||
| 124 | units | ||
| 125 | verbose | ||
| 126 | white-point | ||
| 127 | Image | ||
| 128 | base-columns | ||
| 129 | base-filename | ||
| 130 | base-rows | ||
| 131 | class | ||
| 132 | colors | ||
| 133 | comment | ||
| 134 | columns | ||
| 135 | depth | ||
| 136 | directory | ||
| 137 | error | ||
| 138 | filesize | ||
| 139 | format | ||
| 140 | gamma | ||
| 141 | geometry | ||
| 142 | height | ||
| 143 | label | ||
| 144 | maximum-error | ||
| 145 | mean-error | ||
| 146 | montage | ||
| 147 | rows | ||
| 148 | signature | ||
| 149 | taint | ||
| 150 | width | ||
| 151 | x-resolution | ||
| 152 | y-resolution) ; | ||
| 153 | |||
| 154 | # --------------------------------------------------------------------------- | ||
| 155 | # | ||
| 156 | # find a image with the same base name, but of different type | ||
| 157 | # | ||
| 158 | |||
| 159 | sub find_image | ||
| 160 | { | ||
| 161 | my ($r, $directory, $base) = @_; | ||
| 162 | my $dh = DirHandle->new($directory) or return; | ||
| 163 | |||
| 164 | my $source; | ||
| 165 | for my $entry ($dh->read) | ||
| 166 | { | ||
| 167 | my $candidate = fileparse($entry, '\.\w+'); | ||
| 168 | if ($base eq $candidate || $base eq $entry) | ||
| 169 | { | ||
| 170 | # determine whether this is an image file | ||
| 171 | $source = join '', $directory, $entry; | ||
| 172 | my $subr = $r->lookup_file($source); | ||
| 173 | last if $subr->content_type =~ m:^image/:; | ||
| 174 | $source = ""; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | $dh->close; | ||
| 178 | return $source; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | # --------------------------------------------------------------------------- | ||
| 183 | # | ||
| 184 | # compile, cache and execute external script | ||
| 185 | # | ||
| 186 | |||
| 187 | |||
| 188 | sub execute_script | ||
| 189 | |||
| 190 | { | ||
| 191 | my ($script, $r, $image, $filter, $args) = @_ ; | ||
| 192 | |||
| 193 | return 1 if (!-r $script) ; | ||
| 194 | |||
| 195 | my $package ; | ||
| 196 | my $sub ; | ||
| 197 | |||
| 198 | if ($scriptmtime{$script} != -M _) | ||
| 199 | { # load and compile script | ||
| 200 | $r -> log_error ("Apache::ImageMagick: Compile $script") if ($debug) ; | ||
| 201 | my $content ; | ||
| 202 | { | ||
| 203 | local $/ = undef ; | ||
| 204 | open FH, $script or do { $r -> log_error ("Apache::ImageMagick: Cannot open script $script ($!)") ; return undef ; } ; | ||
| 205 | $content = <FH> ; | ||
| 206 | close FH ; | ||
| 207 | } | ||
| 208 | $package = "Apache::ImageMagick::_$packnum" ; | ||
| 209 | $packnum++ ; | ||
| 210 | my $code = join("\n", "package $package ;", | ||
| 211 | '\sub {', "#line 1 \"$script\"", | ||
| 212 | $content, ';} ;') ; | ||
| 213 | $sub = eval $code ; | ||
| 214 | if ($@) | ||
| 215 | { | ||
| 216 | $r -> log_error ("Apache::ImageMagick: $@") ; | ||
| 217 | return undef ; | ||
| 218 | } | ||
| 219 | |||
| 220 | $scriptmtime{$script} = -M _ ; | ||
| 221 | $scriptsub{$script} = $sub ; | ||
| 222 | } | ||
| 223 | else | ||
| 224 | { | ||
| 225 | $sub = $scriptsub{$script} ; | ||
| 226 | } | ||
| 227 | |||
| 228 | $r -> log_error ("Apache::ImageMagick: Call $script") if ($debug) ; | ||
| 229 | my $rc = eval { &{$$sub}($r, $image, $filter, $args) ; } ; | ||
| 230 | if ($@) | ||
| 231 | { | ||
| 232 | $r -> log_error ("Apache::ImageMagick: $@") ; | ||
| 233 | return undef ; | ||
| 234 | } | ||
| 235 | if (!$rc) | ||
| 236 | { | ||
| 237 | $r -> log_error ("Apache::ImageMagick: Script $script doesn't returned true") ; | ||
| 238 | return undef ; | ||
| 239 | } | ||
| 240 | return $rc ; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | |||
| 245 | # --------------------------------------------------------------------------- | ||
| 246 | # | ||
| 247 | # Fixup handler | ||
| 248 | # | ||
| 249 | |||
| 250 | |||
| 251 | sub handler | ||
| 252 | { | ||
| 253 | my $r = shift; | ||
| 254 | my $useiofile = shift ; | ||
| 255 | |||
| 256 | my $args = $r -> args ; | ||
| 257 | my $path_info = $r -> path_info ; | ||
| 258 | |||
| 259 | # If the file exists and there are no transformation arguments | ||
| 260 | # just decline the transaction. It will be handled as usual. | ||
| 261 | return OK if ((!$args && !$path_info) || -d $r -> finfo) ; | ||
| 262 | |||
| 263 | # calculate name of cache file | ||
| 264 | my $file = $r->filename; | ||
| 265 | my $uri = $r -> uri ; | ||
| 266 | my $md5 = Digest::MD5::md5_hex($uri . $args) ; | ||
| 267 | $file =~ /.*\.(.*?)$/; | ||
| 268 | my $ext = $1 ; | ||
| 269 | my $cachefn = "$md5.$ext" ; | ||
| 270 | my $cachedir = $r -> dir_config ("AIMCacheDir") || '.' ; | ||
| 271 | my $cachepath = "$cachedir/$cachefn" ; | ||
| 272 | my $checkmtime = $r -> dir_config ("AIMCheckMTime") || 0 ; | ||
| 273 | $checkmtime = 0 if (lc($checkmtime) eq 'off') ; | ||
| 274 | |||
| 275 | if (!$checkmtime && -r $cachepath) | ||
| 276 | { # let apache do the rest if image already exists | ||
| 277 | $r -> filename ($cachepath) ; | ||
| 278 | $r -> path_info ('') ; | ||
| 279 | return OK ; | ||
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | my $srcdir = $r -> dir_config ("AIMSourceDir") ; | ||
| 284 | my $stripprefix = $r -> dir_config ("AIMStripPrefix") ; | ||
| 285 | my $scriptdir = $r -> dir_config ("AIMScriptDir") ; | ||
| 286 | my $scriptdef = $r -> dir_config ("AIMScriptDefault") ; | ||
| 287 | my $scriptext = $r -> dir_config ("AIMScriptExt") || 'pl' ; | ||
| 288 | my $cache = $r -> dir_config ("AIMCache") || 1 ; | ||
| 289 | my $param = $r -> dir_config ("AIMParameter") ; | ||
| 290 | my $disablesearch = $r -> dir_config ("AIMDisableSearch") || 0 ; | ||
| 291 | $debug = $r -> dir_config ("AIMDebug") || 0 ; | ||
| 292 | $cache = 0 if (lc($cache) eq 'off') ; | ||
| 293 | $debug = 0 if (lc($debug) eq 'off') ; | ||
| 294 | $disablesearch = 0 if (lc($disablesearch) eq 'off') ; | ||
| 295 | |||
| 296 | |||
| 297 | if ($debug) | ||
| 298 | { | ||
| 299 | $r -> log_error ("Apache::ImageMagick: AIMCacheDir = $cachedir") ; | ||
| 300 | $r -> log_error ("Apache::ImageMagick: AIMSourceDir = $srcdir") ; | ||
| 301 | $r -> log_error ("Apache::ImageMagick: AIMStripPrefix = $stripprefix") ; | ||
| 302 | $r -> log_error ("Apache::ImageMagick: AIMScriptDir = $scriptdir") ; | ||
| 303 | $r -> log_error ("Apache::ImageMagick: AIMScriptDefault = $scriptdef") ; | ||
| 304 | $r -> log_error ("Apache::ImageMagick: AIMScriptExt = $scriptext") ; | ||
| 305 | $r -> log_error ("Apache::ImageMagick: AIMCache = $cache") ; | ||
| 306 | $r -> log_error ("Apache::ImageMagick: AIMParameter = $param") ; | ||
| 307 | $r -> log_error ("Apache::ImageMagick: AIMCheckMTime = $checkmtime") ; | ||
| 308 | $r -> log_error ("Apache::ImageMagick: AIMDisableSearch = $disablesearch") ; | ||
| 309 | } | ||
| 310 | |||
| 311 | my $script ; | ||
| 312 | my $basefile ; | ||
| 313 | my $source ; | ||
| 314 | my ($base, $directory, $extension) = fileparse($file, '\.\w+'); | ||
| 315 | |||
| 316 | if ($stripprefix) | ||
| 317 | { | ||
| 318 | $file =~ /$stripprefix(.*?)^/ ; | ||
| 319 | $basefile = $1 ; | ||
| 320 | } | ||
| 321 | else | ||
| 322 | { | ||
| 323 | $basefile = $base . $extension ; | ||
| 324 | } | ||
| 325 | |||
| 326 | $file = "$srcdir/$basefile" if ($srcdir) ; | ||
| 327 | |||
| 328 | # Conversion arguments are kept in the query string, and the | ||
| 329 | # image filter operations are kept in the path info | ||
| 330 | my (%arguments) = $r->args; | ||
| 331 | my @filters = split '/', $r->path_info ; | ||
| 332 | |||
| 333 | if (-r $file || $arguments{-new}) | ||
| 334 | { # file exists or new, so it becomes the source | ||
| 335 | $source = $file; | ||
| 336 | } | ||
| 337 | elsif (!$disablesearch) | ||
| 338 | { # file doesn't exist, so we search for it | ||
| 339 | return DECLINED unless -r $directory; | ||
| 340 | $source = find_image($r, $directory, $base); | ||
| 341 | } | ||
| 342 | |||
| 343 | unless ($source) | ||
| 344 | { | ||
| 345 | $r->log_error("Apache::ImageMagick: Couldn't find a replacement for $file"); | ||
| 346 | return NOT_FOUND; | ||
| 347 | } | ||
| 348 | |||
| 349 | if ($checkmtime && -M $file > -M $cachepath && -r _) | ||
| 350 | { # let apache do the rest if image already exists and is not new then cache | ||
| 351 | $r -> filename ($cachepath) ; | ||
| 352 | $r -> path_info ('') ; | ||
| 353 | $r -> log_error ("Apache::ImageMagick: Use cached image") if ($debug) ; | ||
| 354 | return OK ; | ||
| 355 | } | ||
| 356 | |||
| 357 | |||
| 358 | if ($scriptdir) | ||
| 359 | { | ||
| 360 | if ($scriptdir eq '.') | ||
| 361 | { | ||
| 362 | $script = "$file.$scriptext" ; | ||
| 363 | } | ||
| 364 | else | ||
| 365 | { | ||
| 366 | $script = "$scriptdir/$basefile.$scriptext" ; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | |||
| 372 | $r -> log_error ("Apache::ImageMagick: Source: $source Script: $script Cachefile: $cachepath") if ($debug) ; | ||
| 373 | |||
| 374 | #$r->send_http_header; | ||
| 375 | #return OK if $r->header_only; | ||
| 376 | |||
| 377 | |||
| 378 | |||
| 379 | # Read the image | ||
| 380 | my $q = Image::Magick->new; | ||
| 381 | my $err ; | ||
| 382 | $err = $q->Read($source) if (!$arguments{-new}) ; | ||
| 383 | my $errfilter ; | ||
| 384 | |||
| 385 | execute_script ($script, $r, $q, \@filters, \%arguments) or return SERVER_ERROR if ($script) ; | ||
| 386 | execute_script ($scriptdef, $r, $q, \@filters, \%arguments) or return SERVER_ERROR if ($scriptdef) ; | ||
| 387 | |||
| 388 | if ($param) | ||
| 389 | { | ||
| 390 | my @arglist = quotewords ('\s+', 0, $param) ; | ||
| 391 | foreach (@arglist) | ||
| 392 | { | ||
| 393 | /^(\!)?(.*?)\s*=\s*(.*?)$/ ; | ||
| 394 | if ($1) | ||
| 395 | { | ||
| 396 | $arguments{$2} = $3 ; | ||
| 397 | } | ||
| 398 | else | ||
| 399 | { | ||
| 400 | $arguments{$2} = $3 if (!defined ($arguments{$2})) ; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | # Run the filters | ||
| 406 | foreach my $f (@filters) | ||
| 407 | { | ||
| 408 | my $filter = ucfirst $f; | ||
| 409 | next unless $LegalFilters{$filter}; | ||
| 410 | my %args = map { | ||
| 411 | if (/^-/) | ||
| 412 | { | ||
| 413 | () | ||
| 414 | } | ||
| 415 | elsif (!(/:/)) | ||
| 416 | { | ||
| 417 | ($_ => $arguments{$_}) | ||
| 418 | } | ||
| 419 | else | ||
| 420 | { | ||
| 421 | if (/^$filter:(.*?)$/i) | ||
| 422 | { | ||
| 423 | ($1 => $arguments{$_}) | ||
| 424 | } | ||
| 425 | else | ||
| 426 | { | ||
| 427 | () | ||
| 428 | } | ||
| 429 | } | ||
| 430 | } keys %arguments ; | ||
| 431 | |||
| 432 | if ($debug) | ||
| 433 | { | ||
| 434 | $r -> log_error ("Apache::ImageMagick: Filter $filter (", join (',', map { "$_=>$args{$_}" } keys %args), ")") ; | ||
| 435 | } | ||
| 436 | my $ferr = $q->$filter(%args); | ||
| 437 | if ($ferr) | ||
| 438 | { | ||
| 439 | my @args = %args ; | ||
| 440 | $err ||= $ferr ; | ||
| 441 | $errfilter ||= "$filter (@args)" ; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | |||
| 445 | my($tmpnam, $fh) ; | ||
| 446 | if (!$useiofile) | ||
| 447 | { | ||
| 448 | require Apache::File ; | ||
| 449 | if ($cache) | ||
| 450 | { # create and open cachefile | ||
| 451 | $tmpnam = $cachepath ; | ||
| 452 | $fh = Apache::File->new(">$tmpnam"); | ||
| 453 | } | ||
| 454 | else | ||
| 455 | { | ||
| 456 | # Create a temporary file name to use for conversion | ||
| 457 | # The file is automaticly deleted after the request | ||
| 458 | ($tmpnam, $fh) = Apache::File->tmpfile; | ||
| 459 | $tmpnam ||= 'temporary file' if (!$fh) ; | ||
| 460 | } | ||
| 461 | } | ||
| 462 | else | ||
| 463 | { | ||
| 464 | require IO::File ; | ||
| 465 | if ($cache) | ||
| 466 | { # create and open cachefile | ||
| 467 | $tmpnam = $cachepath ; | ||
| 468 | $fh = IO::File->new(">$tmpnam"); | ||
| 469 | } | ||
| 470 | else | ||
| 471 | { | ||
| 472 | # Create a temporary file name to use for conversion | ||
| 473 | # The file is automaticly deleted after the request | ||
| 474 | ($tmpnam, $fh) = IO::File->new_tmpfile; | ||
| 475 | $tmpnam ||= 'temporary file' if (!$fh) ; | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | unless ($fh) | ||
| 480 | { | ||
| 481 | $r->log_error("Apache::ImageMagick: Couldn't open file $tmpnam for writing ($!)"); | ||
| 482 | return SERVER_ERROR; | ||
| 483 | } | ||
| 484 | |||
| 485 | # Remove invalid arguments before the conversion | ||
| 486 | foreach (keys %arguments) | ||
| 487 | { | ||
| 488 | $arguments{$1} = $arguments{$_} if (/^Set:(.*?)$/i) ; | ||
| 489 | delete $arguments{$_} unless $LegalArguments{$_}; | ||
| 490 | } | ||
| 491 | |||
| 492 | if ($debug) | ||
| 493 | { | ||
| 494 | $r -> log_error ("Apache::ImageMagick: Write $tmpnam (", join (',', map { "$_=>$arguments{$_}" } keys %arguments), ")") ; | ||
| 495 | } | ||
| 496 | |||
| 497 | # Write out the modified image | ||
| 498 | my $werr ; | ||
| 499 | open(OLDOUT, ">&STDOUT"); | ||
| 500 | open(STDOUT, ">&=" . fileno($fh)); | ||
| 501 | $werr = $q->Write('filename' => "\U$ext\L:-", %arguments); | ||
| 502 | $err ||= $werr ; | ||
| 503 | close $fh; | ||
| 504 | open(STDOUT, ">&OLDOUT"); | ||
| 505 | if ($err) | ||
| 506 | { | ||
| 507 | $r->log_error("Apache::ImageMagick: $errfilter $err"); | ||
| 508 | if ($err !~ /Warning/) | ||
| 509 | { | ||
| 510 | unlink $tmpnam; | ||
| 511 | return SERVER_ERROR; | ||
| 512 | } | ||
| 513 | } | ||
| 514 | $r -> filename ($tmpnam) ; | ||
| 515 | $r -> path_info ('') ; | ||
| 516 | |||
| 517 | return OK; | ||
| 518 | } | ||
| 519 | |||
| 520 | |||
| 521 | 1; | ||
| 522 | __END__ |