public function autoRotate() {
if($this->_use_imagick) {
switch($this->im->getImageOrientation()) {
case imagick::ORIENTATION_TOPRIGHT:
$this->im->flopImage();
break;
case imagick::ORIENTATION_BOTTOMRIGHT:
$this->im->rotateimage(new ImagickPixel('none'), 180); // rotate 180 degrees
break;
case imagick::ORIENTATION_BOTTOMLEFT:
$this->im->flopImage();
$this->im->rotateImage(new ImagickPixel('none'), 180);
break;
case imagick::ORIENTATION_LEFTTOP:
$this->im->flopImage();
$this->im->rotateImage(new ImagickPixel('none'), -90);
break;
case imagick::ORIENTATION_RIGHTTOP:
$this->im->rotateimage(new ImagickPixel('none'), 90); // rotate 90 degrees CW
break;
case imagick::ORIENTATION_RIGHTBOTTOM:
$this->im->flopImage();
$this->im->rotateImage(new ImagickPixel('none'), 90);
break;
case imagick::ORIENTATION_LEFTBOTTOM:
$this->im->rotateimage(new ImagickPixel('none'), -90); // rotate 90 degrees CCW
break;
default:
// DO NOTHING, THE IMAGE IS OK OR WE DON'T KNOW IF IT'S ROTATED
break;
}
$this->im->stripImage();
} else {
if(isset($this->_exif['Orientation'])) {
switch($this->_exif['Orientation']) {
case 1:
default:
// DO NOTHING, THE IMAGE IS OK OR WE DON'T KNOW IF IT'S ROTATED
break;
case 2:
imageflip($this->im, IMG_FLIP_HORIZONTAL);
break;
case 3:
$this->im = imagerotate($this->im, 180, 0);
break;
case 4:
$this->im = imagerotate($this->im, 180, 0);
imageflip($this->im, IMG_FLIP_HORIZONTAL);
break;
case 5:
$this->im = imagerotate($this->im, 270, 0);
imageflip($this->im, IMG_FLIP_HORIZONTAL);
$aux = $this->_height;
$this->_height = $this->_width;
$this->_width = $aux;
break;
case 6:
$this->im = imagerotate($this->im, -90, 0);
$aux = $this->_height;
$this->_height = $this->_width;
$this->_width = $aux;
break;
case 7:
$this->im = imagerotate($this->im, 90, 0);
imageflip($this->im, IMG_FLIP_HORIZONTAL);
$aux = $this->_height;
$this->_height = $this->_width;
$this->_width = $aux;
break;
case 8:
$this->im = imagerotate($this->im, 90, 0);
$aux = $this->_height;
$this->_height = $this->_width;
$this->_width = $aux;
break;
}
$this->_exif['Orientation'] = 1;
}
}
return $this;
}