Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment