示例#1
0
 /**
  * Upload an image via form-data.
  * Allowed only .jpg format.
  *
  * Optional parameters: (lat, lon), tag, description.
  */
 function index_post()
 {
     header('Access-Control-Allow-Origin: *');
     if (!isset($_FILES['image'])) {
         $this->response(error('Provide \'image\' parameter.', 400), 400);
         return;
     }
     $imageBytes = $_FILES['image'];
     $imageDescription = $this->post('description');
     $lat = $this->post('lat');
     $lon = $this->post('lon');
     $location = null;
     if ($lat != null && $lon != null) {
         $location = new Location();
         $location->setLat((double) $lat);
         $location->setLon((double) $lon);
     }
     $tagValue = $this->post('tag');
     $tag = null;
     if ($tagValue != null) {
         $tag = new Tag();
         $tag->setValue($tagValue);
     }
     $imageModel = new ImageModel($this->db);
     try {
         $image = $imageModel->createImage($imageBytes, $imageDescription, $tag, $location);
         $this->response($image->serialize(), 201);
     } catch (ImageAlreadyExistsException $e) {
         $this->response(error('Image already exists.', 409), 409);
     } catch (InvalidImageExtensionException $e) {
         $this->response(error('Invalid image extension. Allowed only .jpg.', 400), 400);
     }
 }