Install Dependencies:
composer require avro/image-bundle liip/imagine-bundle doctrine/mongodb-odm-bundle
Ensure DoctrineMongoDBBundle and LiipImagineBundle are enabled in AppKernel.php.
Enable Bundle:
// app/AppKernel.php
$bundles[] = new Avro\ImageBundle\AvroImageBundle();
Add Routing:
# app/config/routing.yml
avro_image:
resource: "@AvroImageBundle/Resources/config/routing/routing.xml"
First Use Case:
Create a document implementing ImageObjectInterface (e.g., Product):
use Avro\ImageBundle\Model\ImageObjectInterface;
class Product implements ImageObjectInterface {
// ...
public function getDocumentName() { return 'product'; }
}
Upload an Image:
Use the provided controller or create a custom form handler to upload files to GridFS via the Image document.
Uploading:
Image document to store file metadata (e.g., filename, mimetype, path).$image = new \Avro\ImageBundle\Document\Image();
$image->setFile($uploadedFile);
$dm->persist($image);
$dm->flush();
Associating Images with Documents:
Image documents in your model (e.g., Product):
$product->addImage($image);
$dm->persist($product);
$dm->flush();
Rendering:
{{ carousel_render('twitter', product.images) }}
Configuration Reference).Resizing with LiipImagine:
config.yml:
liip_imagine:
filters:
thumbnail:
size: [100, 100]
mode: outbound
{{ image_filter(image, 'thumbnail') }}
Batch Processing:
prePersist) to auto-generate thumbnails or metadata.Forms:
Use Symfony’s FileType field with custom validation to ensure only images are uploaded.
$builder->add('images', 'file', [
'multiple' => true,
'mime_types' => ['image/jpeg', 'image/png'],
]);
GridFS Optimization:
php app/console doctrine:mongodb:gridfs:info.Caching: Cache rendered carousels/galleries in Twig or Varnish to reduce DB load.
MongoDB GridFS Limitations:
mongodb.yml.ImageObjectInterface:
getDocumentName() will break routing and template rendering.images property is an ArrayCollection to avoid serialization issues.LiipImagine Dependencies:
image_filter in templates.app/console cache:clear) after adding new filters.Routing Conflicts:
/image/upload) may clash with existing routes. Override in routing.yml if needed.Assetic Assets:
assets:install and assetic:dump are run post-install.--watch in development to auto-recompile assets.Upload Failures:
GridFS storage with:
php app/console doctrine:mongodb:gridfs:list
Template Errors:
AvroImageBundle::carousel.html.twig).{{ dump(carousel) }} to inspect passed variables.Performance:
explain() to optimize slow loads.LiipImagineBundle's cache to avoid regenerating thumbnails.Custom Carousels:
AcmeBundle:Carousel:my_carousel.html.twig and reference them in config:
avro_image:
carousels:
my_carousel:
template: AcmeBundle:Carousel:my_carousel.html.twig
Image Validation:
Image document to add custom validation (e.g., max file size):
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\File(maxSize="2M")
*/
protected $file;
Event Listeners:
prePersist to auto-generate metadata (e.g., width, height):
$dm->getEventManager()->addEventListener(
\Doctrine\ODM\MongoDB\Events::prePersist,
function ($event) {
$image = $event->getDocument();
if ($image instanceof \Avro\ImageBundle\Document\Image) {
$image->setDimensions(getimagesize($image->getPath()));
}
}
);
Alternative Storage:
Image document’s storage logic (requires custom File handling).How can I help you explore Laravel packages today?