aammui/laravel-media

File and Image uploader for laravel.

v3.0.0 2023-02-16 16:14 UTC

This package is auto-updated.

Last update: 2024-11-03 20:43:29 UTC


README

Laravel Media Handler package.

Content

  1. Installation
  2. Publish Assets and Migration
  3. Use Trait in model
  4. Call from anywhere
  5. Standalone Media

Installation

composer require aammui/laravel-media

Laravel Compatibility

Publish assests

php artisan vendor:publish --provider="Aammui\LaravelMedia\LaravelMediaServiceProvider"
php artisan migrate

Add HasMedia Trait to your model.

use Aammui\LaravelMedia\Traits\HasMedia;
use Illuminate\Database\Eloquent\Model;
 
class Document extends Model
{
    use HasMedia;
}

Use with model form whereever.

$user->addMedia(request()->file);
$user->toCollection('profilePicture')
    ->addMedia(request()->file);
$user->toCollection('profilePicture')
     ->toDisk('public')
    ->addMedia(request()->file);
$user->toCollection('profilePicture')
     ->toDisk('public')
    ->addMedia(request()->file);

Add Media From URL

This doesnot download media into your storage. It just add url to the database and when your application serve, media will load from remote url.

$user->addMediaFromUrl('http://example.com/image.jpeg');
Download and Add Media From Remote URL.
$user->setDownloadTrue()
    ->addMediaFromUrl('http://example.com/image.jpeg');

Retrive media from your model

$user->getMedia();
$user->fromCollection('profilePicture')
    ->fromDisk('local')
    ->getMedia();

Setup Responsive images

The standard responsive image size can be defined into the configuration file. The original image will be resized into the different sizes defined in configuration.

use Aammui\LaravelMedia\Enum\Responsive;

'responsive'=>[
   'responsive' => [
        Responsive::SM => [
            'w' => 50,
            'h' => 50,
        ],
        Responsive::MD => [
            'w' => 150,
            'h' => 150,
        ],
        Responsive::LG => [
            'w' => 600, // Can define either height or width only.
        ],
    ],
];
Only Particular size of image:
use Aammui\LaravelMedia\Facades\Media;
use Aammui\LaravelMedia\Enum\Responsive;

Media::ofSize([Responsive::SM,Responsive::MD])
    ->addMedia(request()->file);

Enable Image optimization

'optimize'=>true

Standalone Media

Suppose you want to add file which doesnot belong to any model, then you can create standalone media. Use case could be you are storing images in description of some products in ecommerce shop, Where you simply store images in database before the product form is submitted via api, which returns public image url.

use Aammui\LaravelMedia\Facades\Media; // Use Media facade instead

Media::addMedia(request()->file); // returns Media Object
Media::toCollection('profilePicture')
    ->addMedia(request()->file); // returns Media Object while storing grouping files in profilepicture
Media::toCollection('profilePicture')
    ->toStorage('s3')
    ->addMedia(request()->file); // returns Media Object while storing files in s3