Skip to content

Commit

Permalink
Merge pull request #3584 from tkempf/manipulationfix
Browse files Browse the repository at this point in the history
recreate BackedEnums when applying Manipulations
  • Loading branch information
timvandijck authored May 24, 2024
2 parents d6daf18 + 152d9d2 commit 3afe621
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Conversions/Manipulations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
namespace Spatie\MediaLibrary\Conversions;

use Spatie\Image\Drivers\ImageDriver;
use Spatie\Image\Enums\BorderType;
use Spatie\Image\Enums\AlignPosition;
use Spatie\Image\Enums\ColorFormat;
use Spatie\Image\Enums\Constraint;
use Spatie\Image\Enums\CropPosition;
use Spatie\Image\Enums\Fit;
use Spatie\Image\Enums\FlipDirection;


/** @mixin \Spatie\Image\Drivers\ImageDriver */
class Manipulations
Expand Down Expand Up @@ -52,6 +60,17 @@ public function isEmpty(): bool
public function apply(ImageDriver $image): void
{
foreach ($this->manipulations as $manipulationName => $parameters) {
match($manipulationName){
'border' => (isset($parameters['type'])) && $parameters['type'] = BorderType::from($parameters['type']),
'watermark' => (isset($parameters['fit'])) && $parameters['fit'] = Fit::from($parameters['fit']),
'watermark','resizeCanvas','insert' => (isset($parameters['position'])) && $parameters['position'] = AlignPosition::from($parameters['position']),
'pickColor' => (isset($parameters['colorFormat'])) && $parameters['colorFormat'] = ColorFormat::from($parameters['colorFormat']),
'resize','width','height' => (isset($parameters['constraints'])) && $parameters['constraints'] = Constraint::from($parameters['constraints']),
'crop' => (isset($parameters['position'])) && $parameters['position'] = CropPosition::from($parameters['position']),
'fit' => (isset($parameters['fit'])) && $parameters['fit'] = Fit::from($parameters['fit']),
'flip' => (isset($parameters['flip'])) && $parameters['flip'] = FlipDirection::from($parameters['flip']),
default => ''
};
$image->$manipulationName(...$parameters);
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/FileAdder/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Tests\TestSupport\RenameOriginalFileNamer;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModel;
use Spatie\Image\Enums\BorderType;
use Symfony\Component\HttpFoundation\File\UploadedFile;

it('can add an file to the default collection', function () {
Expand Down Expand Up @@ -451,6 +452,18 @@
expect($media->manipulations['thumb']['width'])->toEqual(['10']);
});

it('can add manipulations with parameters of type Enum to the saved media', function () {
$media = $this->testModelWithConversion
->addMedia($this->getTestJpg())
->preservingOriginal()
->withManipulations(['thumb' => [
'background' => ['color'=>'ffffff'],
'border' =>['width'=>15, 'type'=>BorderType::Shrink, 'color'=>'000000']
]
])
->toMediaCollection();
})->throwsNoExceptions();

it('can add file to model with morph map', function () {
$media = $this->testModelWithMorphMap
->addMedia($this->getTestJpg())
Expand Down

0 comments on commit 3afe621

Please sign in to comment.