Search code examples
laravellaravel-medialibrary

How to add image to specific Media Collection Conversion with Laravel-medialibrary?


I have two input image in my form, and one registerMediaConversions :

 public function registerMediaConversions(Media $media = null) : void
    {
        $this->addMediaConversion('big')
            ->performOnCollections('category-cover');

        $this->addMediaConversion('medium')
            ->crop(Manipulations::CROP_CENTER, 645, 300)
            ->performOnCollections('category-cover');

        $this->addMediaConversion('small')
            ->crop(Manipulations::CROP_CENTER, 300, 300)
            ->performOnCollections('category-cover');

        $this->addMediaConversion('thumb')
            ->width(100)
            ->sharpen(10)
            ->performOnCollections('category-cover');

    }

i want the first input do the big, medium and thumb conversions , and the second input do the small conversion, is this possible with laravel media-library ?


Solution

  • Yes, it is possible. To do that you must define collections as same as your input in your model. For example:

    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('category-cover');
        $this->addMediaCollection('categories-another-collection');
    }
    

    Then You need to register media conversion for 2 different collections as follows:

    public function registerMediaConversions(Media $media = null) : void
    {
        $this->addMediaConversion('big')
            ->performOnCollections('category-cover');
    
        $this->addMediaConversion('medium')
            ->crop(Manipulations::CROP_CENTER, 645, 300)
            ->performOnCollections('category-cover');
    
        $this->addMediaConversion('small')
            ->crop(Manipulations::CROP_CENTER, 300, 300)
            ->performOnCollections('categories-another-collection');
    
        $this->addMediaConversion('thumb')
            ->width(100)
            ->sharpen(10)
            ->performOnCollections('category-cover');
    }
    

    Let say, in your controller taking 2 field request as follows:

        $validator = Validator::make($request->all(), [
            'files1' => 'required',
            'files1.*' => 'image|mimes:jpg,jpeg,png,gif|max:2048',
            'files2' => 'required',
            'files2.*' => 'image|mimes:jpg,jpeg,png,gif|max:2048',
        ]);
    

    Then you can able to save those 2 types of file by:

        if ($request->hasFile('files1')) {
            $fileAdders = $categoryModel->addMultipleMediaFromRequest(['images'])
                ->each(function ($fileAdder) {
                    $fileAdder->toMediaCollection('category-cover');
                });
        }
    
        if ($request->hasFile('files2')) {
            $fileAdders = $categoryModel->addMultipleMediaFromRequest(['images'])
                ->each(function ($fileAdder) {
                    $fileAdder->toMediaCollection('categories-another-collection');
                });
        }
        
    

    At the end the files1 input request will use big, medium and thumb conversions based on collection(category-cover) and files2 input request will use only small conversion based on collection('categories-another-collection')