PHP code example of iamgerwin / nova-media-hub
1. Go to this page and download the library: Download iamgerwin/nova-media-hub 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/ */
iamgerwin / nova-media-hub example snippets
// app/Providers/NovaServiceProvider.php
use Iamgerwin\NovaMediaHub\MediaHub;
public function tools()
{
return [
MediaHub::make(),
];
}
'disk' => env('MEDIA_HUB_DISK', 'public'),
'path' => env('MEDIA_HUB_PATH', 'media'),
'chunked_upload' => [
// Enable/disable chunked upload feature
'enabled' => true,
// Size of each chunk (adjust based on server limits)
'chunk_size' => 5 * 1024 * 1024, // 5MB
// Maximum file size for uploads
'max_file_size' => 1024 * 1024 * 1024, // 1GB
// Upload session lifetime
'session_lifetime_hours' => 24,
// Retry attempts for failed chunks
'retry_attempts' => 3,
// Auto-enable chunked upload for files larger than this
'auto_threshold' => 10 * 1024 * 1024, // 10MB
// Cleanup old chunks after this many hours
'cleanup_old_chunks_after_hours' => 48,
],
'image_conversions' => [
'thumbnail' => [
'width' => 150,
'height' => 150,
'fit' => 'crop',
],
'medium' => [
'width' => 800,
'height' => 600,
'fit' => 'contain',
],
],
'image_optimizer' => [
'enabled' => true,
'quality' => 85,
],
use Iamgerwin\NovaMediaHub\Nova\Fields\MediaHubField;
class Product extends Resource
{
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name'),
MediaHubField::make('Image', 'image'),
];
}
}
MediaHubField::make('Featured Image', 'featured_image')
->defaultCollection('featured')
->rules('
MediaHubField::make('Gallery', 'gallery_images')
->multiple()
->defaultCollection('galleries')
->max(10);
MediaHubField::make('Product Images', 'images')
->multiple()
->defaultCollection('products')
->filterCollection('products'); // Only show media from this collection
MediaHubField::make('Images', 'images')
->hideFromIndex();
use Iamgerwin\NovaMediaHub\Casts\MediaCast;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $casts = [
'image' => MediaCast::class,
'gallery_images' => MediaCast::class,
];
}
// Get media URL
$url = $product->image->url;
$thumbnailUrl = $product->image->url('thumbnail');
// Get media metadata
$filename = $product->image->filename;
$mimeType = $product->image->mime_type;
$size = $product->image->size;
$collection = $product->image->collection;
// Check media type
$isImage = $product->image->isImage();
$isVideo = $product->image->isVideo();
$isPdf = $product->image->isPdf();
// app/Providers/NovaServiceProvider.php
MediaHub::make()
->withCustomFields([
'copyright' => __('Copyright'),
'photographer' => __('Photographer'),
'license' => __('License'),
]);
use Iamgerwin\NovaMediaHub\MediaHandler\FileHandler;
$fileHandler = app(FileHandler::class);
// Upload from file path
$media = $fileHandler->fromFile('/path/to/file.jpg', 'products', [
'alt' => 'Product image',
'copyright' => '© 2025',
]);
// Upload from URL
$media = $fileHandler->fromUrl('https://example.com/image.jpg', 'external', [
'alt' => 'External image',
]);
// Upload from base64
$media = $fileHandler->fromBase64($base64Data, 'uploads', [
'alt' => 'Base64 image',
]);
use Iamgerwin\NovaMediaHub\Models\Media;
// Move media to another collection
Media::whereCollection('old-collection')
->update(['collection' => 'new-collection']);
// Delete unused media
Media::whereDoesntHave('models')
->where('created_at', '<', now()->subMonths(6))
->delete();
// Get media by type
$images = Media::where('mime_type', 'like', 'image/%')->get();
$videos = Media::where('mime_type', 'like', 'video/%')->get();
// config/nova-media-hub.php
'image_conversions' => [
'thumbnail' => [
'width' => 150,
'height' => 150,
'fit' => 'crop',
],
'square' => [
'width' => 500,
'height' => 500,
'fit' => 'crop',
],
'hero' => [
'width' => 1920,
'height' => 1080,
'fit' => 'contain',
],
],
// config/nova-media-hub.php
'queue_conversions' => true,
'queue_name' => 'media',
// config/nova-media-hub.php
'chunked_upload' => [
'chunk_size' => 10 * 1024 * 1024, // 10MB chunks
'session_lifetime_hours' => 48, // Longer session
'retry_attempts' => 5, // More retry attempts
],
'chunked_upload' => [
'retry_attempts' => 5,
'retry_delay' => 2000, // milliseconds between retries
],
// Chunks are combined using stream_copy_to_stream()
// Memory usage stays constant regardless of file size
// config/cors.php
'paths' => ['nova-vendor/media-hub/*'],
'allowed_methods' => ['POST', 'GET', 'OPTIONS'],
'allowed_origins' => ['https://your-domain.com'],
'allowed_headers' => ['Content-Type', 'X-Requested-With', 'X-CSRF-TOKEN'],
// config/nova-media-hub.php
'allowed_mime_types' => [
'image/jpeg',
'image/png',
'image/webp',
'image/svg+xml',
'video/mp4',
'video/webm',
'application/pdf',
],
'max_file_size' => 50 * 1024 * 1024, // 50MB
namespace Iamgerwin\NovaMediaHub\Tests\Unit;
use Iamgerwin\NovaMediaHub\Tests\TestCase;
class YourFeatureTest extends TestCase
{
/** @test */
public function it_does_something()
{
// Arrange
$input = 'test';
// Act
$result = yourFunction($input);
// Assert
$this->assertEquals('expected', $result);
}
}
// config/nova-media-hub.php
'allowed_mime_types' => [
'image/jpeg',
'image/png',
'image/webp',
'video/mp4',
],
'max_file_size' => 50 * 1024 * 1024, // 50MB
// Define authorization in Nova resource
public static function authorizedToCreate(Request $request)
{
return $request->user()->can('create-media');
}
protected function schedule(Schedule $schedule)
{
// Clean up chunks older than 48 hours, daily at 3am
$schedule->command('media-hub:cleanup-chunks')->dailyAt('03:00');
// Or use cron expression
$schedule->command('media-hub:cleanup-chunks')->cron('0 3 * * *');
}
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Iamgerwin\NovaMediaHub\MediaHubServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Iamgerwin\NovaMediaHub\MediaHubServiceProvider" --tag="translations"
bash
php artisan queue:work --queue=media
tests/
├── TestCase.php # Base test case
└── Unit/
└── PackageTest.php # Package loading tests