A Symfony Bundle for generating secure, signed URLs for the imgproxy service.
- Clean, fluent Builder pattern API
- HMAC-SHA256 signing with URL-safe Base64 encoding
- Full Symfony integration with Dependency Injection
- Comprehensive test coverage
- PHP 8.1+ support
composer require xiidea/easy-imgproxy-bundleAdd the bundle to your config/bundles.php:
return [
// ...
Xiidea\EasyImgProxyBundle\XiideaEasyImgProxyBundle::class => ['all' => true],
];Create config/packages/xiidea_easy_img_proxy.yaml:
xiidea_easy_img_proxy:
key: '%env(IMGPROXY_KEY)%'
salt: '%env(IMGPROXY_SALT)%'
base_url: '%env(IMGPROXY_BASE_URL)%'Add to .env:
# Hex-encoded 32-byte key
IMGPROXY_KEY=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
# Hex-encoded 16-byte salt
IMGPROXY_SALT=0123456789abcdef0123456789abcdef
# imgproxy service URL
IMGPROXY_BASE_URL=http://localhost:8080use Xiidea\EasyImgProxyBundle\Service\ImgProxyUrlGenerator;
// Inject the service
public function __construct(ImgProxyUrlGenerator $generator)
{
$this->generator = $generator;
}
// Build a URL
$url = $this->generator->builder()
->withImageUrl('https://example.com/image.jpg')
->withWidth(200)
->withHeight(300)
->withQuality(80)
->withExtension('webp')
->build();$url = $this->generator->generate(
'https://example.com/image.jpg',
[
'width' => 200,
'height' => 300,
'quality' => 80,
'gravity' => 'center',
],
'webp' // optional extension
);Dimension Options:
withWidth(int)- Image width in pixelswithHeight(int)- Image height in pixelswithResizing(string)- Resizing type:fit,fill,auto,forcewithGravity(string)- Gravity:center,north,south,east,west, etc.withQuality(int)- JPEG quality: 0-100
Format Option:
withExtension(string)- Output format:webp,png,jpg,gif, etc.
Custom Options:
withOption(string $key, mixed $value)- Add any custom processing option
The bundle supports two types of presets:
Define reusable configurations in config/packages/xiidea_easy_img_proxy.yaml:
xiidea_easy_img_proxy:
key: '%env(IMGPROXY_KEY)%'
salt: '%env(IMGPROXY_SALT)%'
base_url: '%env(IMGPROXY_BASE_URL)%'
presets:
thumbnail:
options:
width: 200
height: 200
resizing_type: fill
gravity: center
quality: 85
extension: webp
hero:
options:
width: 1200
height: 400
resizing_type: fill
quality: 90
extension: jpg
product:
options:
width: 600
quality: 90Use custom presets in your code:
// Apply a single preset
$url = $this->generator->builder()
->withImageUrl('https://example.com/image.jpg')
->withPreset('thumbnail')
->build();
// Apply multiple presets (later ones override earlier ones)
$url = $this->generator->builder()
->withImageUrl('https://example.com/image.jpg')
->withPresets(['product', 'quality'])
->build();
// Override preset options with explicit values
$url = $this->generator->builder()
->withImageUrl('https://example.com/image.jpg')
->withPreset('thumbnail')
->withQuality(95) // Overrides preset quality
->withExtension('png') // Overrides preset extension
->build();Apply presets defined on the imgproxy server:
// Apply a single server preset
$url = $this->generator->builder()
->withImageUrl('https://example.com/image.jpg')
->withServerPreset('blurry')
->build();
// Apply server preset with parameters
$url = $this->generator->builder()
->withImageUrl('https://example.com/image.jpg')
->withServerPreset('blur:strong')
->build();
// Apply multiple server presets
$url = $this->generator->builder()
->withImageUrl('https://example.com/image.jpg')
->withServerPresets(['sharpen', 'quality:high'])
->build();Both preset types can be used together:
$url = $this->generator->builder()
->withImageUrl('https://example.com/image.jpg')
->withServerPreset('blur') // imgproxy server preset
->withPreset('product') // custom preset
->withQuality(90) // explicit option (highest priority)
->build();Priority Order (highest to lowest):
- Explicitly set options (e.g.,
withWidth(300)) - Custom preset options
- Server presets (imgproxy-side)
Generated URLs follow the imgproxy format:
{BASE_URL}/{SIGNATURE}/{PROCESSING_PATH}/{IMAGE_URL}
Example:
http://localhost:8080/UtBg7s3YMkw5-gP...bQ/width/200/height/300/format/webp/https://example.com/image.jpg
Run the test suite:
vendor/bin/phpunitThe bundle correctly implements imgproxy's signing specification:
- Processes all options into a URL path
- Signs the path using HMAC-SHA256 with the provided key
- Prepends the salt to the signature
- Encodes using URL-safe Base64 without padding
- Builds the final signed URL
MIT