Skip to content

Commit 91ffc36

Browse files
committed
Add default option format=auto if no options specified
1 parent 2bdc796 commit 91ffc36

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/CFImageResizing.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public function build(): ?string
5353
// Construct the options string
5454
$options = [];
5555

56+
// if there are no options, we add the default format auto
57+
if (blank($this->options)) {
58+
$this->format('auto');
59+
}
60+
5661
foreach ($this->options as $key => $value) {
5762
$options[] = "{$key}={$value}";
5863
}

tests/CFImageTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
use AneesKhan47\CloudflareImageResizing\CFImageResizing;
44

5-
it('will modify the image url to have /cdn-cgi/image', function () {
5+
it('will modify the image url to have /cdn-cgi/image with default format=auto option', function () {
66
$url = 'https://example.com/image.jpg';
77

88
$cfImage = CFImageResizing::make($url);
99

10-
expect($cfImage->build())->toBe('https://example.com/cdn-cgi/image/image.jpg');
10+
expect($cfImage->build())->toBe('https://example.com/cdn-cgi/image/format=auto/image.jpg');
1111
});
1212

13-
it('will modify the image url to have /cdn-cgi/image if the url has a path', function () {
13+
it('will modify the image url to have /cdn-cgi/image with default format=auto option if the url has a path', function () {
1414
$url = 'https://example.com/uploads/2023/image.jpg';
1515

1616
$cfImage = CFImageResizing::make($url);
1717

18-
expect($cfImage->build())->toBe('https://example.com/cdn-cgi/image/uploads/2023/image.jpg');
18+
expect($cfImage->build())->toBe('https://example.com/cdn-cgi/image/format=auto/uploads/2023/image.jpg');
1919
});
2020

2121
it('will modify the image url to have /cdn-cgi/image with options', function () {
@@ -30,6 +30,17 @@
3030
expect($cfImage)->toBe('https://example.com/cdn-cgi/image/width=300,height=300,format=jpeg/image.jpg');
3131
});
3232

33+
it('will not add the default format=auto option to the image url if any options is provided', function () {
34+
$url = 'https://example.com/image.jpg';
35+
36+
$cfImage = CFImageResizing::make($url)
37+
->width(300)
38+
->height(300)
39+
->build();
40+
41+
expect($cfImage)->toBe('https://example.com/cdn-cgi/image/width=300,height=300/image.jpg');
42+
});
43+
3344
it('will not modify the image url if it already has /cdn-cgi/image', function () {
3445
$url = 'https://example.com/cdn-cgi/image/image.jpg';
3546

0 commit comments

Comments
 (0)