public function testFindWithoutLoader()
 {
     $config = $this->getMockFilterConfiguration();
     $config->expects($this->once())->method('get')->with('thumbnail')->will($this->returnValue(array('size' => array(180, 180), 'mode' => 'outbound', 'data_loader' => null)));
     $dataManager = new DataManager($config);
     $this->setExpectedException('InvalidArgumentException', 'Could not find data loader for "thumbnail" filter type');
     $dataManager->find('thumbnail', 'cats.jpeg');
 }
 /**
  * @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());
 }
Exemple #3
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);
 }
 /**
  * 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 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;
 }
 /**
  * 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);
     }
 }
 /**
  * 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'])))));
 }
Exemple #10
0
    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);
    }
 public function testShouldReturnBinaryWithLoaderContentAndGuessedFormatOnFind()
 {
     $content = 'theImageBinaryContent';
     $mimeType = 'image/png';
     $expectedFormat = 'png';
     $loader = $this->getMockLoader();
     $loader->expects($this->once())->method('find')->will($this->returnValue($content));
     $mimeTypeGuesser = $this->getMockMimeTypeGuesser();
     $mimeTypeGuesser->expects($this->once())->method('guess')->with($content)->will($this->returnValue($mimeType));
     $extensionGuesser = $this->getMockExtensionGuesser();
     $extensionGuesser->expects($this->once())->method('guess')->with($mimeType)->will($this->returnValue($expectedFormat));
     $config = $this->createFilterConfigurationMock();
     $config->expects($this->once())->method('get')->with('thumbnail')->will($this->returnValue(array('size' => array(180, 180), 'mode' => 'outbound', 'data_loader' => null)));
     $dataManager = new DataManager($mimeTypeGuesser, $extensionGuesser, $config, 'default');
     $dataManager->addLoader('default', $loader);
     $binary = $dataManager->find('thumbnail', 'cats.jpeg');
     $this->assertInstanceOf('Liip\\ImagineBundle\\Model\\Binary', $binary);
     $this->assertEquals($expectedFormat, $binary->getFormat());
 }
Exemple #12
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);
 }