Skip to content

Commit

Permalink
Added GridFS loader
Browse files Browse the repository at this point in the history
  • Loading branch information
jdewit committed Aug 16, 2012
1 parent 455fe60 commit 86c166a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -34,6 +34,7 @@ public function getConfigTreeBuilder()
->scalarNode('cache_base_path')->defaultValue('')->end()
->scalarNode('data_loader')->defaultValue('filesystem')->end()
->scalarNode('controller_action')->defaultValue('liip_imagine.controller:filterAction')->end()
->scalarNode('image_class')->defaultNull()->end()
->arrayNode('formats')
->defaultValue(array())
->prototype('scalar')->end()
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/LiipImagineExtension.php
Expand Up @@ -45,6 +45,8 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('liip_imagine.controller_action', $config['controller_action']);

$container->setParameter('liip_imagine.image_class', $config['image_class']);

if ('2' == Kernel::MAJOR_VERSION && '0' == Kernel::MINOR_VERSION) {
$container->removeDefinition('liip_imagine.cache.clearer');
}
Expand Down
60 changes: 60 additions & 0 deletions Imagine/Data/Loader/GridFSLoader.php
@@ -0,0 +1,60 @@
<?php

namespace Liip\ImagineBundle\Imagine\Data\Loader;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use Imagine\Image\ImagineInterface;
use Doctrine\ODM\MongoDB\DocumentManager;

class GridFSLoader implements LoaderInterface
{
/**
* @var Imagine\Image\ImagineInterface
*/
protected $imagine;

/**
* @var Doctrine\ODM\MongoDB\DocumentManager
*/
protected $dm;

/**
* @var Image Class
*/
protected $class;

/**
* Constructs
*
* @param ImagineInterface $imagine
* @param DocumentManager $dm
* @param string Image class
*/
public function __construct(ImagineInterface $imagine, DocumentManager $dm, $class)
{
$this->imagine = $imagine;
$this->dm = $dm;
$this->class = $class;
}

/**
* @param string $id
*
* @return Imagine\Image\ImageInterface
*/
public function find($id)
{
$image = $this->dm
->getRepository($this->class)
->findAll()
->getCollection()
->findOne(array("_id" => new \MongoId($id)));

if (!$image) {
throw new NotFoundHttpException(sprintf('Source image not found with id "%s"', $id));
}

return $this->imagine->load($image['file']->getBytes());
}
}
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -443,6 +443,23 @@ services:
- { name: 'liip_imagine.data.loader', loader: 'stream.profile_photos' }
```

### GridFSLoader
Load your images from mongodb
``` yaml
liip_imagine:
image_class: Application\ImageBundle\Document\Image
filter_sets:
my_special_style:
data_loader: grid_fs
filters:
my_custom_filter: { }
```

Reference the image by its id
``` jinja
<img src="{{ image.id | imagine_filter('my_thumb') }}" />
```

## Extending the image loader with data transformers

You can extend a custom data loader to support virtually any file type using transformers.
Expand Down
11 changes: 9 additions & 2 deletions Resources/config/imagine.xml
Expand Up @@ -144,6 +144,13 @@
<argument>%liip_imagine.data_root%</argument>
</service>

<service id="liip_imagine.data.loader.grid_fs" class="Liip\ImagineBundle\Imagine\Data\Loader\GridFSLoader">
<tag name="liip_imagine.data.loader" loader="grid_fs" />
<argument type="service" id="liip_imagine" />
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
<argument>%liip_imagine.image_class%</argument>
</service>

<!-- Cache resolver -->

<service id="liip_imagine.cache.resolver.web_path" class="%liip_imagine.cache.resolver.web_path.class%">
Expand All @@ -160,12 +167,12 @@
</service>

<!-- Cache Clearer -->

<service id="liip_imagine.cache.clearer" class="%liip_imagine.cache.clearer.class%">
<tag name="kernel.cache_clearer" />
<argument type="service" id="liip_imagine.cache.manager" />
<argument>%liip_imagine.cache_prefix%</argument>
</service>

</services>
</container>

0 comments on commit 86c166a

Please sign in to comment.