New in Symfony 3.1: Data URI Normalizer
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
The Serializer component uses normalizers/denormalizers to turn objects into
the interim arrays used to serialize/deserialize contents. In Symfony 3.1, a new
DataUriNormalizer has been added to turn file objects into
First, to normalize an object into a
data:URI string, pass the instance of
the object to the
1 2 3 4 5
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer; $normalizer = new DataUriNormalizer(); $avatar = $normalizer->normalize(new \SplFileObject('avatar.gif')); // $avatar = 'data:image/gif;base64,R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=';
The normalized file contents can now be shared safely even with other technologies,
data:URI string into an object instance is very simple too:
1 2 3 4 5
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer; $normalizer = new DataUriNormalizer(); $avatar = $normalizer->denormalize('data:image/gif;base64,R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=', 'SplFileObject'); // $avatar is a SplFileObject with the GIF image contents
The normalizer/denormalizer supports any file object which is an instance of
When normalizing objects, the
MimeTypeGuesser provided by the HttpFoundation
component is used to determine the MIME type of the object. If your application
uses a custom guesser, pass it as the first argument of the
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.