megoxv / laravel-magika
A Laravel package for file type detection using Google Magika AI.
v1.0.0
2026-04-19 10:47 UTC
Requires
- php: ^8.2
- illuminate/support: ^10.0|^11.0|^12.0
- symfony/process: ^6.0|^7.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.0
README
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.