melihovv / base64-image-decoder
A base64 image decoder
Installs: 109 569
Dependents: 0
Suggesters: 0
Security: 0
Stars: 44
Watchers: 3
Forks: 3
Open Issues: 0
Requires
- php: >=7.0
- ext-fileinfo: *
Requires (Dev)
- ext-gd: *
- phpunit/phpunit: ~6.0
This package is auto-updated.
Last update: 2024-12-08 22:59:21 UTC
README
A small set of classes (decoder, encoder) to work with images as data-uris.
Installation
Install via composer
composer require melihovv/base64-image-decoder
Usage
Encoder
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.
Decoder
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.
Security
If you discover any security related issues, please email amelihovv@ya.ru instead of using the issue tracker.
Credits
This package is bootstrapped with melihovv/laravel-package-generator.