Skip to content

Commit 5a14740

Browse files
committed
feat: add background color option
1 parent 1d2551c commit 5a14740

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

config/image-transform-url.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
'flip',
4444
'contrast',
4545
'version',
46+
'background',
4647
// 'blur'
4748
]),
4849

src/Enums/AllowedOptions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum AllowedOptions: string
1414
case Contrast = 'contrast';
1515
case Flip = 'flip';
1616
case Version = 'version';
17+
case Background = 'background';
1718

1819
public static function all(): array
1920
{
@@ -31,6 +32,7 @@ public static function withTypes(): array
3132
self::Contrast->value => 'integer',
3233
self::Flip->value => 'string',
3334
self::Version->value => 'integer',
35+
self::Background->value => 'string',
3436
];
3537
}
3638
}

src/Http/Controllers/ImageTransformerController.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ public function __invoke(Request $request, string $options, string $path)
9494
};
9595
}
9696

97+
if (Arr::has($options, 'background')) {
98+
$backgroundColor = $this->getStringOptionValue($options, 'background', 'ffffff');
99+
100+
if (! preg_match('/^([a-f0-9]{6}|[a-f0-9]{3})$/', $backgroundColor)) {
101+
$backgroundColor = null;
102+
}
103+
104+
if ($backgroundColor) {
105+
$image->blendTransparency($backgroundColor);
106+
}
107+
108+
}
109+
97110
// We use the mime type instead of the extension to determine the format, because this is more reliable.
98111
$originalMimetype = File::mimeType($publicPath);
99112

tests/Feature/OptionsTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
declare(strict_types=1);
44

55
use AceOfAces\LaravelImageTransformUrl\Tests\TestCase;
6+
use Illuminate\Support\Facades\Cache;
7+
use Illuminate\Support\Facades\Storage;
68

79
beforeEach(function () {
810
Cache::flush();
@@ -94,6 +96,18 @@
9496
]);
9597
});
9698

99+
it('can process the background option', function () {
100+
/** @var TestCase $this */
101+
$response = $this->get(route('image.transform', [
102+
'options' => 'background=ffaa00',
103+
'path' => 'octocat.png',
104+
]));
105+
106+
expect($response)->toBeImage([
107+
'mime' => 'image/png',
108+
]);
109+
});
110+
97111
it('can process multiple options at once', function () {
98112
/** @var TestCase $this */
99113
$response = $this->get(route('image.transform', [

workbench/test-data/octocat.png

99 KB
Loading

0 commit comments

Comments
 (0)