awaisjameel / base64filehandler
A Laravel package for handling and storing base64 encoded files.
Fund package maintenance!
AwaisJameel
Requires
- php: ^8.2
- awaisjameel/mimetypes: dev-main
- illuminate/contracts: ^10.0||^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
This package is not auto-updated.
Last update: 2025-02-28 18:48:51 UTC
README
A Laravel package for handling and storing base64 encoded files.
Installation
You can install the package via composer:
composer require AwaisJameel/base64-file-handler
The package will automatically register its service provider if you're using Laravel 5.5+.
You can publish the configuration file with:
php artisan vendor:publish --provider="AwaisJameel\Base64FileHandler\Base64FileHandlerServiceProvider" --tag="config"
Configuration
After publishing the configuration file, you can find it at config/base64-file-handler.php
. The configuration allows you to set:
- Default storage disk
- Default storage path
- Allowed file extensions
- Valid image extensions
Usage
Basic Usage
You can use the facade to store base64 encoded files:
use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler; // Store a base64 encoded file $filePath = Base64FileHandler::store($base64Data); // Store with custom parameters $filePath = Base64FileHandler::store( $base64Data, 'local', // custom disk 'custom/path/', // custom path 'original-filename.jpg', // original filename ['jpg', 'png'] // allowed extensions );
Validating Images
use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler; try { Base64FileHandler::isValidImage($base64Data); // The data is a valid image } catch (Exception $e) { // The data is not a valid image }
Getting File Information
use AwaisJameel\Base64FileHandler\Facades\Base64FileHandler; $fileInfo = Base64FileHandler::getFileInfo($base64Data); // Returns array with mime, extension, size, and decoded data
Direct Instantiation
You can also use the class directly without the facade:
use AwaisJameel\Base64FileHandler\Base64FileHandler; $handler = new Base64FileHandler([ 'disk' => 'local', 'path' => 'custom/path/', 'allowed_extensions' => ['jpg', 'png', 'pdf'], ]); $filePath = $handler->store($base64Data);
Error Handling
The package throws exceptions when:
- The base64 data is invalid
- The file extension is not allowed
- The image extension is invalid (when validating images)
- Unable to store the file
Wrap your code in try-catch blocks to handle these exceptions.
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.