/**
  * @param ObjectEvent $event
  */
 public function onPostSerialize(ObjectEvent $event)
 {
     // getSubscribedEvents doesn't seem to support parent classes
     if (!$event->getObject() instanceof MediaInterface) {
         return;
     }
     $provider = $this->pool->getProvider($event->getObject()->getProvider());
     if ($provider->getName() == 'image') {
         $reference = $provider->getThumb($event->getObject());
         $groups = $event->getContext()->attributes->get('groups');
         if (!$groups instanceof None && in_array('detail', $groups->get())) {
             $filters = array_keys($this->filterConfig->all());
         } else {
             $filters = ['medialibrary'];
         }
         $variants = [];
         foreach ($filters as $filter) {
             if ($event->getObject()->getContentType() == 'image/svg+xml') {
                 $variants[$filter] = $provider->getUrl($event->getObject());
             } else {
                 $variants[$filter] = $this->cacheManager->getBrowserPath($reference, $filter);
             }
         }
         $event->getVisitor()->addData('images', $variants);
     }
     $event->getVisitor()->addData('original', $provider->getUrl($event->getObject()));
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options = array())
 {
     $filters = array();
     foreach (array_keys($this->filterConfiguration->all()) as $filter) {
         $filters[$filter] = sprintf('sylius.form.imagine_block.filter.%s', $filter);
     }
     $builder->add('publishable', null, array('label' => 'sylius.form.imagine_block.publishable'))->add('publishStartDate', 'datetime', array('label' => 'sylius.form.imagine_block.publish_start_date', 'empty_value' => array('year' => '-', 'month' => '-', 'day' => '-'), 'time_widget' => 'text'))->add('publishEndDate', 'datetime', array('label' => 'sylius.form.imagine_block.publish_end_date', 'empty_value' => array('year' => '-', 'month' => '-', 'day' => '-'), 'time_widget' => 'text'))->add('parentDocument', null, array('label' => 'sylius.form.imagine_block.parent'))->add('name', 'text', array('label' => 'sylius.form.imagine_block.internal_name'))->add('label', 'text', array('label' => 'sylius.form.imagine_block.label', 'required' => false))->add('linkUrl', 'text', array('label' => 'sylius.form.imagine_block.link_url', 'required' => false))->add('filter', 'choice', array('choices' => $filters, 'label' => 'sylius.form.imagine_block.filter', 'required' => false))->add('image', 'cmf_media_image', array('label' => 'sylius.form.imagine_block.image', 'attr' => array('class' => 'imagine-thumbnail'), 'required' => false));
 }
