PHP code example of melihovv / base64-image-decoder

1. Go to this page and download the library: Download melihovv/base64-image-decoder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

melihovv / base64-image-decoder example snippets


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.