public function indexAction()
 {
     $id = $this->params()->fromQuery('id', null);
     $token = $this->params()->fromQuery('token', null);
     if (!$id) {
         return new JsonModel(['status' => 0, 'errors' => ['You must supply the image id']]);
     }
     if (!$token) {
         return new JsonModel(['status' => 0, 'errors' => ['You must supply the access token']]);
     }
     /** @var Upload $upload */
     $upload = Upload::where('access_token', '=', $token)->where('id', '=', $id)->where('access_token_expiration', '>=', time())->first();
     if (!$upload) {
         return new JsonModel(['status' => 0, 'errors' => ['The request has expired or invalid upload id requested']]);
     }
     // get image content
     $response = $this->getResponse();
     $response->setContent($upload->content);
     $response->getHeaders()->addHeaderLine('Content-Type', $upload->headers)->addHeaderLine('Content-Length', mb_strlen($upload->content));
     return $response;
 }
 /**
  * @param array $post
  * @param null  $minWidth
  * @param null  $minHeight
  *
  * @return UploadResultDto
  */
 public function upload(array $post, $minWidth = null, $minHeight = null)
 {
     //TODO
     $file = array_key_exists('file', $post) ? $post['file'] : [];
     if (isset($post['flowFilename'])) {
         $file['name'] = $post['flowFilename'];
     }
     try {
         $types = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'application/octet-stream'];
         $exts = ['.jpeg', '.jpg', '.png', '.gif'];
         if (!$file) {
             return new UploadResultDto(['status' => 0, 'error_code' => self::UPLOAD_RESULT_ERROR_NO_FILE_DATA_SENT]);
         }
         if (!in_array($file['type'], $types)) {
             return new UploadResultDto(['status' => 0, 'error_code' => self::UPLOAD_ERROR_INVALID_IMAGE_TYPE]);
         }
         $imageinfo = getimagesize($file['tmp_name']);
         if (!in_array($imageinfo['mime'], $types)) {
             return new UploadResultDto(['status' => 0, 'error_code' => self::UPLOAD_ERROR_INVALID_IMAGE_TYPE]);
         }
         $found = false;
         foreach ($exts as $item) {
             if (preg_match("/{$item}\$/i", $file['name'])) {
                 $found = true;
             }
         }
         if (!$found) {
             return new UploadResultDto(['status' => 0, 'error_code' => self::UPLOAD_ERROR_INVALID_IMAGE_TYPE]);
         }
         $tempPath = $file['tmp_name'];
         $header = null;
         switch ($imageinfo[2]) {
             case IMAGETYPE_GIF:
                 $images_orig = imagecreatefromgif($tempPath);
                 $header = 'image/gif';
                 break;
             case IMAGETYPE_JPEG:
             case IMAGETYPE_JPEG2000:
                 $images_orig = imagecreatefromjpeg($tempPath);
                 $header = 'image/jpeg';
                 break;
             case IMAGETYPE_PNG:
                 $images_orig = imagecreatefrompng($tempPath);
                 $header = 'image/png';
                 break;
             default:
                 return new UploadResultDto(['status' => 0, 'error_code' => self::UPLOAD_ERROR_INVALID_IMAGE_TYPE]);
         }
         if ($minWidth || $minHeight) {
             $sizex = imagesx($images_orig);
             $sizey = imagesy($images_orig);
             if ($minWidth && $sizex < $minWidth) {
                 return new UploadResultDto(['status' => 0, 'error_code' => self::UPLOAD_ERROR_INVALID_WIDTH]);
             }
             if ($minHeight && $sizey < $minHeight) {
                 return new UploadResultDto(['status' => 0, 'error_code' => self::UPLOAD_ERROR_INVALID_HEIGHT]);
             }
         }
         ob_start();
         imagejpeg($images_orig);
         $imageFileContents = ob_get_contents();
         ob_end_clean();
         $upload = new Upload();
         $upload->headers = $header;
         $upload->content = $imageFileContents;
         $upload->save();
         return new UploadResultDto(['status' => 1, 'id' => $upload->id]);
     } catch (\Exception $e) {
         //            sd($e->getMessage());
         return new UploadResultDto(['status' => 0, 'error_code' => self::UPLOAD_ERROR_OTHER]);
     }
 }