Installation:
composer.json:
"require": {
"paradigmate/image-bundle": "dev-master"
}
composer update.app/AppKernel.php:
new Paradigma\Bundle\ImageBundle\ParadigmaImageBundle(),
Configuration:
app/config/config.yml:
paradigmate_image:
default_driver: gd # or 'imagick' if available
First Use Case:
use Paradigma\Bundle\ImageBundle\Image\ImageResizer;
use Paradigma\Bundle\ImageBundle\Image\ImageSize;
class ImageController extends Controller
{
public function resizeAction($sourcePath, $destinationPath, $size)
{
$resizer = $this->get('image_resizer');
$resizer->resize(
$sourcePath,
$destinationPath,
new ImageSize($size, $size),
ImageResizer::RESIZE_TYPE_CROP
);
return new Response('Image resized!');
}
}
# app/config/routing.yml
image_resize:
path: /resize/{source}/{destination}/{size}
defaults: { _controller: AppBundle:Image:resize }
Dynamic Thumbnail Generation:
// In a FormType or EventListener
$resizer->resize(
$uploadedFile->getPathname(),
$destinationPath,
new ImageSize(200, 200),
ImageResizer::RESIZE_TYPE_FIT
);
Lazy-Loading with Cache:
public/uploads/thumbs/ and cache responses:
$cacheKey = md5($sourcePath . $size);
$cachedPath = $this->getParameter('kernel.root_dir') . '/../web/uploads/thumbs/' . $cacheKey;
if (!file_exists($cachedPath)) {
$resizer->resize($sourcePath, $cachedPath, new ImageSize($size, $size));
}
return $this->renderView('image.html.twig', ['path' => '/uploads/thumbs/' . $cacheKey]);
Batch Processing:
foreach ($imagePaths as $path) {
$this->get('image_resizer')->resize(
$path,
$path . '_thumb',
new ImageSize(100, 100),
ImageResizer::RESIZE_TYPE_CROP
);
}
Doctrine Entities:
getThumbnailPath() method to your entity:
public function getThumbnailPath($size = 100)
{
return $this->getPath() . '_thumb_' . $size . '.jpg';
}
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function generateThumbnail()
{
if ($this->getPath()) {
$resizer = $this->getContainer()->get('image_resizer');
$resizer->resize(
$this->getPath(),
$this->getThumbnailPath(),
new ImageSize(100, 100)
);
}
}
Twig Filters:
// src/AppBundle/Twig/AppExtension.php
class AppExtension extends \Twig_Extension
{
public function getFilters()
{
return [
new \Twig_SimpleFilter('resize_image', [$this, 'resizeImageFilter']),
];
}
public function resizeImageFilter($path, $size)
{
return $this->container->get('router')->generate(
'image_resize',
['source' => basename($path), 'destination' => 'thumb_' . $size, 'size' => $size]
);
}
}
<img src="{{ path|resize_image(200) }}" />
File Permissions:
www-data) has write permissions to the destination directory.chmod -R 775 public/uploads/thumbs and chown -R www-data:www-data public/uploads/thumbs.GD vs. Imagick:
default_driver: imagick in config if available.Memory Limits:
memory_limit.memory_limit in php.ini or use Imagick for better memory handling.EXIF Orientation:
paradigmate_image:
exif_aware: true
Race Conditions:
flock) or a queue system (Symfony Messenger).Check Logs:
APP_DEBUG=true) to see errors in var/log/dev.log.Validate Input:
$sourcePath exists before resizing:
if (!file_exists($sourcePath)) {
throw new \RuntimeException("Source image not found: {$sourcePath}");
}
Custom Resize Types:
ImageResizer class to add custom resize logic (e.g., "stretch to fill"):
class CustomImageResizer extends ImageResizer
{
const RESIZE_TYPE_STRETCH = 'stretch';
protected function resizeStretch($source, $destination, ImageSize $size)
{
// Custom logic here
}
}
services:
custom_image_resizer:
class: AppBundle\Service\CustomImageResizer
arguments: ['@image_resizer.driver.gd']
Driver Overrides:
// src/AppBundle/DependencyInjection/Compiler/CustomImagePass.php
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('image_resizer.driver.gd');
$definition->setClass('AppBundle\Image\CustomDriver');
}
Event Listeners:
// src/AppBundle/EventListener/ImageListener.php
class ImageListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'image_resizer.pre_resize' => 'onPreResize',
'image_resizer.post_resize' => 'onPostResize',
];
}
public function onPreResize(GetResponseEvent $event)
{
// Validate image before resize
}
}
Cache Busters:
return '/uploads/thumbs/' . $cacheKey . '?v=' . filemtime($sourcePath);
Predefined Sizes:
ImageSize objects:
paradigmate_image:
sizes:
thumbnail: 100
medium: 500
large: 1200
$size = new ImageSize($this->getParameter('paradigmate_image.sizes.thumbnail'));
How can I help you explore Laravel packages today?