public function images_rotate()
 {
     $view = $this->getView();
     $request = $this->getPageRequest();
     if (!$request->isJSON()) {
         return View::ERROR_BADREQUEST;
     }
     $albumid = $request->getParameter(0);
     $album = new GalleryAlbumModel($albumid);
     $image = new GalleryImageModel($request->getParameter('image'));
     $rotate = $request->getParameter('rotate');
     $view->contenttype = View::CTYPE_JSON;
     if (!$albumid) {
         return View::ERROR_BADREQUEST;
     }
     if (!$album->exists()) {
         return View::ERROR_NOTFOUND;
     }
     if (!$image->exists()) {
         return View::ERROR_NOTFOUND;
     }
     if ($image->get('albumid') != $album->get('id')) {
         return View::ERROR_BADREQUEST;
     }
     $manager = \Core\user()->checkAccess('p:/gallery/manage_all');
     $editor = \Core\user()->checkAccess($album->get('editpermissions')) || $manager;
     $uploader = \Core\user()->checkAccess($album->get('uploadpermissions')) || $editor;
     if (!$uploader) {
         return View::ERROR_ACCESSDENIED;
     }
     // Uploaders only can only edit their own image!
     if (!$editor && $image->get('uploaderid') != \Core\user()->get('id')) {
         return View::ERROR_ACCESSDENIED;
     }
     // According to GD:
     //       0
     //  90       270
     //      180
     // The new rotation is based on the previous and the direction.
     $current = $image->get('rotation');
     $new = null;
     if ($rotate == 'cw') {
         if ($current == 0) {
             $new = 270;
         } elseif ($current > 270) {
             $new = 270;
         } elseif ($current > 180) {
             $new = 180;
         } elseif ($current > 90) {
             $new = 90;
         } else {
             $new = 0;
         }
     } else {
         if ($current == 0) {
             $new = 90;
         } elseif ($current < 90) {
             $new = 90;
         } elseif ($current < 180) {
             $new = 180;
         } elseif ($current < 270) {
             $new = 270;
         } else {
             $new = 0;
         }
     }
     $image->set('rotation', $new);
     $image->save();
     $view->jsondata = array('status' => '1', 'message' => 'Rotated image');
 }