MODX imageRotate Plugin
This file contains 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 | |
// run plugin OnFileManagerUpload | |
switch ($modx->event->name) { | |
case "OnFileManagerUpload": | |
if(!empty($files)){ | |
foreach($files as $file){ | |
if ($file['type'] != "image/jpeg" && $file['type'] != "image/png") | |
return; | |
// max number of pixels wide or high | |
$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); | |
$orientation = $imagen->getImageOrientation(); | |
switch($orientation) { | |
case imagick::ORIENTATION_BOTTOMRIGHT: | |
$imagen->rotateimage("#000", 180); // rotate 180 degrees | |
break; | |
case imagick::ORIENTATION_RIGHTTOP: | |
$imagen->rotateimage("#000", 90); // rotate 90 degrees CW | |
break; | |
case imagick::ORIENTATION_LEFTBOTTOM: | |
$imagen->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! | |
$imagen->setImageOrientation(imagick::ORIENTATION_TOPLEFT); | |
// Limit the max size to a specific dimension (if set) | |
if($max_image_dimension > 0){ | |
$width = $imagen->getImageWidth(); | |
$height = $imagen->getImageHeight(); | |
if($width > $height) | |
$imagen->resizeImage($max_image_dimension,0,Imagick::FILTER_LANCZOS,1); | |
if($height > $width) | |
$imagen->resizeImage(0,$max_image_dimension,Imagick::FILTER_LANCZOS,1); | |
} | |
$imagen->writeImage($originalFilename); | |
}else{ | |
$modx->log(xPDO::LOG_LEVEL_ERROR, $originalFilename. ' NOT FOUND!'); | |
} | |
} | |
} | |
return; | |
break; | |
} | |
return; |
This file contains 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 | |
use \Imagick; | |
use MODX\Revolution\modX; | |
// run plugin OnFileManagerUpload | |
switch ($modx->event->name) { | |
case "OnFileManagerUpload": | |
if ($directory == DIRECTORY_SEPARATOR) { | |
$directory = ''; | |
} | |
if (!empty($files)) { | |
$filesystem = $source->getFilesystem(); | |
foreach ($files as $file) { | |
if ($file['type'] != "image/jpeg" && $file['type'] != "image/png") { | |
return; | |
} | |
if ((boolean)$modx->getOption('upload_translit')) { | |
$newName = $modx->filterPathSegment($file['name']); | |
if ($newName !== $file['name']) { | |
$file['name'] = $newName; | |
} | |
} | |
// max number of pixels wide or high | |
$max_image_dimension = $modx->getOption('max_image_dimension', $this->sp, 1000); | |
$path = !empty($directory) | |
? $source->sanitizePath( | |
$directory . DIRECTORY_SEPARATOR . ltrim($file['name'], DIRECTORY_SEPARATOR) | |
) | |
: $file['name']; | |
if ($filesystem->fileExists($path)) { | |
$imagen = new Imagick(); | |
$imagen->readImageBlob($filesystem->read($path)); | |
$orientation = $imagen->getImageOrientation(); | |
switch ($orientation) { | |
case Imagick::ORIENTATION_BOTTOMRIGHT: | |
$imagen->rotateimage("#000", 180); // rotate 180 degrees | |
break; | |
case Imagick::ORIENTATION_RIGHTTOP: | |
$imagen->rotateimage("#000", 90); // rotate 90 degrees CW | |
break; | |
case Imagick::ORIENTATION_LEFTBOTTOM: | |
$imagen->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! | |
$imagen->setImageOrientation(Imagick::ORIENTATION_TOPLEFT); | |
// Limit the max size to a specific dimension (if set) | |
if ($max_image_dimension > 0) { | |
$width = $imagen->getImageWidth(); | |
$height = $imagen->getImageHeight(); | |
if ($width > $height) { | |
$imagen->resizeImage($max_image_dimension, 0, Imagick::FILTER_LANCZOS, 1); | |
} | |
if ($height > $width) { | |
$imagen->resizeImage(0, $max_image_dimension, Imagick::FILTER_LANCZOS, 1); | |
} | |
} | |
$filesystem->write($path, $imagen->getImageBlob()); | |
} else { | |
$modx->log(modX::LOG_LEVEL_ERROR, $path. ' NOT FOUND!'); | |
} | |
} | |
} | |
return; | |
break; | |
} | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment