~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to modules/imagemagick/classes/ImageMagickToolkit.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 * $RCSfile: ImageMagickToolkit.class,v $
4
4
 *
5
5
 * Gallery - a web based photo album viewer and editor
6
 
 * Copyright (C) 2000-2005 Bharat Mediratta
 
6
 * Copyright (C) 2000-2006 Bharat Mediratta
7
7
 *
8
8
 * This program is free software; you can redistribute it and/or modify
9
9
 * it under the terms of the GNU General Public License as published by
20
20
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
21
21
 */
22
22
/**
23
 
 * @version $Revision: 1.46 $ $Date: 2005/08/23 03:49:44 $
 
23
 * @version $Revision: 1.48 $ $Date: 2006/02/08 01:29:23 $
24
24
 * @package ImageMagick
25
25
 * @author Vallimar <vallimar@sexorcisto.net>
26
26
 * @author Bharat Mediratta <bharat@menalto.com>
29
29
/**
30
30
 * Load required classes
31
31
 */
32
 
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryToolkit.class');
33
 
GalleryCoreApi::relativeRequireOnce('modules/imagemagick/classes/ImageMagickToolkitHelper.class');
 
32
GalleryCoreApi::requireOnce('modules/core/classes/GalleryToolkit.class');
 
33
GalleryCoreApi::requireOnce('modules/imagemagick/classes/ImageMagickToolkitHelper.class');
34
34
 
35
35
/**
36
36
 * A ImageMagick version of GalleryToolkit
41
41
 * @subpackage Classes
42
42
 */
43
43
class ImageMagickToolkit extends GalleryToolkit {
44
 
    /*
45
 
     * ****************************************
46
 
     *                 Methods
47
 
     * ****************************************
48
 
     */
49
44
 
50
45
    /**
51
46
     * @see GalleryToolkit::getProperty()
55
50
        switch($propertyName) {
56
51
        case 'dimensions':
57
52
            list ($ret, $width, $height) = $this->_getImageDimensions($mimeType, $sourceFilename);
58
 
            if ($ret->isError()) {
 
53
            if ($ret) {
59
54
                return array($ret->wrap(__FILE__, __LINE__), null);
60
55
            }
61
56
            $results = array((int)$width, (int)$height);
63
58
 
64
59
        case 'page-count':
65
60
            list ($ret, $count) = $this->_getPageCount($sourceFilename, $mimeType);
66
 
            if ($ret->isError()) {
 
61
            if ($ret) {
67
62
                return array($ret->wrap(__FILE__, __LINE__), null);
68
63
            }
69
64
            $results = array($count);
71
66
 
72
67
        case 'colorspace':
73
68
            list ($ret, $colorspace) = $this->_getColorspace($sourceFilename);
74
 
            if ($ret->isError()) {
 
69
            if ($ret) {
75
70
                return array($ret->wrap(__FILE__, __LINE__), null);
76
71
            }
77
72
            $results = array($colorspace);
78
73
            break;
79
74
 
80
75
        default:
81
 
            return array(GalleryStatus::error(ERROR_UNIMPLEMENTED, __FILE__, __LINE__),
 
76
            return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED, __FILE__, __LINE__),
82
77
                         null);
83
78
        }
84
79
 
85
 
        return array(GalleryStatus::success(), $results);
 
80
        return array(null, $results);
86
81
    }
87
82
 
88
83
    /**
128
123
        if (!isset($width) &&
129
124
                ($queueIt || in_array($operationName, array('thumbnail', 'crop', 'composite')))) {
130
125
            list ($ret, $width, $height) = $this->_getImageDimensions($mimeType, $sourceFilename);
131
 
            if ($ret->isError()) {
 
126
            if ($ret) {
132
127
                return array($ret->wrap(__FILE__, __LINE__), null, null);
133
128
            }
134
129
        }
148
143
            /* Strip metadata to make derivative files smaller.. */
149
144
            list ($ret, $removeMetaDataSwitch) =
150
145
                GalleryCoreApi::getPluginParameter('module', 'imagemagick', 'removeMetaDataSwitch');
151
 
