PHP code example of carone / laravel-media

1. Go to this page and download the library: Download carone/laravel-media 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/ */

    

carone / laravel-media example snippets


'admin' => [
    'enabled' => true,
    'route_prefix' => 'admin/media', // Customize the admin URL
],

'management_middleware' => ['auth'], // Require authentication
// or
'management_middleware' => ['auth', 'admin'], // Require admin role

use Carone\Media\Facades\Media;

// Store a new media file with tags
$media = Media::store([
    'file' => $uploadedFile,
    'name' => 'My Image',
    'description' => 'A beautiful landscape photo',
    'type' => 'image',
    'tags' => ['Nature', 'Photography', 'Landscape'], // Optional tags
]);

// Get media by ID
$media = Media::getById(1);

// Get media by type with pagination
$result = Media::getByType('image', limit: 10, offset: 0);

// Search media
$result = Media::search('landscape', type: 'image', limit: 5);

// Serve media file
return Media::serve('image', 'filename.jpg');

// Serve thumbnail
return Media::thumbnail('image', 'filename.jpg');

// Delete media
Media::delete(1);

// Delete multiple media files
$result = Media::deleteMultiple([1, 2, 3]);

// Delete all media of a specific type
$result = Media::deleteByType('image');

// Clean up orphaned files
$result = Media::cleanupOrphanedFiles('image');

// Get enabled media types
$types = Media::getEnabledTypes();

return [
    'enabled_types' => [
        'image' => [
            'enabled' => true,
            'max_file_size' => 10240, // KB
            'allowed_extensions' => ['jpg', 'jpeg', 'png', 'gif', 'webp'],
            'thumbnails' => [
                'enabled' => true,
                'width' => 300,
                'height' => 300,
            ],
        ],
        'video' => [
            'enabled' => true,
            'max_file_size' => 102400, // KB
            'allowed_extensions' => ['mp4', 'avi', 'mov', 'wmv'],
        ],
        // ... other types
    ],
    
    'storage' => [
        'disk' => 'public',
        'path' => 'media',
    ],
    
    'tags' => [
        'enabled' => true, // Enable/disable tagging system
    ],
];

use Carone\Media\Facades\Media;

// Upload image with tags
$media = Media::store([
    'type' => 'image',
    'file' => $request->file('image'),
    'name' => 'Sunset Photo',
    'tags' => ['Nature', 'Photography', 'Sunset'], // Tags are automatically created if they don't exist
]);

// Upload external media with tags
$media = Media::store([
    'type' => 'video',
    'source' => 'external',
    'url' => 'https://youtube.com/watch?v=example',
    'name' => 'Tutorial Video',
    'tags' => ['Tutorial', 'Education'],
]);

// Get all tags for a media resource
$tags = $media->tags; // Collection of Tag models

// Get tag names
$tagNames = $media->tags->pluck('name')->toArray();
// ['Nature', 'Photography', 'Sunset']

// Get tag slugs
$tagSlugs = $media->tags->pluck('slug')->toArray();
// ['nature', 'photography', 'sunset']

// Sync tags (replaces existing tags)
$media->syncTags(['New Tag', 'Another Tag']);

// The old tags are removed, only the new ones remain

'tags' => [
    'enabled' => true, // Set to false to disable tagging functionality
],

{
    "id": 1,
    "name": "My Image",
    "description": "A beautiful landscape photo",
    "type": "image",
    "file_name": "image_20231025_123456.jpg",
    "file_size": 2048576,
    "mime_type": "image/jpeg",
    "source": "local",
    "path": "/storage/media/images/image_20231025_123456.jpg",
    "thumbnail_path": "/storage/media/images/thumbnails/image_20231025_123456.jpg",
    "meta_data": {
        "width": 1920,
        "height": 1080,
        "exif": {...}
    },
    "tags": [
        {"id": 1, "name": "Nature", "slug": "nature"},
        {"id": 2, "name": "Photography", "slug": "photography"}
    ],
    "created_at": "2023-10-25T12:34:56.000000Z",
    "updated_at": "2023-10-25T12:34:56.000000Z"
}

