liip/LiipImagineBundle — pull request #59 by neilferreira (2012-03-19 14:38:48+00:00)
Title: Issue #56 - Added a DbLoader data loader

First pull request ever, or open source submission for that matter so go easy.

I've added a DbLoader data loader, along with YML schema

Usage:

Creating an image, should be moved to a handler method at some point to just pass the filename and it returns an image object.

// The image path, can be from /tmp etc if it were from $_FILES
$img = $_SERVER['DOCUMENT_ROOT'] . 'wrx.jpg';

$imageInfo = getimagesize($img);
$imageContents = file_get_contents($img);

$image = new \Liip\ImagineBundle\Entity\LiipImage();

$image->setData($imageContents);
$image->setMimetype($imageInfo['mime']);
$image->setFilename('wrx.jpg');

$em = $this->getDoctrine()->getEntityManager();
$em->persist($image);
$em->flush();

echo $image->getId();

Now setup another filter inside your config.yml, using the data_loader of "db"

liip_imagine:
filter_sets:
my_thumb:
quality: 75
filters:
thumbnail: { size: [120, 90], mode: outbound }
db:
data_loader: db
quality: 75
filters:
relative_resize: { widen: 400, mode: outbound }

From your view or controller you can now call imagine_filter on your image object, ie:

$repository = $this->getDoctrine()->getRepository('LiipImagineBundle:LiipImage');
$image = $repository->find([id]);

$source = $this->get('liip_imagine.cache.manager')->getBrowserPath($image->getUniqueIdentifier(), 'db');

echo $source;

As you can see, every image has a unique identifer (md5 of time(), filename) with the file extention on the end.

If the image cannot be found then the 404 exception will be thrown.