Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GridFS Loader #99

Merged
merged 2 commits into from
Aug 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Imagine/Data/Loader/GridFSLoader.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,32 @@ services:
- { name: 'liip_imagine.data.loader', loader: 'stream.profile_photos' }
```

### GridFSLoader
Load your images from mongodb
``` yaml
liip_imagine:
filter_sets:
my_special_style:
data_loader: grid_fs
filters:
my_custom_filter: { }
```

Add loader to your services
``` xml
<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>Application\ImageBundle\Document\Image</argument>
</service>
```

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
4 changes: 2 additions & 2 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,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>