arkounay/gregwar-image-bundle
Installation
composer require arkounay/gregwar-image-bundle
Ensure ext-gd is enabled in your PHP environment.
Configuration
Add the bundle to config/bundles.php (Symfony 5+):
return [
// ...
Arkounay\GregwarImageBundle\ArkounayGregwarImageBundle::class => ['all' => true],
];
Configure config/packages/gregwar_image.yaml:
gregwar_image:
web_dir: '%kernel.project_dir%/public'
cache_dir: '%kernel.project_dir%/var/cache/gregwar_image'
First Use Case Generate a thumbnail in a controller:
use Arkounay\GregwarImageBundle\Service\ImageService;
public function thumbnail(ImageService $imageService)
{
$image = $imageService->get('path/to/image.jpg');
$image->resize(100, 100);
$image->save('path/to/thumbnail.jpg');
return new Response('Thumbnail generated!');
}
Twig Integration
Use the image Twig filter in templates:
<img src="{{ asset(image('path/to/image.jpg')->resize(200, 200)->getUrl()) }}">
Image Processing Pipeline Chain methods for transformations:
$image = $imageService->get('image.jpg')
->resize(800, 600)
->crop(200, 200, 100, 100) // x, y, width, height
->rotate(90)
->watermark('watermark.png', 0.5, 0.5)
->save('processed.jpg');
Dynamic URL Generation Generate URLs with query parameters for caching:
$url = $imageService->get('image.jpg')
->resize(300, 300)
->getUrl(); // e.g., /cache/abc123.jpg?width=300&height=300
Batch Processing Process multiple images in a loop:
foreach ($imagePaths as $path) {
$image = $imageService->get($path)
->resize(400, 400)
->save("thumbs/{$path}");
}
Symfony Forms
Use with VichUploaderBundle for file uploads:
// In a form type
$builder->add('image', FileType::class, [
'mapped' => false,
'constraints' => [new Image(['maxSize' => '1024k'])]
]);
API Responses Return processed images as binary responses:
$image = $imageService->get('image.jpg')->resize(500, 500);
return new BinaryFileResponse($image->getPath(), 200, [
'Content-Type' => $image->getMimeType(),
'Content-Disposition' => 'inline; filename="image.jpg"',
]);
Event Listeners Trigger processing on upload:
// src/EventListener/ImageUploadListener.php
public function onUpload(UploadEvent $event)
{
$image = $this->imageService->get($event->getPath());
$image->resize(800, 600)->save($event->getPath());
}
Twig Extensions Create custom filters/extensions:
// src/Twig/ImageExtension.php
public function getThumbnailUrl($path, $width, $height)
{
return $this->imageService->get($path)
->resize($width, $height)
->getUrl();
}
GD Extension Requirements
ext-gd is installed and enabled (php -m | grep gd).Cache Directory Permissions
cache_dir permissions to 775 or 777 if PHP runs as a non-root user:
chmod -R 775 %kernel.project_dir%/var/cache/gregwar_image
Memory Limits
memory_limit. Increase if needed:
# config/packages/framework.yaml
framework:
php_errors:
memory_limit: 512M
URL Generation Quirks
web_dir in URLs? Override the getUrl() method or use a custom service:
$image->setWebDir('/custom/web/path');
Mime Type Detection
$image->setMimeType('image/jpeg');
Log Image Metadata Dump image properties for debugging:
$image = $imageService->get('image.jpg');
\Log::debug([
'width' => $image->getWidth(),
'height' => $image->getHeight(),
'mime' => $image->getMimeType(),
'path' => $image->getPath(),
]);
Check Cache Directory
Verify processed images are saved to cache_dir:
ls -la %kernel.project_dir%/var/cache/gregwar_image
Validate Config
Ensure web_dir and cache_dir paths are correct and writable. Test with:
$this->imageService->getConfig()->getWebDir();
Custom Filters
Extend the Gregwar\Image\Image class:
// src/Service/CustomImage.php
class CustomImage extends \Gregwar\Image\Image
{
public function customFilter()
{
// Custom logic
}
}
Register as a service and replace the default.
Override Templates
Customize Twig templates (e.g., image.html.twig) in templates/bundles/gregwarimage/.
Add New Operations
Extend the ImageService to support custom operations:
// src/Service/ExtendedImageService.php
public function addBorder($color, $width)
{
$this->image->drawRectangle(
0, 0, $this->image->getWidth(), $this->image->getHeight(),
$color, $width
);
return $this;
}
Event Dispatching
Listen for image events (e.g., ImageEvent):
// config/services.yaml
services:
App\EventListener\ImageListener:
tags:
- { name: kernel.event_listener, event: gregwar_image.process, method: onProcess }
Cache Aggressively Use query parameters for cache busting:
<img src="{{ asset(image('image.jpg')->resize(300, 300)->getUrl()) }}">
Avoid regenerating images unless parameters change.
Lazy Loading
Process images on-demand (e.g., in a Controller or Command) rather than during request handling.
Batch Processing
Use Symfony’s Messenger component for async processing:
$this->messageBus->dispatch(new ProcessImageMessage($imagePath, $transformations));
How can I help you explore Laravel packages today?