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

Commit 122567c

Browse files
feat: Allow url-encoding source URLs
1 parent 3949e9e commit 122567c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/Options.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,10 +863,15 @@ public function getCacheBuster(): ?string
863863

864864
/**
865865
* Set filename
866+
*
867+
* @param bool $encode Whether to base64 encode the filename. If true, the filename is base64 encoded. Otherwise, the filename is url-encoded.
868+
*
869+
* Note: This diverges from the imgproxy arguments.
866870
*/
867-
public function setFilename(string $filename): self
871+
public function setFilename(string $filename, bool $encode = true): self
868872
{
869-
$this->options['filename'] = [base64_encode($filename), true];
873+
$this->options['filename'] = $encode ? [base64_encode($filename), true] : [urlencode($filename), false];
874+
870875
return $this;
871876
}
872877

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

881-
return $value !== null ? (base64_decode($value, true) ?: null) : null;
887+
if ($value === null) {
888+
return null;
889+
}
890+
891+
if ($encoded === true) {
892+
return base64_decode($value, true) ?: null;
893+
}
894+
895+
return urldecode($value);
882896
}
883897

884898
/**

tests/Unit/OptionsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,23 @@
9797
expect($options->toString())->toContain("filename:{$encodedFilename}:1");
9898
expect($options->getFilename())->toBe($filename);
9999
});
100+
101+
test('setFilename encodes and decodes correctly with encode=true', function (): void {
102+
$options = createOptions();
103+
$filename = 'test image @ 2024.png';
104+
$options->setFilename($filename, true);
105+
106+
$encoded = base64_encode($filename);
107+
expect($options->toString())->toContain("filename:{$encoded}:1");
108+
expect($options->getFilename())->toBe($filename);
109+
});
110+
111+
test('setFilename stores and decodes correctly with encode=false', function (): void {
112+
$options = createOptions();
113+
$filename = 'test image @ 2024.png';
114+
$options->setFilename($filename, false);
115+
116+
$urlEncoded = urlencode($filename);
117+
expect($options->toString())->toContain("filename:{$urlEncoded}:0");
118+
expect($options->getFilename())->toBe($filename);
119+
});

0 commit comments

Comments
 (0)