/** * @param Request $request * @param string $filename * * @throws NotFoundHttpException If media is not found * * @return Response */ public function showAction(Request $request, $filename) { if (!$this->filesystem->has($filename)) { throw new NotFoundHttpException(sprintf('Media "%s" not found', $filename)); } $response = new Response($content = $this->filesystem->read($filename)); $mime = $this->filesystem->mimeType($filename); if (($filter = $request->query->get('filter')) && null !== $mime && 0 === strpos($mime, 'image')) { try { $cachePath = $this->cacheManager->resolve($request, $filename, $filter); if ($cachePath instanceof Response) { $response = $cachePath; } else { $image = $this->imagine->load($content); $response = $this->filterManager->get($request, $filter, $image, $filename); $response = $this->cacheManager->store($response, $cachePath, $filter); } } catch (\RuntimeException $e) { if (0 === strpos($e->getMessage(), 'Filter not defined')) { throw new HttpException(404, sprintf('The filter "%s" cannot be found', $filter), $e); } throw $e; } } if ($mime) { $response->headers->set('Content-Type', $mime); } return $response; }
/** * {@inheritdoc} */ public function getImageUrl($fileType, $filter) { $fileKey = sprintf('%s_default_image', $fileType); if (!$this->cacheManager->isStored($fileKey, $filter)) { $binary = $this->getImageBinary($fileType); $this->cacheManager->store($this->filterManager->applyFilter($binary, $filter), $fileKey, $filter); } return $this->cacheManager->resolve($fileKey, $filter); }
/** * @param ImagePreview $imagePreviewData * @return string */ private function resize(ImagePreview $imagePreviewData) { $filter = 'preview'; if (!$this->cacheManager->isStored($imagePreviewData->getTargetName(), $filter)) { $binary = $this->dataManager->find($filter, $imagePreviewData->getSourceUri()); $filteredBinary = $this->filterManager->applyFilter($binary, $filter, ['filters' => ['thumbnail' => ['size' => [$imagePreviewData->getWidth(), $imagePreviewData->getHeight()]]]]); $this->cacheManager->store($filteredBinary, $imagePreviewData->getTargetName(), $filter); } return $this->assetsResolver->uriToPath($this->targetBasePath . $imagePreviewData->getTargetName()); }
/** * @param string $path * @param string $filter * @param Image $image * * @return string */ public function getFormattedImage($path, $filter, Image $image = null) { if (!$this->cacheManager->isStored($path, $filter)) { $config = $this->filterManager->getFilterConfiguration()->get($filter); if (isset($config['filters']['focused_crop'])) { $config['filters']['focused_crop']['object'] = $image; } $binary = $this->dataManager->find($filter, $path); $binary = $this->filterManager->applyFilter($binary, $filter, $config); $this->cacheManager->store($binary, $path, $filter); } return $this->cacheManager->resolve($path, $filter); }
/** * @param string $imageData * @param string $filter * @throws \Exception * @return bool|string */ public function handle($imageData, $filter) { try { $binary = $this->createBinary($imageData); $pictureName = time() . '.' . $binary->getFormat(); $this->imagineCache->store($this->filterManager->applyFilter($binary, $filter), $pictureName, $filter); $this->imagineCache->resolve($pictureName, $filter); return $this->getUploadDir() . $filter . '/' . $pictureName; } catch (\Exception $e) { $this->error = $e->getMessage(); } return false; }
/** * 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 $path * @param string $filter * * @return Response */ public function filterAction(Request $request, $path, $filter) { $targetPath = $this->cacheManager->resolve($request, $path, $filter); if ($targetPath instanceof Response) { return $targetPath; } $image = $this->dataManager->find($filter, $path); $response = $this->filterManager->get($request, $filter, $image, $path); if ($targetPath) { $response = $this->cacheManager->store($response, $targetPath, $filter); } return $response; }
/** * 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); } }
/** * Generate media thumbnails cache used by the PDF document * * @param ProductInterface $product * @param AttributeInterface[] $imageAttributes * @param string $locale * @param string $scope */ protected function generateThumbnailsCache(ProductInterface $product, array $imageAttributes, $locale, $scope) { foreach ($imageAttributes as $attribute) { $media = $product->getValue($attribute->getCode(), $locale, $scope)->getMedia(); if (null !== $media && null !== $media->getKey()) { $path = $media->getKey(); $filter = 'thumbnail'; if (!$this->cacheManager->isStored($path, $filter)) { $binary = $this->dataManager->find($filter, $path); $this->cacheManager->store($this->filterManager->applyFilter($binary, $filter), $path, $filter); } } } }
public function testFallbackToDefaultResolver() { $response = new Response('', 200); $request = new Request(); $resolver = $this->getMockResolver(); $resolver->expects($this->once())->method('resolve')->with($request, 'cats.jpeg', 'thumbnail')->will($this->returnValue('/thumbs/cats.jpeg')); $resolver->expects($this->once())->method('store')->with($response, '/thumbs/cats.jpeg', 'thumbnail')->will($this->returnValue($response)); $resolver->expects($this->once())->method('remove')->with('/thumbs/cats.jpeg', 'thumbnail')->will($this->returnValue(true)); $config = $this->getMockFilterConfiguration(); $config->expects($this->exactly(3))->method('get')->with('thumbnail')->will($this->returnValue(array('size' => array(180, 180), 'mode' => 'outbound', 'cache' => null))); $cacheManager = new CacheManager($config, $this->getMockRouter(), $this->fixturesDir . '/assets', 'default'); $cacheManager->addResolver('default', $resolver); // Resolve fallback to default resolver $this->assertEquals('/thumbs/cats.jpeg', $cacheManager->resolve($request, 'cats.jpeg', 'thumbnail')); // Store fallback to default resolver $this->assertEquals($response, $cacheManager->store($response, '/thumbs/cats.jpeg', 'thumbnail')); // Remove fallback to default resolver $this->assertTrue($cacheManager->remove('/thumbs/cats.jpeg', 'thumbnail')); }
/** * 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 (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) { throw new NotFoundHttpException(sprintf('Source image could not be found for path "%s" and filter "%s"', $path, $filter), $e); } $cachePrefix = 'rc/' . $hash; $this->cacheManager->store($this->filterManager->applyFilter($binary, $filter, array('filters' => $filters)), $cachePrefix . '/' . $path, $filter); return new RedirectResponse($this->cacheManager->resolve($cachePrefix . '/' . $path, $filter), 301); } 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 testFallbackToDefaultResolver() { $binary = new Binary('aContent', 'image/png', 'png'); $resolver = $this->createResolverMock(); $resolver ->expects($this->once()) ->method('resolve') ->with('cats.jpeg', 'thumbnail') ->will($this->returnValue('/thumbs/cats.jpeg')) ; $resolver ->expects($this->once()) ->method('store') ->with($binary, '/thumbs/cats.jpeg', 'thumbnail') ; $resolver ->expects($this->once()) ->method('remove') ->with(array('/thumbs/cats.jpeg'), array('thumbnail')) ->will($this->returnValue(true)) ; $config = $this->createFilterConfigurationMock(); $config ->expects($this->exactly(3)) ->method('get') ->with('thumbnail') ->will($this->returnValue(array( 'size' => array(180, 180), 'mode' => 'outbound', 'cache' => null, ))) ; $cacheManager = new CacheManager( $config, $this->createRouterMock(), new Signer('secret'), $this->createEventDispatcherMock() ); $cacheManager->addResolver('default', $resolver); // Resolve fallback to default resolver $this->assertEquals('/thumbs/cats.jpeg', $cacheManager->resolve('cats.jpeg', 'thumbnail')); $cacheManager->store($binary, '/thumbs/cats.jpeg', 'thumbnail'); // Remove fallback to default resolver $cacheManager->remove('/thumbs/cats.jpeg', 'thumbnail'); }
/** * Applies the given filter to the given picture * @param Picture $picture * @param $filter */ public function filter(Picture $picture, $filter) { $binary = $this->dataManager->find($filter, $picture->getId()); $thumb = $this->filterManager->applyFilter($binary, $filter); $this->cacheManager->store($thumb, $picture->getId(), $filter); }