/** * This action applies a given filter to a given image, saves the image and * outputs it to the browser at the same time * * @param string $path * @param string $filter * * @return Response * * @throws Exception */ public function filterAction($path, $filter) { $baseUrl = $this->request->getBaseUrl(); try { try { $cachedPath = $this->cacheManager->cacheImage($baseUrl, $path, $filter); } catch (RuntimeException $e) { if (!isset($this->notFoundImages[$filter])) { throw $e; } $path = $this->notFoundImages[$filter]; $cachedPath = $this->cacheManager->cacheImage($baseUrl, $path, $filter); } } catch (RouteNotFoundException $e) { throw new NotFoundHttpException('Filter doesn\'t exist.'); } // if cache path cannot be determined, return 404 if (null === $cachedPath) { throw new NotFoundHttpException('Image doesn\'t exist'); } try { // Using File instead of Imagine::open(), because i.e. image/x-icon is not widely supported. $file = new ImageFile($cachedPath, false); // TODO: add more media headers $headers = ['content-type' => $file->getMimeType(), 'content-length' => $file->getSize()]; $response = new Response($file->getContents(), 201, $headers); // Cache if (!($cacheType = $this->filterManager->getOption($filter, 'cache_type', false))) { return $response; } $cacheType === 'public' ? $response->setPublic() : $response->setPrivate(); $cacheExpires = $this->filterManager->getOption($filter, 'cache_expires', '1 day'); $expirationDate = new DateTime('+' . $cacheExpires); $maxAge = $expirationDate->format('U') - time(); if ($maxAge < 0) { throw new InvalidArgumentException('Invalid cache expiration date'); } $response->setExpires($expirationDate); $response->setMaxAge($maxAge); return $response; } catch (Exception $e) { throw $e; } }
/** * This action applies a given filter to a given image, saves the image and * outputs it to the browser at the same time * * @param string $path * @param string $filter * * @return Response */ public function filter($path, $filter) { $cachedPath = $this->cacheManager->cacheImage($this->request->getBaseUrl(), $path, $filter); // if cache path cannot be determined, return 404 if (null === $cachedPath) { throw new NotFoundHttpException('Image doesn\'t exist'); } ob_start(); try { $format = $this->filterManager->getOption($filter, "format", "png"); $this->imagine->open($cachedPath)->show($format); $type = 'image/' . $format; $length = ob_get_length(); $content = ob_get_clean(); // TODO: add more media headers $response = new Response($content, 201, array('content-type' => $type, 'content-length' => $length)); // Cache $cacheType = $this->filterManager->getOption($filter, "cache_type", false); if (false == $cacheType) { return $response; } $cacheType === "public" ? $response->setPublic() : $response->setPrivate(); $cacheExpires = $this->filterManager->getOption($filter, "cache_expires", "1 day"); $expirationDate = new \DateTime("+" . $cacheExpires); $maxAge = $expirationDate->format("U") - time(); if ($maxAge < 0) { throw new \InvalidArgumentException("Invalid cache expiration date"); } $response->setExpires($expirationDate); $response->setMaxAge($maxAge); return $response; } catch (\Exception $e) { ob_end_clean(); throw $e; } }