Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -1295,15 +1295,15 @@ 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;
}

/**
* Set watermark text (Pro)
*/
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;
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

/**
Expand Down
50 changes: 44 additions & 6 deletions tests/Unit/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

use fostercommerce\imgproxy\Options;

// Test creating Options with individual setters
test('can set options with individual setters', function (): void {
$options = createOptions();
$options->setWidth(300)
Expand All @@ -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,
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -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);
});