try {
    $media = Media::store($data);
} catch (\InvalidArgumentException $e) {
    // Invalid media type or validation failed
} catch (\Exception $e) {
    // Other storage errors
}

use Carone\Media\Actions\StoreMediaAction;

// Upload a local image with tags
$media = StoreMediaAction::run([
    'type' => 'image',
    'file' => $request->file('image'),
    'name' => 'My Beautiful Image',
    'description' => 'A description of the image',
    'tags' => ['Nature', 'Landscape', 'Photography'],
]);

// Upload external media with tags
$media = StoreMediaAction::run([
    'type' => 'video',
    'source' => 'external',
    'url' => 'https://www.youtube.com/watch?v=example',
    'name' => 'External Video',
    'tags' => ['Tutorial', 'Video'],
]);

use Carone\Media\Actions\GetMediaAction;

// Get media by type with pagination
$images = GetMediaAction::byType('image', $limit = 20, $offset = 0);

// Get single media item (

use Carone\Media\Actions\DeleteMediaAction;

// Delete single media (also removes tag associations)
$success = DeleteMediaAction::run($mediaId);

// Bulk delete
$result = DeleteMediaAction::make()->deleteMultiple([1, 2, 3]);

// Before (your existing controller)
public function uploadMedia(MediaUploadRequest $request)
{
    // ... complex upload logic ...
}

// After (using this package)
public function uploadMedia(Request $request)
{
    $media = StoreMediaAction::run([
        'type' => $request->input('type'),
        'file' => $request->file('file'),
        'name' => $request->input('name'),
        'tags' => $request->input('tags', []), // Optional tags
    ]);

    return response()->json([
        'success' => true, 
        'media' => $media,
        'tags' => $media->tags->pluck('name'),
    ]);
}

// config/media.php
'validation' => [
    'image' => ['mimes:jpg,jpeg,png,gif', 'max:5120'], // 5MB max
    'video' => ['mimes:mp4,mov', 'max:51200'], // 50MB max
    'audio' => ['mimes:mp3,wav', 'max:10240'], // 10MB max
    'document' => ['mimes:pdf,doc,docx', 'max:10240'],
],

'disk' => env('DEFAULT_MEDIA_STORAGE_DISK', 'public'), // Main media files disk (not customizable per upload)
'storage_path' => 'media/{path}',
'enabled_types' => ['image', 'video', 'audio', 'document'],

// Thumbnail configuration
'thumbnails' => [
    'enabled' => true,
    'disk' => null, // If null, uses same disk as media. Can be customized per upload.
    'storage_path' => 'media/thumbnails/{path}',
],

// Tags configuration
'tags' => [
    'enabled' => true, // Enable/disable tagging system
],

'management_middleware' => ['auth'], // Default: sanctum'], // Use Sanctum
'management_middleware' => ['auth', 'admin'], // Require admin role
'management_middleware' => [], // No protection (not recommended for production)

try {
    $media = StoreMediaAction::run($data);
} catch (\InvalidArgumentException $e) {
    // Handle validation errors
} catch (\Exception $e) {
    // Handle general errors
}

'image_driver' => env('IMAGE_DRIVER', 'imagick'),

// config/media.php
'processing' => [
    'image' => [
        'scale_oversized_images' => true,     // Enable/disable automatic scaling
        'max_dimension_before_encode' => 3000, // Threshold for scaling
        'scaled_max_dimension' => 2560,        // Target size (2.5K, still HD)
    ],
],

  'quality' => 85,  // 0-100, higher = better quality
  
bash
php artisan vendor:publish --provider="Carone\Media\CaroneMediaServiceProvider" --tag="config"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Carone\Media\CaroneMediaServiceProvider" --tag=config
bash
php artisan migrate
ini
   extension=imagick
   
bash
php artisan media:info
ini
# php.ini
memory_limit = 256M  # Minimum recommended for large images (4000×4000)
bash
php artisan media:info