DataUriInterface, enabling seamless integration with existing DTOs/Entities.1tomany/data-uri library handles file-to-Data-URI conversion, while the bundle provides Symfony-specific bindings (denormalizers, console commands). This separation allows future swaps if needed.DataUriNormalizer for DataUriInterface, requiring only:
@Serializer\Context (if using groups).onetomany:data-uri:encode-file command enables manual testing and CLI-driven workflows (e.g., generating Data URIs for email templates).client_max_body_size).DataUriInterface is used with untrusted input.collection/item responses.@Serializer\Context.symfony/serializer).1tomany/data-uri library manually.composer require 1tomany/data-uri-bundle.DataUriInterface implementation (e.g., for a ProductImage entity):
use OneToMany\DataUri\Contract\Record\DataUriInterface;
class ProductImage implements DataUriInterface {
public function getFilePath(): string { ... }
public function getMimeType(): string { ... }
}
php bin/console onetomany:data-uri:encode-file public/uploads/product.jpg
/api/health).@Serializer\Context.DataUriInterface:
public function toDataUri(): ?string {
if ($this->getFileSize() > 1_000_000) { // 1MB limit
return null; // Use URL instead
}
return base64_encode(file_get_contents($this->getFilePath()));
}
@Serializer\Groups({"api"})), explicitly configure the denormalizer in config/packages/serializer.yaml:
services:
OneToMany\DataUriBundle\Serializer\DataUriNormalizer:
tags: ['serializer.normalizer', 'serializer.denormalizer']
symfony/serializer to a stable version (e.g., ^6.3).config/bundles.php.DataUriInterface for one entity (e.g., ProductImage).use Symfony\Component\Serializer\SerializerInterface;
$serializer = $container->get(SerializerInterface::class);
$dataUri = $serializer->serialize($productImage, 'json');
#[Serializer\Context(['groups' => ['api']])]
public function getProduct(Product $product): ProductDto { ... }
<img src="{{ product.image|data_uri }}" alt="{{ product.name }}">
$cache = $container->get('cache.app');
$key = 'data_uri_' . md5($filePath);
return $cache->get($key, fn() => $this->encodeFile($filePath));
How can I help you explore Laravel packages today?