Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.

Commit 6dbd13a

Browse files
fix: Ensure options requiring base64 encoding are properly encoded
1 parent de137e6 commit 6dbd13a

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

src/Options.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ public function getCacheBuster(): ?string
866866
*/
867867
public function setFilename(string $filename): self
868868
{
869-
$this->options['filename'] = [$filename];
869+
$this->options['filename'] = [base64_encode($filename), true];
870870
return $this;
871871
}
872872

@@ -878,7 +878,7 @@ public function getFilename(): ?string
878878
/** @var ?string $value */
879879
$value = $this->options['filename'][0] ?? null;
880880

881-
return $value;
881+
return $value !== null ? (base64_decode($value, true) ?: null) : null;
882882
}
883883

884884
/**
@@ -1283,7 +1283,7 @@ public function getGradient(): ?array
12831283
*/
12841284
public function setWatermarkUrl(string $url): self
12851285
{
1286-
$this->options['watermark_url'] = [$url];
1286+
$this->options['watermark_url'] = [base64_encode($url)];
12871287
return $this;
12881288
}
12891289

@@ -1295,15 +1295,15 @@ public function getWatermarkUrl(): ?string
12951295
/** @var ?string $value */
12961296
$value = $this->options['watermark_url'][0] ?? null;
12971297

1298-
return $value;
1298+
return $value !== null ? (base64_decode($value, true) ?: null) : null;
12991299
}
13001300

13011301
/**
13021302
* Set watermark text (Pro)
13031303
*/
13041304
public function setWatermarkText(string $text, ?string $font = null, ?float $fontSize = null, ?string $color = null, ?bool $wrap = null): self
13051305
{
1306-
$values = [$text];
1306+
$values = [base64_encode($text)];
13071307
if ($font !== null) {
13081308
$values[] = $font;
13091309
}
@@ -1331,7 +1331,15 @@ public function setWatermarkText(string $text, ?string $font = null, ?float $fon
13311331
*/
13321332
public function getWatermarkText(): ?array
13331333
{
1334-
return $this->options['watermark_text'] ?? null;
1334+
$options = $this->options['watermark_text'] ?? null;
1335+
1336+
if ($options !== null && isset($options[0])) {
1337+
/** @var string $value */
1338+
$value = $options[0];
1339+
$options[0] = (base64_decode($value, true) ?: null);
1340+
}
1341+
1342+
return $options;
13351343
}
13361344

13371345
/**
@@ -1411,7 +1419,7 @@ public function getWatermarkShadow(): ?array
14111419
*/
14121420
public function setStyle(string $style): self
14131421
{
1414-
$this->options['style'] = [$style];
1422+
$this->options['style'] = [base64_encode($style)];
14151423
return $this;
14161424
}
14171425

@@ -1423,7 +1431,7 @@ public function getStyle(): ?string
14231431
/** @var ?string $value */
14241432
$value = $this->options['style'][0] ?? null;
14251433

1426-
return $value;
1434+
return $value !== null ? (base64_decode($value, true) ?: null) : null;
14271435
}
14281436

14291437
/**
@@ -1735,7 +1743,7 @@ public function getVideoThumbnailAnimation(): ?array
17351743
*/
17361744
public function setFallbackImageUrl(string $url): self
17371745
{
1738-
$this->options['fallback_image_url'] = [$url];
1746+
$this->options['fallback_image_url'] = [base64_encode($url)];
17391747
return $this;
17401748
}
17411749

@@ -1747,7 +1755,7 @@ public function getFallbackImageUrl(): ?string
17471755
/** @var ?string $value */
17481756
$value = $this->options['fallback_image_url'][0] ?? null;
17491757

1750-
return $value;
1758+
return $value !== null ? (base64_decode($value, true) ?: null) : null;
17511759
}
17521760

17531761
/**

tests/Unit/OptionsTest.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22

3-
use fostercommerce\imgproxy\Options;
43

5-
// Test creating Options with individual setters
64
test('can set options with individual setters', function (): void {
75
$options = createOptions();
86
$options->setWidth(300)
@@ -14,7 +12,6 @@
1412
expect($options->toString())->toBe('width:300/height:400/resizing_type:fill/gravity:sm');
1513
});
1614

17-
// Test creating Options with constructor array
1815
test('can set options with constructor array', function (): void {
1916
$options = createOptions([
2017
'width' => 300,
@@ -27,7 +24,6 @@
2724
expect($options->toString())->toBe('width:300/height:400/resizing_type:fill/gravity:sm');
2825
});
2926

30-
// Test complex options
3127
test('can handle complex options', function (): void {
3228
$options = createOptions();
3329
$options->setPreset('sharp')
@@ -41,7 +37,6 @@
4137
expect($options->toString())->toBe('preset:sharp/resize:fill:300:400:0/gravity:sm/watermark:0.5:ce:10:10:0.2/quality:80/format:png');
4238
});
4339

44-
// Test toString method and __toString magic method
4540
test('magic __toString behaves like toString method', function (): void {
4641
$options = createOptions();
4742
$options->setPreset('sharp')
@@ -50,7 +45,6 @@
5045
expect($options->toString())->toBe((string) $options);
5146
});
5247

53-
// Test boolean value conversion
5448
test('boolean values are converted to 1 and 0', function (): void {
5549
$options = createOptions();
5650
$options->setEnlarge(true)
@@ -59,3 +53,47 @@
5953

6054
expect($options->toString())->toBe('enlarge:1/auto_rotate:0/strip_metadata:1');
6155
});
56+
57+
test('options requiring base64 encoding are properly encoded', function (): void {
58+
$options = createOptions();
59+
60+
// Test watermark URL
61+
$watermarkUrl = 'https://example.com/watermark.png';
62+
$options->setWatermarkUrl($watermarkUrl);
63+
64+
$encodedUrl = base64_encode($watermarkUrl);
65+
expect($options->toString())->toContain("watermark_url:{$encodedUrl}");
66+
expect($options->getWatermarkUrl())->toBe($watermarkUrl);
67+
68+
// Test watermark text
69+
$text = 'Sample watermark text';
70+
$options->setWatermarkText($text);
71+
72+
$encodedText = base64_encode($text);
73+
expect($options->toString())->toContain("watermark_text:{$encodedText}");
74+
expect($options->getWatermarkText()[0])->toBe($text);
75+
76+
// Test style
77+
$style = 'filter:blur(10px)';
78+
$options->setStyle($style);
79+
80+
$encodedStyle = base64_encode($style);
81+
expect($options->toString())->toContain("style:{$encodedStyle}");
82+
expect($options->getStyle())->toBe($style);
83+
84+
// Test fallback image URL
85+
$fallbackUrl = 'https://example.com/fallback.png';
86+
$options->setFallbackImageUrl($fallbackUrl);
87+
88+
$encodedFallbackUrl = base64_encode($fallbackUrl);
89+
expect($options->toString())->toContain("fallback_image_url:{$encodedFallbackUrl}");
90+
expect($options->getFallbackImageUrl())->toBe($fallbackUrl);
91+
92+
// Test filename with encoded flag
93+
$filename = 'image.png';
94+
$options->setFilename($filename);
95+
96+
$encodedFilename = base64_encode($filename);
97+
expect($options->toString())->toContain("filename:{$encodedFilename}:1");
98+
expect($options->getFilename())->toBe($filename);
99+
});

0 commit comments

Comments
 (0)