Skip to content

Instantly share code, notes, and snippets.

@matdave
Last active October 2, 2023 07:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matdave/896dac22ee939afd8f228cf37cd84ee7 to your computer and use it in GitHub Desktop.
Save matdave/896dac22ee939afd8f228cf37cd84ee7 to your computer and use it in GitHub Desktop.
MODX imageRotate Plugin
<?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;
<?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