/**
  * Create an Image from the submitted data.
  *
  * @ApiDoc(
  *   resource = true,
  *   description = "Creates a new image from the submitted data.",
  *   statusCodes = {
  *     200 = "Returned when successful.",
  *     400 = "Returned when image wasnt saved successfully."
  *   }
  * )
  *
  * @param ParamFetcher $paramFetcher Paramfetcher
  *
  * @RequestParam(name="title", nullable=false, strict=true, description="Title.")
  * @RequestParam(name="data", nullable=false, strict=true, description="Image data.")
  * @RequestParam(name="dropbox_auth", nullable=true, strict=false, description="Storage hash for dropbox.")
  *
  * @return View
  */
 public function postAction(ParamFetcher $paramFetcher)
 {
     $image = new Image();
     $imageFile = base64_decode($paramFetcher->get('data'));
     $image->setTitle($paramFetcher->get('title'));
     $image->setStorageId($this->get('galerija_api.images.storage')->getStorageId());
     $view = View::create();
     try {
         $image->setImagePath($this->get('galerija_api.images.storage')->saveImage($imageFile));
     } catch (\RuntimeException $ex) {
         return $view->setStatusCode(400);
     }
     $this->getDoctrine()->getEntityManager()->persist($image);
     $this->getDoctrine()->getEntityManager()->flush();
     $this->get('galerija_api.images.storage')->setImageData($image);
     return $view->setData($image)->setStatusCode(200);
 }
 /**
  * Returns a <numColumns>x* collage of given images.
  *
  * @param int[]  $imageIds
  * @param int    $numColumns
  * @param string $title
  *
  * @return Image
  */
 public function makeCollage($imageIds, $title, $numColumns = 2)
 {
     if (!$numColumns) {
         $numColumns = 2;
     }
     /** @var Image[] $imageRecords */
     $imageRecords = $this->repository->findBy(['id' => $imageIds]);
     $photos = [];
     foreach ($imageRecords as $record) {
         $photos[] = $this->imagine->load($this->fileSystem->getImage($record->getImagePath()));
     }
     $photos = $this->sortPhotosByHeight($photos);
     $imagesPerColumn = ceil(sizeof($photos) / $numColumns);
     $columns = array_chunk($photos, $imagesPerColumn);
     $normalizedColumns = $this->findImagePositions($columns);
     $boxSize = $this->findBoxSize($normalizedColumns);
     $collage = $this->imagine->create($boxSize);
     foreach ($normalizedColumns as $column) {
         foreach ($column as $image) {
             $collage->paste($image['image'], $image['top']);
         }
     }
     $image = new Image();
     $image->setImagePath($this->fileSystem->saveImage($collage->get('png')));
     $image->setTitle($title);
     $image->setStorageId($this->fileSystem->getStorageId());
     $this->em->persist($image);
     $this->em->flush();
     return $image;
 }