melihovv/base64-image-decoder

A base64 image decoder

0.2.0 2018-09-08 08:35 UTC

This package is auto-updated.

Last update: 2024-12-08 22:59:21 UTC


README

Build Status styleci Scrutinizer Code Quality SensioLabsInsight Coverage Status

Packagist Packagist Packagist

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.