spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.78k stars 1.08k forks source link

Error when using withManipulations in combination with enumerations #3582

Closed tkempf closed 6 months ago

tkempf commented 7 months ago

When adding media with medialibrary v11 using withManipulations, the BackedEnum BorderType gets transformed to its scalar string value before calling the manipulation itself. This leads to the following type error: Spatie\Image\Image::border(): Argument #2 ($type) must be of type Spatie\Image\Enums\BorderType, string given

The code used is as follows: $this->addMedia($params->favicon->file) ->preservingOriginal() ->withManipulations([ 'bgrectangle' => [ 'background' => ['color'=>$favIconbgcolor], 'border' => ['width' => $bordersize,'type' => BorderType::Expand, 'color'=>$bordercolor] ] ]) ->toMediaCollection('favicon');

freekmurze commented 7 months ago

It's accept a PR that fixes this.

tkempf commented 7 months ago

Ok, the problem occurs on FileAdder.php line 331 during $media->manipulations = $this->manipulations; the manipulation get cast to an array as requested in Models/Media.php on Line 74

    protected $casts = [
        'manipulations' => 'array',
        'custom_properties' => 'array',
        'generated_conversions' => 'array',
        'responsive_images' => 'array',
    ];

For my purpose i just need a border, so i made it work for me now by recreating the Enum in Conversions/Manipulations 53 I made a PR but this is just a fast and dirty fix and has to be reviewed toroughly by someone who has a better knwoledge of media-libary or functionality of custom casts in laravel.