laravel-magika maintained by megoxv
Laravel Magika AI
Laravel Magika AI is a high-performance file type detection package for Laravel. It utilizes Google's Magika deep learning model to accurately identify file types based on their content rather than extensions.
Key Features
- AI-Powered: Uses Google's deep learning model (Magika) for 99%+ accuracy.
- Fast and Lightweight: Inference takes only a few milliseconds per file.
- Support for 100+ Formats: Accurately detects code, documents, archives, and media.
- Security Focused: Detects misleading extensions (e.g., PHP code disguised as a JPG).
- Confidence Scoring: Require a minimum AI confidence score before accepting uploads.
Installation
Install the package via composer:
composer require megoxv/laravel-magika
Download and install the appropriate Magika binary for your system:
php artisan magika:install
Check the installation status anytime:
php artisan magika:install --status
Usage
Validation Rule
Use the magika rule in your controllers. You can specify allowed labels and an optional minimum confidence score.
// Simple usage (allow only PDF)
$request->validate([
'document' => 'required|file|magika:pdf'
]);
// With Confidence Threshold (e.g., must be 95% sure it's a PDF)
$request->validate([
'document' => 'required|file|magika:pdf,0.95'
]);
// Multiple types
$request->validate([
'profile_image' => 'required|file|magika:png,jpeg,0.9'
]);
Using the Facade
use Megoxv\LaravelMagika\Facades\Magika;
$result = Magika::predict($path);
echo $result->label; // e.g., "pdf"
echo $result->mimeType; // e.g., "application/pdf"
echo $result->score; // e.g., 0.992
echo $result->isText; // true/false
Using the Helper
$result = magika()->predict($path);
Configuration
Publish the config file:
php artisan vendor:publish --tag="magika-config"
License
The MIT License; see LICENSE for details.