Example #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);
 }
 /**
  * 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];
 }
    protected function createFilterConfiguration()
    {
        $config = new FilterConfiguration();
        $config->set('thumbnail', array(
            'size' => array(180, 180),
            'mode' => 'outbound',
        ));

        return $config;
    }
 public function testGetVariationNotStoredHavingReferences()
 {
     $originalPath = 'foo/bar/image.jpg';
     $variationName = 'my_variation';
     $reference1 = 'reference1';
     $reference2 = 'reference2';
     $configVariation = array('reference' => $reference1);
     $configReference1 = array('reference' => $reference2);
     $configReference2 = array();
     $this->filterConfiguration->set($variationName, $configVariation);
     $this->filterConfiguration->set($reference1, $configReference1);
     $this->filterConfiguration->set($reference2, $configReference2);
     $imageId = '123-45';
     $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
     $field = new Field(array('value' => $imageValue));
     $expectedUrl = "http://localhost/foo/bar/image_{$variationName}.jpg";
     $this->ioResolver->expects($this->once())->method('isStored')->with($originalPath, $variationName)->will($this->returnValue(false));
     $this->logger->expects($this->once())->method('debug');
     $binary = $this->getMock('\\Liip\\ImagineBundle\\Binary\\BinaryInterface');
     $this->dataLoader->expects($this->once())->method('find')->with($originalPath)->will($this->returnValue($binary));
     // Filter manager is supposed to be called 3 times to generate references, and then passed variation.
     $this->filterManager->expects($this->at(0))->method('applyFilter')->with($binary, $reference2)->will($this->returnValue($binary));
     $this->filterManager->expects($this->at(1))->method('applyFilter')->with($binary, $reference1)->will($this->returnValue($binary));
     $this->filterManager->expects($this->at(2))->method('applyFilter')->with($binary, $variationName)->will($this->returnValue($binary));
     $this->ioResolver->expects($this->once())->method('store')->with($binary, $originalPath, $variationName);
     $this->ioResolver->expects($this->once())->method('resolve')->with($originalPath, $variationName)->will($this->returnValue($expectedUrl));
     $expected = new ImageVariation(array('name' => $variationName, 'fileName' => "image_{$variationName}.jpg", 'dirPath' => 'http://localhost/foo/bar', 'uri' => $expectedUrl, 'imageId' => $imageId));
     $this->assertEquals($expected, $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName));
 }
Example #7
0
    /**
     * @param string|string[]|null $paths
     * @param string|string[]|null $filters
     *
     * @return void
     */
    public function remove($paths = null, $filters = null)
    {
        if (null === $filters) {
            $filters = array_keys($this->filterConfig->all());
        }
        if (!is_array($filters)) {
            $filters = array($filters);
        }
        if (!is_array($paths)) {
            $paths = array($paths);
        }

        $paths = array_filter($paths);
        $filters = array_filter($filters);

        $mapping = new \SplObjectStorage();
        foreach ($filters as $filter) {
            $resolver = $this->getResolver($filter);

            $list = isset($mapping[$resolver]) ? $mapping[$resolver] : array();

            $list[] = $filter;

            $mapping[$resolver] = $list;
        }

        foreach ($mapping as $resolver) {
            $resolver->remove($paths, $mapping[$resolver]);
        }
    }
 /**
  * @param string[] $paths The paths where the original files are expected to be.
  * @param string[] $filters The imagine filters in effect.
  *
  * @return void
  */
 public function remove(array $paths, array $filters)
 {
     // TODO: $paths may be empty, meaning that all generated images corresponding to $filters need to be removed.
     if (empty($filters)) {
         $filters = array_keys($this->filterConfiguration->all());
     }
     foreach ($paths as $path) {
         foreach ($filters as $filter) {
             $filteredImagePath = $this->getFilePath($path, $filter);
             if (!$this->ioService->exists($filteredImagePath)) {
                 continue;
             }
             $binaryFile = $this->ioService->loadBinaryFile($filteredImagePath);
             $this->ioService->deleteBinaryFile($binaryFile);
         }
     }
 }
 /**
  * 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);
 }
 /**
  * @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()]);
 }
Example #11
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;
 }
 /**
  * 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));
 }
 /**
  * @param string[] $paths The paths where the original files are expected to be.
  * @param string[] $filters The imagine filters in effect.
  */
 public function remove(array $paths, array $filters)
 {
     if (empty($filters)) {
         $filters = array_keys($this->filterConfiguration->all());
     }
     if (empty($paths)) {
         $this->variationPurger->purge($filters);
     }
     foreach ($paths as $path) {
         foreach ($filters as $filter) {
             $filteredImagePath = $this->getFilePath($path, $filter);
             if (!$this->ioService->exists($filteredImagePath)) {
                 continue;
             }
             $binaryFile = $this->ioService->loadBinaryFile($filteredImagePath);
             $this->ioService->deleteBinaryFile($binaryFile);
         }
     }
 }
 public function all()
 {
     return $this->configResolver->getParameter('image_variations') + parent::all();
 }
Example #16
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'));
 }
 public function testGetUndefinedFilter()
 {
     $filterConfiguration = new FilterConfiguration();
     $this->setExpectedException('RuntimeException', 'Filter not defined: thumbnail');
     $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);
 }