/** * This action applies a given filter to a given image, optionally saves the image and outputs it to the browser at the same time. * * @param Request $request * @param string $hash * @param string $path * @param string $filter * * @throws \RuntimeException * @throws BadRequestHttpException * * @return RedirectResponse */ public function filterRuntimeAction(Request $request, $hash, $path, $filter) { try { $filters = $request->query->get('filters', array()); if (!is_array($filters)) { throw new NotFoundHttpException(sprintf('Filters must be an array. Value was "%s"', $filters)); } if (true !== $this->signer->check($hash, $path, $filters)) { throw new BadRequestHttpException(sprintf('Signed url does not pass the sign check for path "%s" and filter "%s" and runtime config %s', $path, $filter, json_encode($filters))); } try { $binary = $this->dataManager->find($filter, $path); } catch (NotLoadableException $e) { if ($defaultImageUrl = $this->dataManager->getDefaultImageUrl($filter)) { return new RedirectResponse($defaultImageUrl); } throw new NotFoundHttpException(sprintf('Source image could not be found for path "%s" and filter "%s"', $path, $filter), $e); } $rcPath = $this->cacheManager->getRuntimePath($path, $filters); $this->cacheManager->store($this->filterManager->applyFilter($binary, $filter, array('filters' => $filters)), $rcPath, $filter); return new RedirectResponse($this->cacheManager->resolve($rcPath, $filter), 301); } catch (NonExistingFilterException $e) { $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $hash . '/' . $path, $e->getMessage()); if (null !== $this->logger) { $this->logger->debug($message); } throw new NotFoundHttpException($message, $e); } catch (RuntimeException $e) { throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $hash . '/' . $path, $filter, $e->getMessage()), 0, $e); } }
public function testUseDefaultFilterImageUsedIfImageNotFound() { $loader = $this->getMockLoader(); $defaultFilterImage = 'cats.jpeg'; $config = $this->createFilterConfigurationMock(); $config ->expects($this->once()) ->method('get') ->with('thumbnail') ->will($this->returnValue(array( 'default_image' => $defaultFilterImage, ))) ; $mimeTypeGuesser = $this->getMockMimeTypeGuesser(); $mimeTypeGuesser ->expects($this->never()) ->method('guess') ; $dataManager = new DataManager($mimeTypeGuesser, $this->getMockExtensionGuesser(), $config, 'default', null); $dataManager->addLoader('default', $loader); $defaultImage = $dataManager->getDefaultImageUrl('thumbnail'); $this->assertEquals($defaultImage, $defaultFilterImage); }