Installation Run:
composer require didweb/resize:1.*
Ensure Didweb\Bundle\ResizeBundle\DidwebResizeBundle() is registered in app/AppKernel.php under $bundles.
Configuration
Add the service import to app/config/config.yml:
imports:
- { resource: "@DidwebResizeBundle/Resources/config/services.yml" }
Define resize parameters in app/config/config.yml:
didweb_resize:
img_ancho_p: 240 # Small width (px)
img_alto_p: 196 # Small height (px)
img_ancho_g: 1024 # Large width (px)
img_alto_g: 768 # Large height (px)
img_directorio: "%kernel.root_dir%/../web/fotos" # Output directory
First Use Case
Use the ResizeService to resize an image:
use Didweb\Bundle\ResizeBundle\Service\ResizeService;
$resizeService = $this->get('didweb_resize.service');
$resizeService->resize('path/to/original/image.jpg');
This generates two versions (small and large) in the configured directory.
Resizing Images
Inject ResizeService into controllers/services:
public function uploadAction(Request $request) {
$file = $request->files->get('image');
$resizeService = $this->get('didweb_resize.service');
$resizeService->resize($file->getPathname());
}
Dynamic Configuration Override defaults via dependency injection:
# app/config/config.yml
services:
didweb_resize.service:
arguments:
- { img_ancho_p: 300, img_alto_p: 200 } # Override small size
Batch Processing Loop through uploaded files:
foreach ($request->files->get('images') as $file) {
$resizeService->resize($file->getPathname());
}
Integration with Symfony Forms
Use with FileType fields:
$builder->add('image', FileType::class, [
'mapped' => false,
'required' => false,
]);
Custom Filenames Extend the service to append metadata (e.g., timestamps):
$resizeService->resize($path, 'custom_prefix_');
Event Listeners Trigger post-resize actions (e.g., database updates):
$eventDispatcher->addListener(
'didweb.resize.after',
function ($event) { /* Handle post-resize logic */ }
);
Directory Permissions
Ensure img_directorio is writable by the web server (e.g., chmod -R 775 web/fotos).
Image Format Preservation
The package does not auto-convert formats. Ensure input/output paths use the same extension (e.g., .jpg → .jpg).
Aspect Ratio Distortion
Hardcoded dimensions may distort images. Use maintainAspectRatio (if supported) or implement dynamic ratios:
didweb_resize:
maintain_ratio: true # Hypothetical config (not natively supported)
Memory Limits
Large images may hit PHP’s memory_limit. Increase if needed:
ini_set('memory_limit', '256M');
Verify Config
Check app/config/config.yml for typos in keys (e.g., img_ancho_p vs. img_ancho_P).
Log Errors Wrap calls in try-catch:
try {
$resizeService->resize($path);
} catch (\Exception $e) {
$this->container->get('logger')->error($e->getMessage());
}
Test with Known Files
Use a small test image (e.g., 100x100px) to validate the pipeline.
Custom Resize Logic Override the service class:
class CustomResizeService extends \Didweb\Bundle\ResizeBundle\Service\ResizeService {
protected function getSmallDimensions() {
return [400, 300]; // Custom small size
}
}
Register it in services.yml:
didweb_resize.service: '@custom_resize_service'
Add Watermarks
Extend the service to apply watermarks post-resize using libraries like Imagine.
Support Additional Formats
Modify the underlying Imagine bundle configuration to handle formats like WebP.
Use Environment Variables Externalize paths/configs for deployments:
img_directorio: "%env(RESIZE_DIR)%"
Cache Resized Images Implement a cache layer (e.g., Redis) to avoid reprocessing identical files.
Document Output Structure
Standardize filenames (e.g., original_123.jpg → small_123.jpg, large_123.jpg) for consistency.
How can I help you explore Laravel packages today?