            if ($ret->isError()) {
 
146
            if ($ret) {
152
147
                return array($ret->wrap(__FILE__, __LINE__), null, null);
153
148
            }
154
149
            if (!empty($removeMetaDataSwitch)) {
212
207
        case 'composite':
213
208
            list ($ret, $cmd) =
214
209
                GalleryCoreApi::getPluginParameter('module', 'imagemagick', 'compositeCmd');
215
 
            if ($ret->isError()) {
 
210
            if ($ret) {
216
211
                return array($ret->wrap(__FILE__, __LINE__), null, null);
217
212
            }
218
213
            if (empty($cmd)) {
277
272
                break;
278
273
 
279
274
            default:
280
 
                return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
 
275
                return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
281
276
                                    "Unknown composite alignment type: $compositeAlignmentType"),
282
277
                             null, null);
283
278
            }
299
294
 
300
295
        case 'compress':
301
296
            $targetSize = $parameters[0];
302
 
            $platform = $gallery->getPlatform();
 
297
            $platform =& $gallery->getPlatform();
303
298
            $fileSize = $platform->filesize($sourceFilename) >> 10; /* Size in KB */
304
299
            if ($fileSize <= $targetSize) {
305
300
                break;
307
302
            /* Use module quality parameter as initial guess */
308
303
            list ($ret, $quality) =
309
304
                GalleryCoreApi::getPluginParameter('module', 'imagemagick', 'jpegQuality');
310
 
            if ($ret->isError()) {
 
305
            if ($ret) {
311
306
                return array($ret->wrap(__FILE__, __LINE__), null, null);
312
307
            }
313
308
            $maxQuality = 100;
315
310
            do {
316
311
                $ret = $this->_transformImage($mimeType, 'convert', array(), $sourceFilename,
317
312
                                              $destFilename, $outputMimeType, $quality);
318
 
                if ($ret->isError()) {
 
313
                if ($ret) {
319
314
                    return array($ret->wrap(__FILE__, __LINE__), null, null);
320
315
                }
321
316
                clearstatcache();
329
324
                $quality = round(($minQuality + $maxQuality) / 2);
330
325
            } while ($maxQuality - $minQuality > 2
331
326
                   && abs(($fileSize - $targetSize) / $targetSize) > 0.02);
332
 
            return array(GalleryStatus::success(), $outputMimeType, $context);
 
327
            return array(null, $outputMimeType, $context);
333
328
 
334
329
        default:
335
 
            return array(GalleryStatus::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
 
330
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
336
331
                                              "$operationName $outputMimeType"), null, null);
337
332
        }
338
333
 
346
341
            $context['imagemagick.mime'] = $mimeType;
347
342
 
348
343
            if ($sourceFilename != $destFilename) {
349
 
                $platform = $gallery->getPlatform();
 
344
                $platform =& $gallery->getPlatform();
350
345
                if (!$platform->copy($sourceFilename, $destFilename)) {
351
 
                    return array(GalleryStatus::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__),
 
346
                    return array(GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__),
352
347
                                 null, null);
353
348
                }
354
349
            }
355
 
            return array(GalleryStatus::success(), $outputMimeType, $context);
 
350
            return array(null, $outputMimeType, $context);
356
351
        }
357
352
 
358
353
        if ($outputMimeType == $mimeType && empty($transform)) {
359
354
            /* Just copy the source to the destination */
360
355
            if ($sourceFilename != $destFilename) {
361
 
                $platform = $gallery->getPlatform();
 
356
                $platform =& $gallery->getPlatform();
362
357
                if (!$platform->copy($sourceFilename, $destFilename)) {
363
 
                    return array(GalleryStatus::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__),
 
358
                    return array(GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__),
364
359
                                 null, null);
365
360
                }
366
361
            }
367
362
        } else {
368
363
            $ret = $this->_transformImage($mimeType, $cmd, $transform,
369
364
                                          $sourceFilename, $destFilename, $outputMimeType);
370
 
            if ($ret->isError()) {
 
365
            if ($ret) {
371
366
                return array($ret->wrap(__FILE__, __LINE__), null, null);
372
367
            }
373
368
        }
374
369
 
375
 
        return array(GalleryStatus::success(), $outputMimeType, $context);
 
370
        return array(null, $outputMimeType, $context);
376
371
    }
