3
3
* $RCSfile: ImageMagickToolkit.class,v $
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
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.
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>
30
30
* Load required classes
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');
36
36
* A ImageMagick version of GalleryToolkit
55
50
switch($propertyName) {
57
52
list ($ret, $width, $height) = $this->_getImageDimensions($mimeType, $sourceFilename);
58
if ($ret->isError()) {
59
54
return array($ret->wrap(__FILE__, __LINE__), null);
61
56
$results = array((int)$width, (int)$height);
65
60
list ($ret, $count) = $this->_getPageCount($sourceFilename, $mimeType);
66
if ($ret->isError()) {
67
62
return array($ret->wrap(__FILE__, __LINE__), null);
69
64
$results = array($count);
73
68
list ($ret, $colorspace) = $this->_getColorspace($sourceFilename);
74
if ($ret->isError()) {
75
70
return array($ret->wrap(__FILE__, __LINE__), null);
77
72
$results = array($colorspace);
81
return array(GalleryStatus::error(ERROR_UNIMPLEMENTED, __FILE__, __LINE__),
76
return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED, __FILE__, __LINE__),
85
return array(GalleryStatus::success(), $results);
80
return array(null, $results);
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()) {
132
127
return array($ret->wrap(__FILE__, __LINE__), null, null);
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()) {
152
147
return array($ret->wrap(__FILE__, __LINE__), null, null);
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()) {
216
211
return array($ret->wrap(__FILE__, __LINE__), null, null);
218
213
if (empty($cmd)) {
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"),
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) {
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()) {
311
306
return array($ret->wrap(__FILE__, __LINE__), null, null);
313
308
$maxQuality = 100;
316
311
$ret = $this->_transformImage($mimeType, 'convert', array(), $sourceFilename,
317
312
$destFilename, $outputMimeType, $quality);
318
if ($ret->isError()) {
319
314
return array($ret->wrap(__FILE__, __LINE__), null, null);
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);
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);
346
341
$context['imagemagick.mime'] = $mimeType;
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__),
355
return array(GalleryStatus::success(), $outputMimeType, $context);
350
return array(null, $outputMimeType, $context);
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__),
368
363
$ret = $this->_transformImage($mimeType, $cmd, $transform,
369
364
$sourceFilename, $destFilename, $outputMimeType);
370
if ($ret->isError()) {
371
366
return array($ret->wrap(__FILE__, __LINE__), null, null);
375
return array(GalleryStatus::success(), $outputMimeType, $context);
370
return array(null, $outputMimeType, $context);
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.
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]);
395
390
list ($ret, $cmd) = ImageMagickToolkitHelper::getCommand('identify');
396
if ($ret->isError()) {
397
392
return array($ret->wrap(__FILE__, __LINE__), 0, 0);
402
397
list ($success, $output) = $platform->exec(array(array_merge($cmd, array($filename))));
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);
407
402
$platform->chdir($oldCwd);
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]);
415
return array(GalleryStatus::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null, null);
410
return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null, null);
421
416
function _getPageCount($filename, $mimeType) {
423
$platform = $gallery->getPlatform();
418
$platform =& $gallery->getPlatform();
425
420
list ($ret, $cmd) = ImageMagickToolkitHelper::getCommand('identify');
426
if ($ret->isError()) {
427
422
return array($ret->wrap(__FILE__, __LINE__), null);
441
436
$platform->chdir($oldCwd);
443
return array(GalleryStatus::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null);
438
return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__), null);
469
464
function _getColorspace($filename) {
471
$platform = $gallery->getPlatform();
466
$platform =& $gallery->getPlatform();
473
468
list ($ret, $cmd) = ImageMagickToolkitHelper::getCommand('identify');
474
if ($ret->isError()) {
475
470
return array($ret->wrap(__FILE__, __LINE__), null);
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);
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);
488
return array(GalleryStatus::success(), 'unknown');
483
return array(null, 'unknown');
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__);
512
507
list ($ret, $command) = ImageMagickToolkitHelper::getCommand($cmd);
513
if ($ret->isError()) {
514
509
@$platform->unlink($tmpFilename);
515
510
return $ret->wrap(__FILE__, __LINE__);
517
512
if (!isset($jpegQuality)) {
518
513
list ($ret, $jpegQuality) =
519
514
GalleryCoreApi::getPluginParameter('module', 'imagemagick', 'jpegQuality');
520
if ($ret->isError()) {
521
516
@$platform->unlink($tmpFilename);
522
517
return $ret->wrap(__FILE__, __LINE__);
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");
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__);
620
615
$platform->chdir($oldCwd);
622
617
$success = $platform->rename($tmpFilename, $destFilename);
624
619
@$platform->unlink($tmpFilename);
625
return GalleryStatus::error(ERROR_PLATFORM_FAILURE,
627
"Failed renaming $tmpFilename -> $destFilename");
629
return GalleryStatus::success();
620
return GalleryCoreApi::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__,
621
"Failed renaming $tmpFilename -> $destFilename");
624
/* ImageMagick tends to write output files with 600 permission */
625
$platform->chmod($destFilename);
637
634
* @param string an ImageMagick command (eg. "convert")
639
636
function _imageMagickCmd($cmd) {
642
$platform = $gallery->getPlatform();
643
637
list ($ret, $imageMagickPath) =
644
638
GalleryCoreApi::getPluginParameter('module', 'imagemagick', 'path');
645
if ($ret->isError()) {
646
640
return $ret->wrap(__FILE__, __LINE__);