From 798e26789f1016139bc3dc149e81c1f8ffedf2e4 Mon Sep 17 00:00:00 2001 From: John Flatness Date: Tue, 16 Jul 2024 14:04:12 -0400 Subject: [PATCH] Add PDF CropBox support, default on New pdfUseCropBox derviative setting for the imagemagick strategies can disable it if necessary --- .../Strategy/ExternalImageMagick.php | 26 +++++++++++++++++-- .../File/Derivative/Strategy/Imagick.php | 9 ++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/application/libraries/Omeka/File/Derivative/Strategy/ExternalImageMagick.php b/application/libraries/Omeka/File/Derivative/Strategy/ExternalImageMagick.php index d44554136..61d75a9c6 100644 --- a/application/libraries/Omeka/File/Derivative/Strategy/ExternalImageMagick.php +++ b/application/libraries/Omeka/File/Derivative/Strategy/ExternalImageMagick.php @@ -36,11 +36,11 @@ class Omeka_File_Derivative_Strategy_ExternalImageMagick extends Omeka_File_Deri public function createImage($sourcePath, $destPath, $type, $sizeConstraint, $mimeType) { $convertPath = $this->_getConvertPath(); + $inputArgs = $this->_getInputArgs($sourcePath, $mimeType); $convertArgs = $this->_getConvertArgs($type, $sizeConstraint); - $page = (int) $this->getOption('page', 0); $cmd = join(' ', array( escapeshellarg($convertPath), - escapeshellarg($sourcePath . '[' . $page . ']'), + $inputArgs, $convertArgs, escapeshellarg($destPath) )); @@ -83,6 +83,28 @@ protected function _getConvertPath() } } + /** + * Get the ImageMagick command line for resizing to the given constraints. + * + * @param string $sourcePath Path to the original file + * @param string $mimeType Media type of the original file + * @return string + */ + protected function _getInputArgs($sourcePath, $mimeType) + { + $args = array(); + $page = (int) $this->getOption('page', 0); + + if ($mimeType === 'application/pdf') { + $args[] = '-density 150'; + if ($this->getOption('pdfUseCropBox', true)) { + $args[] = '-define pdf:use-cropbox=true'; + } + } + $args[] = escapeshellarg($sourcePath . '[' . $page . ']'); + return join(' ', $args); + } + /** * Get the ImageMagick command line for resizing to the given constraints. * diff --git a/application/libraries/Omeka/File/Derivative/Strategy/Imagick.php b/application/libraries/Omeka/File/Derivative/Strategy/Imagick.php index df9c79ac2..d16f5e19c 100644 --- a/application/libraries/Omeka/File/Derivative/Strategy/Imagick.php +++ b/application/libraries/Omeka/File/Derivative/Strategy/Imagick.php @@ -34,7 +34,14 @@ public function createImage($sourcePath, $destPath, $type, $sizeConstraint, $mim { $page = (int) $this->getOption('page', 0); try { - $imagick = new Imagick($sourcePath . '[' . $page . ']'); + $imagick = new Imagick; + if ($mimeType === 'application/pdf') { + $imagick->setResolution(150, 150); + if ($this->getOption('pdfUseCropBox', true)) { + $imagick->setOption('pdf:use-cropbox', true); + } + } + $imagick->readImage($sourcePath . '[' . $page . ']'); } catch (ImagickException $e) { _log("Imagick failed to open the file. Details:\n$e", Zend_Log::ERR); return false;