377
372
 
378
373
    /**
385
380
         * Run it through PHP first, it's faster and more portable.  If it runs
386
381
         * afoul of open_basedir it'll return false and we can try ImageMagick.
387
382
         */
388
 
        $platform = $gallery->getPlatform();
 
383
        $platform =& $gallery->getPlatform();
389
384
        $results = $platform->getimagesize($filename);
390
385
        if (($results != false) &&
391
386
            (($results[0] > 1) && ($results[1] > 1))) {
392
 
            return array(GalleryStatus::success(), $results[0], $results[1]);
 
387
            return array(null, $results[0], $results[1]);
393
388
        }
394
389
 
395
390
        list ($ret, $cmd) = ImageMagickToolkitHelper::getCommand('identify');
396
 
        if ($ret->isError()) {
 
391
        if ($ret) {
397
392
            return array($ret->wrap(__FILE__, __LINE__), 0, 0);
398
393
        }
399
394
 
402
397
        list ($success, $output) = $platform->exec(array(array_merge($cmd, array($filename))));
403
398
        if (!$success) {
404
399
            $platform->chdir($oldCwd);
405
 
            return array(GalleryStatus::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), 0, 0);
 
400
            return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), 0, 0);
406
401
        }
407
402
        $platform->chdir($oldCwd);
408
403
 
409
404
        foreach ($output as $line) {
410
405
            if (ereg('([0-9]+)x([0-9]+)', $line, $regs)) {
411
 
                return array(GalleryStatus::success(), $regs[1], $regs[2]);
 
406
                return array(null, $regs[1], $regs[2]);
412
407
            }
413
408
        }
414
409
 
415
 
        return array(GalleryStatus::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null, null);
 
410
        return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null, null);
416
411
    }
417
412
 
418
413
    /**
420
415
     */
421
416
    function _getPageCount($filename, $mimeType) {
422
417
        global $gallery;
423
 
        $platform = $gallery->getPlatform();
 
418
        $platform =& $gallery->getPlatform();
424
419
 
425
420
        list ($ret, $cmd) = ImageMagickToolkitHelper::getCommand('identify');
426
 
        if ($ret->isError()) {
 
421
        if ($ret) {
427
422
            return array($ret->wrap(__FILE__, __LINE__), null);
428
423
        }
429
424
 
440
435
 
441
436
        $platform->chdir($oldCwd);
442
437
        if (!$success) {
443
 
            return array(GalleryStatus::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null);
 
438
            return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null);
444
439
        }
445
440
 
446
441
        $count = 0;
460
455
            }
461
456
        }
462
457
 
463
 
        return array(GalleryStatus::success(), $count);
 
458
        return array(null, $count);
464
459
    }
465
460
 
466
461
    /**
468
463
     */
469
464
    function _getColorspace($filename) {
470
465
        global $gallery;
471
 
        $platform = $gallery->getPlatform();
 
466
        $platform =& $gallery->getPlatform();
472
467
 
473
468
        list ($ret, $cmd) = ImageMagickToolkitHelper::getCommand('identify');
474
 
        if ($ret->isError()) {
 
469
        if ($ret) {
475
470
            return array($ret->wrap(__FILE__, __LINE__), null);
476
471
        }
477
472
 
478
473
        list ($success, $output) =
479
474
            $platform->exec(array(array_merge($cmd, array('-format', '%r', $filename))));
480
475
        if (!$success || empty($output)) {
481
 
            return array(GalleryStatus::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null);
 
476
            return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null);
482
477
        }
483
478
        foreach (array('RGB', 'CMYK') as $colorspace) {
484
479
            if (strpos($output[0], $colorspace) !== false) {
485
 
                return array(GalleryStatus::success(), $colorspace);
 
480
                return array(null, $colorspace);
486
481
            }
487
482
        }
488
 
        return array(GalleryStatus::success(), 'unknown');
 
483
        return array(null, 'unknown');
489
484
    }
