A small set of classes (decoder, encoder) to work with images as data-uris.
Install via composer
composer require melihovv/base64-image-decoder
use Melihovv\Base64ImageDecoder\Base64ImageEncoder;
$encoder = Base64ImageEncoder::fromFileName('/path/to/picture.jpg', $allowedFormats = ['jpeg', 'png', 'gif']);
#$encoder = Base64ImageEncoder::fromBinaryData($someRawBinaryData, $allowedFormats = ['jpeg', 'png', 'gif']);
#$encoder = Base64ImageEncoder::fromResource($someResource, $allowedFormats = ['jpeg', 'png', 'gif']);
$encoder->getMimeType(); // image/jpeg for instance
$encoder->getContent(); // base64 encoded image bytes.
$encoder->getDataUri(); // a base64 data-uri to use in HTML or CSS attributes.
use Melihovv\Base64ImageDecoder\Base64ImageDecoder;
$dataUri = 'data:image/gif;base64,R0lGODlhLAH6AOZ/AMyokXJMK0uE...'; // image may come from http request or any other source.
// We check that image is encoded properly in constructor, otherwise exception will be thrown.
// You can use this info in your validation rule.
$decoder = new Base64ImageDecoder($dataUri, $allowedFormats = ['jpeg', 'png', 'gif']);
$decoder->getFormat(); // 'png', or 'jpeg', or 'gif', or etc.
$decoder->getDecodedContent(); // base64 decoded raw image bytes.
$decoder->getContent(); // base64 encoded raw image bytes.
If you discover any security related issues, please email amelihovv@ya.ru instead of using the issue tracker.
This package is bootstrapped with melihovv/laravel-package-generator.