PHP code example of awaisjameel / base64filehandler

1. Go to this page and download the library: Download awaisjameel/base64filehandler 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/ */

    

awaisjameel / base64filehandler example snippets


return [
    'disk' => env('BASE64_FILE_HANDLER_DISK', 'public'),
    'path' => env('BASE64_FILE_HANDLER_PATH', 'uploads/'),
    'allowed_extensions' => [
        // 'pdf', 'jpg', 'png', 'docx', etc.
    ],
    'valid_image_extensions' => [
        'jpeg', 'jpg', 'png', 'gif', 'webp',
    ],
];

use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;

$path = Base64FileHandler::store($base64Data);

use AwaisJameel\Base64FileHandler\Base64FileHandler;

class AvatarController
{
    public function __construct(private Base64FileHandler $handler)
    {
    }

    public function store(Request $request)
    {
        $path = $this->handler->store($request->input('avatar'));

        // ...
    }
}

use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;

// Uses the configured default disk, path, and allowed extensions.
$path = Base64FileHandler::store($base64Data);

// Every argument after the base64 payload is optional and overrides config for this call only.
$path = Base64FileHandler::store(
    $base64Data,
    disk: 's3',
    path: 'avatars/',
    originalName: 'profile-picture.jpg',
    allowedExtensions: ['jpg', 'jpeg', 'png'],
);

use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;
use AwaisJameel\Base64FileHandler\Exceptions\InvalidImageException;
use AwaisJameel\Base64FileHandler\Exceptions\MimeTypeMismatchException;

try {
    Base64FileHandler::isValidImage($base64Data);
    // The data decodes to one of the configured valid image extensions.
} catch (InvalidImageException | MimeTypeMismatchException $e) {
    // Not a valid image.
}

// Or check against a custom set of extensions for this call only:
Base64FileHandler::isValidImage($base64Data, ['png', 'webp']);

use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;

$fileInfo = Base64FileHandler::getFileInfo($base64Data);

// [
//     'mime' => 'image/png',
//     'extension' => 'png',
//     'size' => 18342,
//     'data' => "\x89PNG\x0D\x0A...", // raw decoded binary
// ]

use AwaisJameel\Base64FileHandler\Exceptions\Base64FileHandlerException;
use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;

try {
    $path = Base64FileHandler::store($base64Data);
} catch (Base64FileHandlerException $e) {
    report($e);
    abort(422, $e->getMessage());
}

use AwaisJameel\Base64FileHandler\Exceptions\Base64FileHandlerException;
use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler;
use AwaisJameel\MimeTypes\Exceptions\MimeTypesException;

try {
    $path = Base64FileHandler::store($base64Data);
} catch (Base64FileHandlerException | MimeTypesException $e) {
    report($e);
    abort(422, 'Unable to process the uploaded file.');
}

use AwaisJameel\Base64FileHandler\Base64FileHandler;

$handler = new Base64FileHandler([
    'disk' => 'local',
    'path' => 'custom/path/',
    'allowed_extensions' => ['jpg', 'png', 'pdf'],
]);

$path = $handler->store($base64Data);
bash
php artisan vendor:publish --tag="base64filehandler-config"