Ejemplo n.º 1
0
 /**
  * Gets a resolver for the given filter.
  *
  * In case there is no specific resolver, but a default resolver has been configured, the default will be returned.
  *
  * @param string $filter
  *
  * @return ResolverInterface
  *
  * @throws \OutOfBoundsException If neither a specific nor a default resolver is available.
  */
 protected function getResolver($filter)
 {
     $config = $this->filterConfig->get($filter);
     $resolverName = empty($config['cache']) ? $this->defaultResolver : $config['cache'];
     if (!isset($this->resolvers[$resolverName])) {
         throw new \OutOfBoundsException(sprintf('Could not find resolver "%s" for "%s" filter type', $resolverName, $filter));
     }
     return $this->resolvers[$resolverName];
 }
Ejemplo n.º 2
0
 /**
  * Returns a loader previously attached to the given filter.
  *
  * @param string $filter
  *
  * @throws \InvalidArgumentException
  *
  * @return LoaderInterface
  */
 public function getLoader($filter)
 {
     $config = $this->filterConfig->get($filter);
     $loaderName = empty($config['data_loader']) ? $this->defaultLoader : $config['data_loader'];
     if (!isset($this->loaders[$loaderName])) {
         throw new \InvalidArgumentException(sprintf('Could not find data loader "%s" for "%s" filter type', $loaderName, $filter));
     }
     return $this->loaders[$loaderName];
 }
Ejemplo n.º 3
0
 private function getFilterDimension($name)
 {
     $filter = $this->filter->get($name);
     if (empty($filter)) {
         return $this->defaultFilterDimension;
     }
     $size = $filter['filters']['thumbnail']['size'];
     return vsprintf('%sx%s', $size);
 }
Ejemplo n.º 4
0
 public function get($filter)
 {
     $configuredVariations = $this->configResolver->getParameter('image_variations');
     if (!isset($configuredVariations[$filter])) {
         return parent::get($filter);
     }
     $filterConfig = array('cache' => 'ezpublish', 'data_loader' => 'ezpublish', 'reference' => isset($configuredVariations[$filter]['reference']) ? $configuredVariations[$filter]['reference'] : null, 'filters' => $this->getVariationFilters($filter, $configuredVariations), 'post_processors' => $this->getVariationPostProcessors($filter, $configuredVariations));
     return $filterConfig;
 }
Ejemplo n.º 5
0
 /**
  * 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);
 }
Ejemplo n.º 6
0
 /**
  * @param Image $image
  * @param $filter
  * @return string
  * @throws NonExistingFilterException
  */
 public function getImageSizeAttrs(Image $image, $filter)
 {
     $config = $this->liipFilterConfiguration->get($filter);
     $width = $image->getWidth();
     $height = $image->getHeight();
     if (isset($config['filters']['thumbnail'])) {
         $width = $config['filters']['thumbnail']['size'][0];
         $height = $config['filters']['thumbnail']['size'][1];
         if ($config['filters']['thumbnail']['mode'] === 'inset') {
             if ($image->getWidth() > $image->getHeight()) {
                 $height = floor($width / $image->getWidth() * $image->getHeight());
             }
             if ($image->getWidth() < $image->getHeight()) {
                 $width = floor($height / $image->getHeight() * $image->getWidth());
             }
         }
     }
     return strtr('width="%w%" height="%h%" alt="%a%"', ['%w%' => $width, '%h%' => $height, '%a%' => $image->getOriginalFileName()]);
 }
Ejemplo n.º 7
0
 /**
  * Get default image url with the given filter applied.
  *
  * @param string $filter
  *
  * @return string
  */
 public function getDefaultImageUrl($filter)
 {
     $config = $this->filterConfig->get($filter);
     $defaultImage = null;
     if (false == empty($config['default_image'])) {
         $defaultImage = $config['default_image'];
     } elseif (!empty($this->globalDefaultImage)) {
         $defaultImage = $this->globalDefaultImage;
     }
     return $defaultImage;
 }
 /**
  * @param ImageResourcesLoader $resourcesLoader
  * @param string $className
  * @param string $fieldName
  * @param string|null $layout
  *
  * @return array
  */
 private function addImagineFilter(ImageResourcesLoader $resourcesLoader, $className, $fieldName, $layout)
 {
     $imageOptions = $this->getImageResourceOptions($resourcesLoader->getConfig());
     $addedNames = [];
     foreach ($imageOptions['images'] as $image) {
         $filterName = $this->getImagineFilterName($className, $fieldName, $layout, $image);
         try {
             $this->filterConfiguration->get($filterName);
         } catch (NonExistingFilterException $e) {
             $this->filterConfiguration->set($filterName, $this->getImagineFilterConfig($imageOptions['filter'], $image));
             $addedNames[] = $filterName;
         }
     }
     return $addedNames;
 }
Ejemplo n.º 9
0
 /**
  * Returns a web accessible URL.
  *
  * @param string $path The path where the resolved file is expected.
  * @param string $filter The name of the imagine filter in effect.
  * @param bool $absolute Whether to generate an absolute URL or a relative path is accepted.
  *                       In case the resolver does not support relative paths, it may ignore this flag.
  *
  * @return string
  */
 public function generateUrl($path, $filter, $absolute = false)
 {
     $config = $this->filterConfig->get($filter);
     if (isset($config['format'])) {
         $pathinfo = pathinfo($path);
         // the extension should be forced and a directory is detected
         if ((!isset($pathinfo['extension']) || $pathinfo['extension'] !== $config['format']) && isset($pathinfo['dirname'])) {
             if ('\\' === $pathinfo['dirname']) {
                 $pathinfo['dirname'] = '';
             }
             $path = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . '.' . $config['format'];
         }
     }
     $params = array('path' => ltrim($path, '/'));
     return str_replace(urlencode($params['path']), urldecode($params['path']), $this->router->generate('_imagine_' . $filter, $params, $absolute));
 }
Ejemplo n.º 10
0
 public function testGetConfigSetViaConstructor()
 {
     $filterConfiguration = new FilterConfiguration(array('profile_photo' => array(), 'thumbnail' => array()));
     $this->assertInternalType('array', $filterConfiguration->get('profile_photo'));
     $this->assertInternalType('array', $filterConfiguration->get('thumbnail'));
 }
 /**
  * {@inheritdoc}
  */
 protected function getFileUrl($path, $filter)
 {
     $filterConf = $this->filterConfig->get($filter);
     $path = $this->changeFileExtension($path, $filterConf['format']);
     return parent::getFileUrl($path, $filter);
 }
 public function testGetUndefinedFilter()
 {
     $filterConfiguration = new FilterConfiguration();
     $this->setExpectedException('RuntimeException', 'Filter not defined: thumbnail');
     $filterConfiguration->get('thumbnail');
 }