diff --git a/src/Options.php b/src/Options.php index 70685d6..909f199 100644 --- a/src/Options.php +++ b/src/Options.php @@ -866,7 +866,7 @@ public function getCacheBuster(): ?string */ public function setFilename(string $filename): self { - $this->options['filename'] = [$filename]; + $this->options['filename'] = [base64_encode($filename), true]; return $this; } @@ -878,7 +878,7 @@ public function getFilename(): ?string /** @var ?string $value */ $value = $this->options['filename'][0] ?? null; - return $value; + return $value !== null ? (base64_decode($value, true) ?: null) : null; } /** @@ -1283,7 +1283,7 @@ public function getGradient(): ?array */ public function setWatermarkUrl(string $url): self { - $this->options['watermark_url'] = [$url]; + $this->options['watermark_url'] = [base64_encode($url)]; return $this; } @@ -1295,7 +1295,7 @@ public function getWatermarkUrl(): ?string /** @var ?string $value */ $value = $this->options['watermark_url'][0] ?? null; - return $value; + return $value !== null ? (base64_decode($value, true) ?: null) : null; } /** @@ -1303,7 +1303,7 @@ public function getWatermarkUrl(): ?string */ public function setWatermarkText(string $text, ?string $font = null, ?float $fontSize = null, ?string $color = null, ?bool $wrap = null): self { - $values = [$text]; + $values = [base64_encode($text)]; if ($font !== null) { $values[] = $font; } @@ -1331,7 +1331,15 @@ public function setWatermarkText(string $text, ?string $font = null, ?float $fon */ public function getWatermarkText(): ?array { - return $this->options['watermark_text'] ?? null; + $options = $this->options['watermark_text'] ?? null; + + if ($options !== null && isset($options[0])) { + /** @var string $value */ + $value = $options[0]; + $options[0] = (base64_decode($value, true) ?: null); + } + + return $options; } /** @@ -1411,7 +1419,7 @@ public function getWatermarkShadow(): ?array */ public function setStyle(string $style): self { - $this->options['style'] = [$style]; + $this->options['style'] = [base64_encode($style)]; return $this; } @@ -1423,7 +1431,7 @@ public function getStyle(): ?string /** @var ?string $value */ $value = $this->options['style'][0] ?? null; - return $value; + return $value !== null ? (base64_decode($value, true) ?: null) : null; } /** @@ -1735,7 +1743,7 @@ public function getVideoThumbnailAnimation(): ?array */ public function setFallbackImageUrl(string $url): self { - $this->options['fallback_image_url'] = [$url]; + $this->options['fallback_image_url'] = [base64_encode($url)]; return $this; } @@ -1747,7 +1755,7 @@ public function getFallbackImageUrl(): ?string /** @var ?string $value */ $value = $this->options['fallback_image_url'][0] ?? null; - return $value; + return $value !== null ? (base64_decode($value, true) ?: null) : null; } /** diff --git a/tests/Unit/OptionsTest.php b/tests/Unit/OptionsTest.php index d9cf63a..18f0da4 100644 --- a/tests/Unit/OptionsTest.php +++ b/tests/Unit/OptionsTest.php @@ -1,8 +1,6 @@ setWidth(300) @@ -14,7 +12,6 @@ expect($options->toString())->toBe('width:300/height:400/resizing_type:fill/gravity:sm'); }); -// Test creating Options with constructor array test('can set options with constructor array', function (): void { $options = createOptions([ 'width' => 300, @@ -27,7 +24,6 @@ expect($options->toString())->toBe('width:300/height:400/resizing_type:fill/gravity:sm'); }); -// Test complex options test('can handle complex options', function (): void { $options = createOptions(); $options->setPreset('sharp') @@ -41,7 +37,6 @@ 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'); }); -// Test toString method and __toString magic method test('magic __toString behaves like toString method', function (): void { $options = createOptions(); $options->setPreset('sharp') @@ -50,7 +45,6 @@ expect($options->toString())->toBe((string) $options); }); -// Test boolean value conversion test('boolean values are converted to 1 and 0', function (): void { $options = createOptions(); $options->setEnlarge(true) @@ -59,3 +53,47 @@ expect($options->toString())->toBe('enlarge:1/auto_rotate:0/strip_metadata:1'); }); + +test('options requiring base64 encoding are properly encoded', function (): void { + $options = createOptions(); + + // Test watermark URL + $watermarkUrl = 'https://example.com/watermark.png'; + $options->setWatermarkUrl($watermarkUrl); + + $encodedUrl = base64_encode($watermarkUrl); + expect($options->toString())->toContain("watermark_url:{$encodedUrl}"); + expect($options->getWatermarkUrl())->toBe($watermarkUrl); + + // Test watermark text + $text = 'Sample watermark text'; + $options->setWatermarkText($text); + + $encodedText = base64_encode($text); + expect($options->toString())->toContain("watermark_text:{$encodedText}"); + expect($options->getWatermarkText()[0])->toBe($text); + + // Test style + $style = 'filter:blur(10px)'; + $options->setStyle($style); + + $encodedStyle = base64_encode($style); + expect($options->toString())->toContain("style:{$encodedStyle}"); + expect($options->getStyle())->toBe($style); + + // Test fallback image URL + $fallbackUrl = 'https://example.com/fallback.png'; + $options->setFallbackImageUrl($fallbackUrl); + + $encodedFallbackUrl = base64_encode($fallbackUrl); + expect($options->toString())->toContain("fallback_image_url:{$encodedFallbackUrl}"); + expect($options->getFallbackImageUrl())->toBe($fallbackUrl); + + // Test filename with encoded flag + $filename = 'image.png'; + $options->setFilename($filename); + + $encodedFilename = base64_encode($filename); + expect($options->toString())->toContain("filename:{$encodedFilename}:1"); + expect($options->getFilename())->toBe($filename); +});