/** * {@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 $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); } }
/** * Applies $variationName filters on $image. * * Both variations configured in eZ (SiteAccess context) and LiipImagineBundle are used. * An eZ variation may have a "reference". * In that case, reference's filters are applied first, recursively (a reference may also have another reference). * Reference must be a valid variation name, configured in eZ or in LiipImagineBundle. * * @param BinaryInterface $image * @param string $variationName * * @return \Liip\ImagineBundle\Binary\BinaryInterface */ private function applyFilter(BinaryInterface $image, $variationName) { $filterConfig = $this->filterConfiguration->get($variationName); // If the variation has a reference, we recursively call this method to apply reference's filters. if (isset($filterConfig['reference']) && $filterConfig['reference'] !== IORepositoryResolver::VARIATION_ORIGINAL) { $image = $this->applyFilter($image, $filterConfig['reference']); } return $this->filterManager->applyFilter($image, $variationName); }
public function testApplyFilterSet() { $image = $this->getMockImage(); $thumbConfig = array('size' => array(180, 180), 'mode' => 'outbound'); $config = $this->getMockFilterConfiguration(); $config->expects($this->atLeastOnce())->method('get')->with('thumbnail')->will($this->returnValue(array('filters' => array('thumbnail' => $thumbConfig)))); $loader = $this->getMockLoader(); $loader->expects($this->once())->method('load')->with($image, $thumbConfig)->will($this->returnValue($image)); $filterManager = new FilterManager($config); $filterManager->addLoader('thumbnail', $loader); $this->assertSame($image, $filterManager->applyFilter($image, 'thumbnail')); }
/** * 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); } } } }
/** * 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 testThrowsIfNoPostProcessorAddedForFilterOnApplyFilter() { $originalContent = 'aContent'; $binary = new Binary($originalContent, 'image/png', 'png'); $thumbConfig = array('size' => array(180, 180), 'mode' => 'outbound'); $config = $this->createFilterConfigurationMock(); $config->expects($this->atLeastOnce())->method('get')->with('thumbnail')->will($this->returnValue(array('filters' => array('thumbnail' => $thumbConfig), 'post_processors' => array('foo' => array())))); $thumbConfig = array('size' => array(180, 180), 'mode' => 'outbound'); $image = $this->getMockImage(); $image->expects($this->once())->method('get')->will($this->returnValue($originalContent)); $imagineMock = $this->createImagineMock(); $imagineMock->expects($this->once())->method('load')->with($originalContent)->will($this->returnValue($image)); $loader = $this->getMockLoader(); $loader->expects($this->once())->method('load')->with($this->identicalTo($image), $thumbConfig)->will($this->returnArgument(0)); $filterManager = new FilterManager($config, $imagineMock, $this->getMockMimeTypeGuesser()); $filterManager->addLoader('thumbnail', $loader); $this->setExpectedException('InvalidArgumentException', 'Could not find post processor "foo"'); $filterManager->applyFilter($binary, '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); }
/** * Process submitted form. * * @param Form $form * @param FilterManager $lippImagineFilterManager * @param BinaryInterface $binary * @param $imageCropLiipImagineFilter * @param $downloadUri * @return JsonResponse */ private function processSubmittedForm(Form $form, FilterManager $lippImagineFilterManager, BinaryInterface $binary, $imageCropLiipImagineFilter, $downloadUri) { try { list($scalingWidth, $scalingHeight) = explode('x', $form->get('scaling')->getData()); $filteredBinary = $lippImagineFilterManager->applyFilter($binary, $imageCropLiipImagineFilter, array('filters' => array('thumbnail' => array('size' => array($scalingWidth, $scalingHeight)), 'crop' => array('start' => array($form->get('cropx')->getData(), $form->get('cropy')->getData()), 'size' => array($form->get('cropw')->getData(), $form->get('croph')->getData()))))); $this->container->get('liip_imagine.cache.manager')->store($filteredBinary, $downloadUri, $imageCropLiipImagineFilter); $message = 'form.submit.message'; } catch (\Exception $e) { $message = 'form.submit.error'; } return new JsonResponse(array('message' => $this->container->get('translator')->trans($message, array(), 'ImageCropBundle'))); }