Last active
August 31, 2022 14:50
-
-
Save matdave/38055342f747fdf0a4c8d6a4831a4cc8 to your computer and use it in GitHub Desktop.
MODX Auto Rotate Image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Based on https://gist.github.com/OptimusCrime/dac5deec0d65872740c9d7bccdc5e336 | |
** Add system setting named "max_image_dimension" to set a max height or width for images | |
*/ | |
if(!function_exists(autoRotateImage)) { | |
function autoRotateImage($image,$max_image_dimension = 0) { | |
$orientation = $image->getImageOrientation(); | |
switch($orientation) { | |
case imagick::ORIENTATION_BOTTOMRIGHT: | |
$image->rotateimage("#000", 180); // rotate 180 degrees | |
break; | |
case imagick::ORIENTATION_RIGHTTOP: | |
$image->rotateimage("#000", 90); // rotate 90 degrees CW | |
break; | |
case imagick::ORIENTATION_LEFTBOTTOM: | |
$image->rotateimage("#000", -90); // rotate 90 degrees CCW | |
break; | |
} | |
// Now that it's auto-rotated, make sure the EXIF data is correct in case the EXIF gets saved with the image! | |
$image->setImageOrientation(imagick::ORIENTATION_TOPLEFT); | |
// Limit the max size to a specific dimension (if set) | |
if($max_image_dimension > 0){ | |
$width = $image->getImageWidth(); | |
$height = $image->getImageHeight(); | |
if($width > $height) | |
$image->resizeImage($max_image_dimension,0,Imagick::FILTER_LANCZOS,1); | |
if($height > $width) | |
$image->resizeImage(0,$max_image_dimension,Imagick::FILTER_LANCZOS,1); | |
} | |
} | |
} | |
switch ($modx->event->name) { | |
case "OnFileManagerUpload": | |
if(!empty($files)){ | |
foreach($files as $file){ | |
if ($file['type'] != "image/jpeg" && $file['type'] != "image/png") | |
return; | |
$max_image_dimension = $modx->getOption('max_image_dimension',$scriptProperties,0); | |
$basePath = $source->getBasePath(); | |
$originalFilename = $basePath . $directory . $file['name']; | |
if(file_exists($originalFilename)){ | |
$imagen = new Imagick($originalFilename); | |
autoRotateImage($imagen,$max_image_dimension); | |
$imagen->writeImage($originalFilename); | |
}else{ | |
$modx->log(xPDO::LOG_LEVEL_ERROR, $originalFilename. ' NOT FOUND!'); | |
} | |
} | |
} | |
return; | |
break; | |
} | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
on line 5 the function has to be quoted
if(!function_exists('autoRotateImage')) {