Ejemplo n.º 1
0
 /**
  * @param integer|string $id path or file id
  * @param int            $maxWidth
  * @param int            $maxHeight
  *
  * @return string
  */
 public function resizeImage($id, $maxWidth = 100, $maxHeight = 100, $quality = 8)
 {
     $path = $this->webFilesystem->getPath($id);
     $cacheDir = 'cache/rendered-image/' . $path;
     //@todo handle cacche
     $image = $this->webFilesystem->getResizeMax($path, $maxWidth, $maxHeight);
     $this->webFilesystem->writeImage($image->getResult(), $cacheDir, $quality);
     return $cacheDir;
 }
Ejemplo n.º 2
0
 /**
  * @ApiDoc(
  *  section="File Manager",
  *  description="Displays a thumbnail/resized version of a image"
  * )
  *
  * This exists the process and sends a `content-type: image/png` http container.
  *
  * @Rest\QueryParam(name="path", requirements=".+", strict=true, description="The file path or its ID")
  * @Rest\QueryParam(name="width", requirements="[0-9]+", description="The image width")
  * @Rest\QueryParam(name="height", requirements="[0-9]*", description="The image height")
  *
  * @Rest\Get("/admin/file/preview")
  *
  * @param ParamFetcher $paramFetcher
  * @return Response
  */
 public function showPreviewAction(ParamFetcher $paramFetcher)
 {
     $path = $paramFetcher->get('path');
     $width = $paramFetcher->get('width');
     $height = $paramFetcher->get('height');
     if (!$width && !$height) {
         $width = 50;
         $height = 50;
     }
     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;
     }
     $image = null;
     try {
         $image = $this->webFilesystem->getResizeMax($path, $width, $height);
     } catch (\Exception $e) {
         $image = $this->webFilesystem->getResizeMax('bundles/jarves/images/broken-image.png', $width, $height);
     }
     $expires = 3600;
     //1 h
     $response = new Response();
     $response->headers->set('Content-Type', 'image/png');
     $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();
     imagepng($image->getResult(), null, 8);
     $imageData = ob_get_contents();
     ob_end_clean();
     $response->setContent($imageData);
     return $response;
 }