/** * @api {put} /images/{imageId}/image?action=rotate Rotate an Image * @apiGroup Manipulating Images * @apiDescription Rotate an image clockwise or counter-clockwise. * @apiParam {string=rotate} action Action to perform. * @apiParam {int=90,180,270} degrees Degrees to rotate by. * @apiParam {string=cw,ccw} [direction=cw] Direction to rotate in (clockwise or counter-clockwise respectively). * @apiUse RequiresEditableImage * @apiUse ImageSuccessResponse * * @param Image $image * @param FileManager $fileManager * * @throws Exception * @return Response */ protected function rotate(Image $image, FileManager $fileManager) { $success = false; $this->validate($this->request, ['degrees' => 'required|integer|in:90,180,270', 'direction' => 'in:cw,ccw']); if ($image->operationInProgress) { throw new HttpException(409, "Another operation is currently in progress for this image."); } $image->operationInProgress = true; $image->save(); $degrees = (int) $this->request->input('degrees'); $direction = $this->request->has('direction') ? $this->request->input('direction') : 'cw'; if ($direction === 'cw') { $degrees = -$degrees; } $picture = $fileManager->getPictureForImageFile($image->imageFile); $picture->rotate($degrees); if ($newImageFile = $fileManager->savePicture($picture)) { $image->setImageFile($newImageFile); $image->operationInProgress = false; $success = $image->save(); } return $this->response(['success' => $success, 'image' => $image->fresh()]); }