Beispiel #1
0
 /**
  * @ApiDoc(
  *  section="File Manager",
  *  description="Displays a (complete) image (with cache-headers)"
  * )
  *
  * @Rest\QueryParam(name="path", requirements=".+", strict=true, description="The file path or its ID")
  *
  * @Rest\Get("/admin/file/image")
  *
  * @param ParamFetcher $paramFetcher
  * @return Response
  */
 public function showImageAction(ParamFetcher $paramFetcher)
 {
     $path = $paramFetcher->get('path');
     if (is_numeric($path)) {
         $path = $this->webFilesystem->getPath($path);
     }
     $this->checkAccess($path, ACL::MODE_VIEW);
     $file = $this->webFilesystem->getFile($path);
     if ($file->isDir()) {
         return;
     }
     $ifModifiedSince = $this->pageStack->getRequest()->headers->get('If-Modified-Since');
     if (isset($ifModifiedSince) && strtotime($ifModifiedSince) == $file->getModifiedTime()) {
         // Client's cache IS current, so we just respond '304 Not Modified'.
         $response = new Response();
         $response->setStatusCode(304);
         $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s', $file->getModifiedTime()) . ' GMT');
         return $response;
     }
     $content = $this->webFilesystem->read($path);
     $image = \PHPImageWorkshop\ImageWorkshop::initFromString($content);
     $result = $image->getResult();
     $size = new FileSize();
     $size->setHandleFromBinary($content);
     $expires = 3600;
     //1 h
     $response = new Response();
     $response->headers->set('Content-Type', 'png' == $size->getType() ? 'image/png' : 'image/jpeg');
     $response->headers->set('Pragma', 'public');
     $response->headers->set('Cache-Control', 'max-age=' . $expires);
     $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s', $file->getModifiedTime()) . ' GMT');
     $response->headers->set('Expires', gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
     ob_start();
     if ('png' === $size->getType()) {
         imagepng($result, null, 3);
     } else {
         imagejpeg($result, null, 100);
     }
     $response->setContent(ob_get_contents());
     ob_end_clean();
     return $response;
 }