1. Go to this page and download the library: Download rayzenai/file-manager 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/ */
rayzenai / file-manager example snippets
use Kirantimsina\FileManager\FileManagerPlugin;
public function panel(Panel $panel): Panel
{
return $panel
->plugins([
FileManagerPlugin::make(),
]);
}
return [
// CDN URL for serving files
'cdn' => env('CDN_URL', env('AWS_URL', env('APP_URL'))),
// Maximum upload dimensions
'max-upload-height' => '5120', // pixels
'max-upload-width' => '5120', // pixels
'max-upload-size' => '8192', // KB
// Model to directory mappings (supports full class names)
'model' => [
'App\Models\User' => 'users', // Or use User::class
'App\Models\Product' => 'products', // Or use Product::class
'App\Models\Blog' => 'blogs', // Or use Blog::class
// Add your models here
],
// Image sizes to generate
// Set to empty array [] to disable automatic resizing completely
'image_sizes' => [
'icon' => 64, // 64px height for small icons
'small' => 120, // 120px height for small thumbnails
'thumb' => 240, // 240px height for thumbnails
'card' => 360, // 360px height for card images
'medium' => 480, // 480px height for medium images
'large' => 720, // 720px height for large images
'full' => 1080, // 1080px height for full size
'ultra' => 2160, // 2160px height for ultra HD
],
// Default thumbnail size for MediaColumn components
'default_thumbnail_size' => env('FILE_MANAGER_DEFAULT_THUMBNAIL_SIZE', 'icon'),
'default_card_size' => env('FILE_MANAGER_DEFAULT_CARD_SIZE', 'card'),
// Cache Control Headers for Images
'cache' => [
'enabled' => env('FILE_MANAGER_CACHE_ENABLED', true),
// Cache duration in seconds
// Common values:
// 3600 = 1 hour
// 86400 = 1 day
// 604800 = 1 week
// 2592000 = 30 days
// 31536000 = 1 year (default)
'max_age' => env('FILE_MANAGER_CACHE_MAX_AGE', 31536000),
// Cache visibility:
// 'public' - Can be cached by browsers and CDNs
// 'private' - Only cached by browsers, not CDNs
'visibility' => env('FILE_MANAGER_CACHE_VISIBILITY', 'public'),
// Immutable directive - tells browsers the file will never change
// Recommended for versioned/hashed filenames
'immutable' => env('FILE_MANAGER_CACHE_IMMUTABLE', true),
],
// Compression settings
'compression' => [
'enabled' => env('FILE_MANAGER_COMPRESSION_ENABLED', true),
'method' => env('FILE_MANAGER_COMPRESSION_METHOD', 'gd'), // 'gd' or 'api'
'auto_compress' => env('FILE_MANAGER_AUTO_COMPRESS', true),
'quality' => env('FILE_MANAGER_COMPRESSION_QUALITY', 85), // 1-95
'format' => env('FILE_MANAGER_COMPRESSION_FORMAT', 'webp'), // webp, jpeg, jpg, png, avif
'mode' => env('FILE_MANAGER_COMPRESSION_MODE', 'contain'), // contain, crop, cover
'threshold' => env('FILE_MANAGER_COMPRESSION_THRESHOLD', 100 * 1024), // 100KB
// Maximum allowed dimensions (hard limits - images will never exceed these)
'max_height' => env('FILE_MANAGER_MAX_HEIGHT', 2160),
'max_width' => env('FILE_MANAGER_MAX_WIDTH', 3840),
// API settings for external compression
'api' => [
// Primary API (AWS Lambda - fast, no background removal)
'url' => env('FILE_MANAGER_COMPRESSION_API_URL', ''),
'token' => env('FILE_MANAGER_COMPRESSION_API_TOKEN', ''),
'timeout' => env('FILE_MANAGER_COMPRESSION_API_TIMEOUT', 30),
// Background removal API (Google Cloud Run - slower, supports bg removal)
'bg_removal' => [
'url' => env('FILE_MANAGER_BG_REMOVAL_API_URL', ''),
'token' => env('FILE_MANAGER_BG_REMOVAL_API_TOKEN', ''),
'timeout' => env('FILE_MANAGER_BG_REMOVAL_API_TIMEOUT', 60),
],
],
],
// Media metadata tracking
'media_metadata' => [
'enabled' => env('FILE_MANAGER_METADATA_ENABLED', true),
'track_file_size' => env('FILE_MANAGER_TRACK_FILE_SIZE', true),
'track_dimensions' => env('FILE_MANAGER_TRACK_DIMENSIONS', true),
'track_mime_type' => env('FILE_MANAGER_TRACK_MIME_TYPE', true),
'model' => \Kirantimsina\FileManager\Models\MediaMetadata::class,
],
];
use Kirantimsina\FileManager\Traits\HasImages;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasImages;
protected $fillable = [
'name',
'image', // Single image field
'gallery', // Multiple images field
];
protected $casts = [
'gallery' => 'array', // Cast array fields
];
// Define which fields contain images for automatic resizing. Use `public` so that this is accessible by view helpers
public function hasImagesTraitFields(): array
{
return ['image', 'gallery'];
}
}
use Kirantimsina\FileManager\Forms\Components\MediaUpload;
// Set SEO title directly
MediaUpload::make('image')
->seoTitle('Premium Chocolate Cake')
// Set SEO title from another field
MediaUpload::make('image')
->seoTitleFromField('name') // Uses the 'name' field value as SEO title
// Dynamic SEO title with closure
MediaUpload::make('image')
->seoTitle(fn (Get $get) => $get('meta_title') ?? $get('name'))
'seo' => [
'enabled_models' => [
'App\Models\Product',
'App\Models\Category',
'App\Models\Blog',
// Models that should have SEO titles
],
'excluded_models' => [
'App\Models\User',
'App\Models\Order',
// Models that should NOT have SEO titles
],
],
use Kirantimsina\FileManager\Forms\Components\MediaUpload;
// Example 1: Convert to WebP with custom quality
MediaUpload::make('image')
->label('Product Image')
->quality(90) // Compression quality, capped at 95
->toWebp() // Convert to WebP format
// Example 2: Compress but keep original format (JPEG stays JPEG, PNG stays PNG)
MediaUpload::make('photo')
->keepOriginalFormat() // Compress but maintain original format
->quality(85) // Still applies compression
// Example 3: Upload without ANY processing
MediaUpload::make('document')
->uploadOriginal() // Skip all processing - no compression, no conversion
// Example 4: Full featured upload with background removal
MediaUpload::make('product_image')
->toAvif() // Convert to AVIF format
->quality(90) // High quality
->removeBg() // Remove background (API
// Use GD library (built-in PHP processing)
MediaUpload::make('image')
->driver('gd')
// Use external API service
MediaUpload::make('image')
->driver('api')
// Enable background removal with boolean
MediaUpload::make('image')
->removeBg(true)
// Or use the convenience method
MediaUpload::make('image')
->withoutBackground()
// Dynamic background removal with closure
use Filament\Forms\Components\Toggle;
Toggle::make('remove_bg')
->label('Remove Background')
->dehydrated(false) // Don't save to database
MediaUpload::make('image')
->removeBg(fn (Get $get) => $get('remove_bg'))
// In your Filament Resource
protected function mutateFormDataBeforeCreate(array $data): array
{
unset($data['remove_bg']);
return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
unset($data['remove_bg']);
return $data;
}
use Kirantimsina\FileManager\Tables\Columns\MediaUrlColumn;
// Basic usage - links to media page
MediaUrlColumn::make('image_file_name')
->label('Product Image')
->thumbnailSize('small')
->openInNewTab() // Open link in new tab
// With relationship
MediaUrlColumn::make('featured_image')
->label('Featured Image')
->relationship('media')
->thumbnailSize('card')
// In your model
class CartItem extends Model
{
public function attachments(): HasMany
{
return $this->hasMany(AttachmentFile::class);
}
}
// In your Filament resource
MediaColumn::make(
field: 'attachment_file_name', // Field on the AttachmentFile model
relationship: 'attachments', // Relationship method name
showInModal: true
)
// config/file-manager.php
'default_thumbnail_size' => 'thumb', // Default for thumbnail displays
'default_card_size' => 'large', // Default for card displays
MediaModalColumn::make('image')
->thumbnailSize('large') // This overrides the default
MediaUrlColumn::make('image')
->thumbnailSize('large') // This overrides the default
use Kirantimsina\FileManager\Tables\Columns\S3Image;
S3Image::make('image')
->label('Image')
->size('medium') // Use specific size: 'small', 'medium', 'large'
->square() // Square aspect ratio
->circular() // Circular mask
use App\Models\Product;
use Kirantimsina\FileManager\Facades\FileManager;
// Upload a file
$result = FileManager::upload(
Product::class, // model class
$uploadedFile, // file
'summer-sale', // tag (optional)
false, // fit (optional, default: false)
true, // resize (optional, default: true)
false, // webp (optional, default: false)
false // reencode (optional, default: false)
);
$path = $result['file']; // Get the file path from result
// Upload multiple files
$result = FileManager::uploadImages(
Product::class, // model class
$uploadedFiles, // files array
'gallery', // tag (optional)
false, // fit (optional)
true // resize (optional)
);
$paths = $result['files']; // Get array of file paths
// Upload base64 encoded image
$result = FileManager::uploadBase64(
Product::class, // model class
$base64String, // base64 image
'user-upload', // tag (optional)
false, // fit (optional)
true // resize (optional)
);
$path = $result['file']; // Get the file path from result
// Move temp file without resizing
$path = FileManager::moveTempImageWithoutResize(
Product::class, // model class
'temp/abc123.jpg' // temp file path
);
// Move temp file with automatic resizing
$path = FileManager::moveTempImage(
Product::class, // model class
'temp/abc123.jpg' // temp file path
);
// Delete image and all its sizes
FileManager::deleteImage('products/image.jpg');
// Delete multiple images
FileManager::deleteImagesArray(['products/img1.jpg', 'products/img2.jpg']);
// Get SEO-friendly filename
$filename = FileManager::filename(
file: $uploadedFile,
tag: 'product-name',
extension: 'webp'
);
// Get image URL with specific size
$url = FileManager::getMediaPath('products/image.jpg', 'medium');
// Get CDN URL
$cdnUrl = FileManager::mainMediaUrl();
// Get upload directory for a model
$directory = FileManager::getUploadDirectory(Product::class);
// Get configured image sizes
$sizes = FileManager::getImageSizes();
use Kirantimsina\FileManager\Services\ImageCompressionService;
$service = new ImageCompressionService();
// Compress and save
$result = $service->compressAndSave(
sourcePath: '/tmp/upload.jpg',
destinationPath: 'products/compressed.webp',
quality: 85,
height: 1080,
width: null, // Auto-calculate
format: 'webp',
mode: 'contain',
disk: 's3',
removeBg: false // Set to true for background removal (API only)
);
// Compress with background removal (
'image_sizes' => [
'icon' => 64,
'small' => 120,
'xlarge' => 1440, // Add your new size here
],
use Kirantimsina\FileManager\Traits\HasImages;
class Product extends Model
{
use HasImages;
/**
* Define which field should be used for SEO title generation
* This field is also monitored for changes to trigger updates
* Default: 'name'
*/
public function seoTitleField(): string
{
return 'meta_title'; // If meta_title is null/empty, SEO title will be null
}
}
// Product model - wants SEO titles from meta_title
class Product extends Model
{
use HasImages;
public function seoTitleField(): string
{
return 'meta_title';
}
}
// Blog model - wants SEO titles from title field
class Blog extends Model
{
use HasImages;
public function seoTitleField(): string
{
return 'title';
}
}
// Order model - no SEO titles needed (internal use)
class Order extends Model
{
use HasImages;
// No seoTitleField() method = no SEO titles for media
}
// CartItem model - no SEO titles needed
class CartItem extends Model
{
use HasImages;
// No seoTitleField() method = media won't have SEO titles
}
// Get image URL with specific size
$url = getImagePath('products/image.jpg', 'medium');
// Get CDN URL
$url = getCdnUrl('products/image.jpg');
// Check if file is an image
$isImage = isImageFile('document.pdf'); // false
bash
# Publish migration files to your app
php artisan vendor:publish --provider="Kirantimsina\FileManager\FileManagerServiceProvider" --tag="file-manager-migrations"
# Run the migrations
php artisan migrate
bash
# Process resize jobs
php artisan queue:work
# Monitor queue
php artisan queue:monitor
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.