Skip to content

Commit

Permalink
Add PDF CropBox support, default on
Browse files Browse the repository at this point in the history
New pdfUseCropBox derviative setting for the imagemagick strategies can
disable it if necessary
  • Loading branch information
zerocrates committed Jul 16, 2024
1 parent c4c3c32 commit 798e267
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 798e267

Please sign in to comment.