Install Dependencies
Ensure LiipImagineBundle and VichUploaderBundle are installed and configured in your Symfony 2.8+ app.
composer require liip/imagine-bundle vich/uploader-bundle
Bundle Registration
Add to config/bundles.php:
Anacona16\ImageCropBundle\Anacona16ImageCropBundle::class => ['all' => true],
First Use Case Extend a form type to enable cropping:
use Anacona16\ImageCropBundle\Form\Type\ImageCropType;
$builder->add('image', ImageCropType::class, [
'label' => 'Profile Image',
'required' => false,
'crop' => true, // Enable cropping
'scale' => 0.8, // Optional: Scale factor (0-1)
]);
Template Integration Use the provided Twig extension to render the cropper:
{{ form_widget(form.image) }}
{{ form_row(form.image) }}
{% if form.image.vars.crop %}
{{ form_widget(form.image.crop) }}
{% endif %}
Form Submission
Handle uploads via VichUploaderBundle (e.g., preUpload, postUpload events).
// In your entity
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[Vich\Uploadable]
class User {
#[Vich\UploadableField(mapping: 'user_images', appDir: 'app/')]
private $image;
}
Crop Processing
Configure LiipImagineBundle filters for cropping (e.g., crop, thumbnail).
# config/packages/liip_imagine.yaml
liip_imagine:
filter_sets:
crop_user:
quality: 75
filters:
crop: { dimensions: [200, 200], position: [0, 0] }
Form Integration
Use ImageCropType with crop and scale options:
$builder->add('image', ImageCropType::class, [
'crop' => true,
'scale' => 0.9,
'crop_filter' => 'crop_user', // Link to LiipImagine filter
]);
Validation Validate cropped dimensions in your controller:
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$image = $form->get('image')->getData();
if ($image && $image->getCrop()) {
$cropData = $image->getCrop();
// Validate $cropData->getWidth(), $cropData->getHeight()
}
}
crop_filter to dynamically apply different cropping rules per entity.postUpload events using LiipImagineBundle.vich_uploader).Deprecated Bundle
VichUploader Dependency
VichUploaderBundle for file handling. Conflicts may arise if not configured properly.vich_uploader is set up with correct mappings and storage paths.Crop Data Persistence
position, dimensions) are not automatically saved to the database.LiipImagine Filters
crop dimensions) may corrupt images.liip:imagine:filter:dump.VichUploaderBundle logs (debug:config vich_uploader).dump($form->get('image')->getData()->getCrop());
imagecrop Twig extension is registered in services.yaml:
services:
Anacona16\ImageCropBundle\Twig\ImageCropExtension:
tags: ['twig.extension']
Custom Crop Logic
Extend Anacona16\ImageCropBundle\Form\Type\ImageCropType to add validation or processing:
class CustomImageCropType extends ImageCropType {
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'custom_option' => false,
]);
}
}
Event Listeners
Hook into vich_uploader.post_upload to process cropped images:
// src/EventListener/CropListener.php
public function onPostUpload(UploadEvent $event) {
$image = $event->getUploadable();
if ($image->getCrop()) {
// Apply custom crop logic
}
}
Asset Pipelines
Integrate with Webpack Encore or Symfony AssetMapper for modern asset handling if using legacy JS/CSS from the bundle.
How can I help you explore Laravel packages today?