/**
  * @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);
 }
Esempio n. 3
0
 /**
  * @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());
 }
 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'));
 }
 /**
  * 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;
 }
Esempio n. 6
0
 /**
  * @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);
 }
Esempio n. 7
0
 /**
  * @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 testFilterActionLive()
 {
     $router = $this->getMockRouter();
     $router->expects($this->any())->method('generate')->with('_imagine_thumbnail', array('path' => 'cats.jpeg'), false)->will($this->returnValue('/media/cache/thumbnail/cats.jpeg'));
     $dataLoader = new FileSystemLoader($this->imagine, array(), $this->dataDir);
     $dataManager = new DataManager($this->configuration, 'filesystem');
     $dataManager->addLoader('filesystem', $dataLoader);
     $filterLoader = new ThumbnailFilterLoader();
     $filterManager = new FilterManager($this->configuration);
     $filterManager->addLoader('thumbnail', $filterLoader);
     $webPathResolver = new WebPathResolver($this->filesystem);
     $cacheManager = new CacheManager($this->configuration, $router, $this->webRoot, 'web_path');
     $cacheManager->addResolver('web_path', $webPathResolver);
     $controller = new ImagineController($dataManager, $filterManager, $cacheManager);
     $request = Request::create('/media/cache/thumbnail/cats.jpeg');
     $response = $controller->filterAction($request, 'cats.jpeg', 'thumbnail');
     $targetPath = realpath($this->webRoot) . '/media/cache/thumbnail/cats.jpeg';
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\Response', $response);
     $this->assertEquals(201, $response->getStatusCode());
     $this->assertTrue(file_exists($targetPath));
     $this->assertNotEmpty(file_get_contents($targetPath));
     return $controller;
 }
 /**
  * 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);
             }
         }
     }
 }
Esempio n. 12
0
 /**
  * @param $entity
  * @param $type
  * @param  null        $filter
  * @return array|mixed
  */
 public function getImageOptions($entity, $type, $filter = null)
 {
     $options = $entity->{'get' . ucfirst($type) . 'Options'}();
     $options = json_decode($options, true);
     if (!$options || (array_key_exists('height', $options) && $options['height'] < 1 || array_key_exists('width', $options) && $options['width'] < 1)) {
         $defaultConfig = ['height' => 0, 'width' => 0, 'x' => 0, 'y' => 0];
         if ($filter) {
             $config = $this->filterManager->getFilterConfiguration()->get($filter);
             if (isset($config['filters']) && isset($config['filters']['crop']) && isset($config['filters']['crop']['size'])) {
                 $size = $config['filters']['crop']['size'];
                 $defaultConfig['height'] = $size[1];
                 $defaultConfig['width'] = $size[0];
             }
             if (isset($config['filters']) && isset($config['filters']['crop']) && isset($config['filters']['crop']['start'])) {
                 $start = $config['filters']['crop']['start'];
                 $defaultConfig['x'] = $start[0];
                 $defaultConfig['y'] = $start[1];
             }
         }
         return $defaultConfig;
     }
     return $options;
 }
 /**
  * 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);
     }
 }
 /**
  * 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')));
 }
 /**
  * Transform the file
  *
  * @param array $data
  *
  * @return \Liip\ImagineBundle\Binary\BinaryInterface
  */
 public function transformFile(array $data)
 {
     return $this->filterManager->apply($this->dataManager->find('original', $data['filename']), array('filters' => array('crop' => array('start' => array($data['x'], $data['y']), 'size' => array($data['width'], $data['height'])))));
 }
 public function testApplyPostProcessorsWhenNotDefined()
 {
     $binary = $this->getMock('Liip\\ImagineBundle\\Binary\\BinaryInterface');
     $filterManager = new FilterManager($this->createFilterConfigurationMock(), $this->createImagineMock(), $this->getMockMimeTypeGuesser());
     $this->assertSame($binary, $filterManager->applyPostProcessors($binary, array()));
 }
Esempio n. 17
0
 /**
  * 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);
 }