Skip to content

Commit

Permalink
Add optional auto-orient support for IM strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
zerocrates committed Aug 25, 2015
1 parent 7701cc2 commit ec2e8f0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions application/config/config.ini.changeme
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ mail.transport.type = "Sendmail"
;
; fileDerivatives.strategyOptions.page = "0"
; fileDerivatives.strategyOptions.gravity = "center"
; fileDerivatives.strategyOptions.autoOrient = false

; fileDerivatives.typeWhitelist[]
; If set, Omeka will only attempt to create derivatives for files with the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ protected function _getConvertArgs($type, $constraint)
$version = $this->getOption('version', '0');

if ($type != 'square_thumbnail') {
return '-background white +repage -flatten -thumbnail ' . escapeshellarg("{$constraint}x{$constraint}>");
$args = array(
'-background white',
'+repage',
'-flatten',
'-thumbnail ' . escapeshellarg("{$constraint}x{$constraint}>")
);
} else {
$gravity = $this->getOption('gravity', 'Center');
// Native square thumbnail resize requires at least version 6.3.8-3.
Expand All @@ -123,8 +128,12 @@ protected function _getConvertArgs($type, $constraint)
'+repage'
);
}
return join (' ', $args);
}

if ($this->getOption('autoOrient', false)) {
array_unshift($args, '-auto-orient');
}
return join (' ', $args);
}

/**
Expand Down
39 changes: 39 additions & 0 deletions application/libraries/Omeka/File/Derivative/Strategy/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function createImage($sourcePath, $destPath, $type, $sizeConstraint, $mim
return false;
}

if ($this->getOption('autoOrient', false)) {
$this->_autoOrient($imagick);
}

$origX = $imagick->getImageWidth();
$origY = $imagick->getImageHeight();

Expand Down Expand Up @@ -137,4 +141,39 @@ protected function _getCropOffsetY($resizedY, $sizeConstraint)
return (int) (($resizedY - $sizeConstraint) / 2);
}
}

protected function _autoOrient($imagick)
{
$orientation = $imagick->getImageOrientation();
$white = new ImagickPixel('#fff');
switch ($orientation) {
case Imagick::ORIENTATION_RIGHTTOP:
$imagick->rotateImage($white, 90);
break;
case Imagick::ORIENTATION_BOTTOMRIGHT:
$imagick->rotateImage($white, 180);
break;
case Imagick::ORIENTATION_LEFTBOTTOM:
$imagick->rotateImage($white, 270);
break;
case Imagick::ORIENTATION_TOPRIGHT:
$imagick->flopImage();
break;
case Imagick::ORIENTATION_RIGHTBOTTOM:
$imagick->flopImage();
$imagick->rotateImage($white, 90);
break;
case Imagick::ORIENTATION_BOTTOMLEFT:
$imagick->flopImage();
$imagick->rotateImage($white, 180);
break;
case Imagick::ORIENTATION_LEFTTOP:
$imagick->flopImage();
$imagick->rotateImage($white, 270);
break;
case Imagick::ORIENTATION_TOPLEFT:
default:
break;
}
}
}

0 comments on commit ec2e8f0

Please sign in to comment.