490
485
 
491
486
    /**
502
497
 
503
498
        /* Get a temp file name and figure out our convert-from-pnm command */
504
499
        $tmpDir = $gallery->getConfig('data.gallery.tmp');
505
 
        $platform = $gallery->getPlatform();
 
500
        $platform =& $gallery->getPlatform();
506
501
        $tmpFilename = $platform->tempnam($tmpDir, 'imgk_');
507
502
        if (empty($tmpFilename)) {
508
503
            /* This can happen if the $tmpDir path is bad */
509
 
            return GalleryStatus::error(ERROR_BAD_PATH, __FILE__, __LINE__);
 
504
            return GalleryCoreApi::error(ERROR_BAD_PATH, __FILE__, __LINE__);
510
505
        }
511
506
 
512
507
        list ($ret, $command) = ImageMagickToolkitHelper::getCommand($cmd);
513
 
        if ($ret->isError()) {
 
508
        if ($ret) {
514
509
            @$platform->unlink($tmpFilename);
515
510
            return $ret->wrap(__FILE__, __LINE__);
516
511
        }
517
512
        if (!isset($jpegQuality)) {
518
513
            list ($ret, $jpegQuality) =
519
514
                GalleryCoreApi::getPluginParameter('module', 'imagemagick', 'jpegQuality');
520
 
            if ($ret->isError()) {
 
515
            if ($ret) {
521
516
                @$platform->unlink($tmpFilename);
522
517
                return $ret->wrap(__FILE__, __LINE__);
523
518
            }
604
599
            } else {
605
600
                /* Don't know this type! */
606
601
                @$platform->unlink($tmpFilename);
607
 
                return GalleryStatus::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__,
 
602
                return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__,
608
603
                                            "Can't convert to unknown mime type: $outputMimeType");
609
604
            }
610
605
        }
615
610
        if (!$success) {
616
611
            @$platform->unlink($tmpFilename);
617
612
            $platform->chdir($oldCwd);
618
 
            return GalleryStatus::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__);
 
613
            return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__);
619
614
        }
620
615
        $platform->chdir($oldCwd);
621
616
 
622
617
        $success = $platform->rename($tmpFilename, $destFilename);
623
618
        if (!$success) {
624
619
            @$platform->unlink($tmpFilename);
625
 
            return GalleryStatus::error(ERROR_PLATFORM_FAILURE,
626
 
                                        __FILE__, __LINE__,
627
 
                                        "Failed renaming $tmpFilename -> $destFilename");
628
 
        } else {
629
 
            return GalleryStatus::success();
 
620
            return GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__,
 
621
                                         "Failed renaming $tmpFilename -> $destFilename");
630
622
        }
 
623
 
 
624
        /* ImageMagick tends to write output files with 600 permission */
 
625
        $platform->chmod($destFilename);
 
626
 
 
627
        return null;
631
628
    }
632
629
 
633
630
    /**
637
634
     * @param string an ImageMagick command (eg. "convert")
638
635
     */
639
636
    function _imageMagickCmd($cmd) {
640
 
        global $gallery;
641
 
 
642
 
        $platform = $gallery->getPlatform();
643
637
        list ($ret, $imageMagickPath) =
644
638
                GalleryCoreApi::getPluginParameter('module', 'imagemagick', 'path');
645
 
        if ($ret->isError()) {
 
639
        if ($ret) {
646
640
            return $ret->wrap(__FILE__, __LINE__);
647
641
        }
648
642