/** * 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 * * @throws \RuntimeException * @throws BadRequestHttpException * * @return RedirectResponse */ public function filterAction(Request $request, $path, $filter) { // decoding special characters and whitespaces from path obtained from url $path = urldecode($path); try { if (!$this->cacheManager->isStored($path, $filter)) { try { $binary = $this->dataManager->find($filter, $path); } catch (NotLoadableException $e) { if ($defaultImageUrl = $this->dataManager->getDefaultImageUrl($filter)) { return new RedirectResponse($defaultImageUrl); } throw new NotFoundHttpException('Source image could not be found', $e); } $this->cacheManager->store($this->filterManager->applyFilter($binary, $filter), $path, $filter); } return new RedirectResponse($this->cacheManager->resolve($path, $filter), 301); } catch (NonExistingFilterException $e) { $message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $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"', $path, $filter, $e->getMessage()), 0, $e); } }
/** * 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 * * @throws \RuntimeException * @throws BadRequestHttpException * * @return RedirectResponse */ public function filterAction(Request $request, $path, $filter) { try { if (!$this->cacheManager->isStored($path, $filter)) { try { $binary = $this->dataManager->find($filter, $path); } catch (NotLoadableException $e) { if ($defaultImageUrl = $this->dataManager->getDefaultImageUrl($filter)) { return new RedirectResponse($defaultImageUrl); } throw new NotFoundHttpException('Source image could not be found', $e); } $this->cacheManager->store( $this->filterManager->applyFilter($binary, $filter), $path, $filter ); } return new RedirectResponse($this->cacheManager->resolve($path, $filter), 301); } catch (RuntimeException $e) { throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $path, $filter, $e->getMessage()), 0, $e); } }
/** * {@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); }
/** * 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); } } } }
function it_renders_a_product_with_an_image($templating, ProductInterface $blender, AttributeGroupInterface $media, AttributeInterface $mainImage, ProductValueInterface $productValue, FileInfoInterface $fileInfo, CacheManager $cacheManager) { $blender->getAttributes()->willReturn([$mainImage]); $blender->getValue("main_image", "en_US", "ecommerce")->willReturn($productValue); $productValue->getMedia()->willReturn($fileInfo); $fileInfo->getKey()->willReturn('fookey'); $cacheManager->isStored('fookey', 'thumbnail')->willReturn(true); $mainImage->getGroup()->willReturn($media); $media->getLabel()->willReturn('Media'); $mainImage->getCode()->willReturn('main_image'); $mainImage->getAttributeType()->willReturn('pim_catalog_image'); $templating->render(self::TEMPLATE_NAME, ['product' => $blender, 'locale' => 'en_US', 'scope' => 'ecommerce', 'groupedAttributes' => ['Media' => ['main_image' => $mainImage]], 'imageAttributes' => ['main_image' => $mainImage], 'uploadDir' => '/tmp/' . DIRECTORY_SEPARATOR, 'customFont' => null])->shouldBeCalled(); $this->render($blender, 'pdf', ['locale' => 'en_US', 'scope' => 'ecommerce']); }
public function getFilteredPath(File $file, $filter) { return $this->cacheManager->isStored('uploaded/' . $file->getId() . '.' . $file->getExtention(), $filter) ? $this->cacheManager->resolve('uploaded/' . $file->getId() . '.' . $file->getExtention(), $filter) : $this->router->generate('digi_file_filter', array('file' => $file->getId() . '.' . $file->getExtention(), 'filter' => $filter), true); }