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
20 changes: 17 additions & 3 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -863,10 +863,15 @@ public function getCacheBuster(): ?string

/**
* Set filename
*
* @param bool $encode Whether to base64 encode the filename. If true, the filename is base64 encoded. Otherwise, the filename is url-encoded.
*
* Note: This diverges from the imgproxy arguments.
*/
public function setFilename(string $filename): self
public function setFilename(string $filename, bool $encode = true): self
{
$this->options['filename'] = [base64_encode($filename), true];
$this->options['filename'] = $encode ? [base64_encode($filename), true] : [urlencode($filename), false];

return $this;
}

Expand All @@ -877,8 +882,17 @@ public function getFilename(): ?string
{
/** @var ?string $value */
$value = $this->options['filename'][0] ?? null;
$encoded = $this->options['filename'][1] ?? null;

return $value !== null ? (base64_decode($value, true) ?: null) : null;
if ($value === null) {
return null;
}

if ($encoded === true) {
return base64_decode($value, true) ?: null;
}

return urldecode($value);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,23 @@
expect($options->toString())->toContain("filename:{$encodedFilename}:1");
expect($options->getFilename())->toBe($filename);
});

test('setFilename encodes and decodes correctly with encode=true', function (): void {
$options = createOptions();
$filename = 'test image @ 2024.png';
$options->setFilename($filename, true);

$encoded = base64_encode($filename);
expect($options->toString())->toContain("filename:{$encoded}:1");
expect($options->getFilename())->toBe($filename);
});

test('setFilename stores and decodes correctly with encode=false', function (): void {
$options = createOptions();
$filename = 'test image @ 2024.png';
$options->setFilename($filename, false);

$urlEncoded = urlencode($filename);
expect($options->toString())->toContain("filename:{$urlEncoded}:0");
expect($options->getFilename())->toBe($filename);
});