Skip to content

Commit 5fc1850

Browse files
feat: Add optional transform URI prefix
1 parent dfeffee commit 5fc1850

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

src/UrlBuilder.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44

55
class UrlBuilder
66
{
7+
/**
8+
* @var ?string
9+
*/
10+
public const TRANSFORM_PATH_PREFIX = 't';
11+
712
/**
813
* @param string $host The host to use for the URL
914
*/
1015
public function __construct(
1116
protected string $host,
1217
protected ?string $secret = null,
18+
protected ?string $transformPathPrefix = self::TRANSFORM_PATH_PREFIX,
1319
) {
1420
}
1521

@@ -26,7 +32,8 @@ public function buildUrl(string $sourceUrl, ?Options $options = null): string
2632
$options ??= new Options();
2733
$optionsString = (string) $options;
2834

29-
$unsignedUrl = "{$this->host}/{$sourceUrl}?{$optionsString}";
35+
$transformPath = $this->transformPathPrefix !== null && $this->transformPathPrefix !== '' ? "/{$this->transformPathPrefix}" : '';
36+
$unsignedUrl = "{$this->host}{$transformPath}/{$sourceUrl}?{$optionsString}";
3037

3138
if ($this->secret === null) {
3239
return $unsignedUrl;

tests/Unit/UrlBuilderTest.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
$builder = new UrlBuilder('https://images.example.com');
1414
$url = $builder->buildUrl('images/image.jpg', $options);
1515

16-
expect($url)->toBe('https://images.example.com/images/image.jpg?w=300&h=400&fit=fill&markpos=center');
16+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?w=300&h=400&fit=fill&markpos=center');
1717
});
1818

1919
test('can build a basic URL without any options', function (): void {
@@ -22,14 +22,14 @@
2222
$builder = new UrlBuilder('https://images.example.com');
2323
$url = $builder->buildUrl('images/image.jpg', $options);
2424

25-
expect($url)->toBe('https://images.example.com/images/image.jpg?');
25+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?');
2626
});
2727

2828
test('can build URL with null options', function (): void {
2929
$builder = new UrlBuilder('https://images.example.com');
3030
$url = $builder->buildUrl('images/image.jpg', null);
3131

32-
expect($url)->toBe('https://images.example.com/images/image.jpg?');
32+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?');
3333
});
3434

3535
test('can handle URLs with leading/trailing slashes', function (): void {
@@ -40,7 +40,7 @@
4040
$builder = new UrlBuilder('https://images.example.com');
4141
$url = $builder->buildUrl('/images/image.jpg/', $options);
4242

43-
expect($url)->toBe('https://images.example.com/images/image.jpg?w=300');
43+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?w=300');
4444
});
4545

4646
test('can build URL with quality and format options', function (): void {
@@ -54,7 +54,7 @@
5454
$builder = new UrlBuilder('https://images.example.com');
5555
$url = $builder->buildUrl('images/image.jpg', $options);
5656

57-
expect($url)->toBe('https://images.example.com/images/image.jpg?w=300&h=400&q=85&fm=webp');
57+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?w=300&h=400&q=85&fm=webp');
5858
});
5959

6060
test('can build URL with watermark options', function (): void {
@@ -68,7 +68,7 @@
6868
$builder = new UrlBuilder('https://images.example.com');
6969
$url = $builder->buildUrl('images/image.jpg', $options);
7070

71-
expect($url)->toBe('https://images.example.com/images/image.jpg?w=300&h=400&markpos=center&markalpha=50');
71+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?w=300&h=400&markpos=center&markalpha=50');
7272
});
7373

7474
test('can build URL with enhancement options', function (): void {
@@ -84,7 +84,7 @@
8484
$builder = new UrlBuilder('https://images.example.com');
8585
$url = $builder->buildUrl('images/image.jpg', $options);
8686

87-
expect($url)->toBe('https://images.example.com/images/image.jpg?w=300&h=400&bri=10&con=15&sharp=3&blur=2');
87+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?w=300&h=400&bri=10&con=15&sharp=3&blur=2');
8888
});
8989

9090
test('can build URL with filter options', function (): void {
@@ -97,7 +97,7 @@
9797
$builder = new UrlBuilder('https://images.example.com');
9898
$url = $builder->buildUrl('images/image.jpg', $options);
9999

100-
expect($url)->toBe('https://images.example.com/images/image.jpg?w=300&h=400&filt=grayscale');
100+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?w=300&h=400&filt=grayscale');
101101
});
102102

103103
test('can build URL with crop options', function (): void {
@@ -107,7 +107,7 @@
107107
$builder = new UrlBuilder('https://images.example.com');
108108
$url = $builder->buildUrl('images/image.jpg', $options);
109109

110-
expect($url)->toBe('https://images.example.com/images/image.jpg?crop=100,200,10,20');
110+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?crop=100,200,10,20');
111111
});
112112

113113
test('can build URL with background and border options', function (): void {
@@ -118,7 +118,7 @@
118118
$builder = new UrlBuilder('https://images.example.com');
119119
$url = $builder->buildUrl('images/image.jpg', $options);
120120

121-
expect($url)->toBe('https://images.example.com/images/image.jpg?bg=ffffff&border=5,ff0000,overlay');
121+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?bg=ffffff&border=5,ff0000,overlay');
122122
});
123123

124124
test('can build URL with boolean options', function (): void {
@@ -128,7 +128,7 @@
128128
$builder = new UrlBuilder('https://images.example.com');
129129
$url = $builder->buildUrl('images/image.jpg', $options);
130130

131-
expect($url)->toBe('https://images.example.com/images/image.jpg?interlace=1');
131+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?interlace=1');
132132
});
133133

134134
test('can build URL with fit options', function (): void {
@@ -138,7 +138,7 @@
138138
$builder = new UrlBuilder('https://images.example.com');
139139
$url = $builder->buildUrl('images/image.jpg', $options);
140140

141-
expect($url)->toBe('https://images.example.com/images/image.jpg?fit=contain');
141+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?fit=contain');
142142
});
143143

144144
test('can build URL with orientation and flip options', function (): void {
@@ -149,7 +149,7 @@
149149
$builder = new UrlBuilder('https://images.example.com');
150150
$url = $builder->buildUrl('images/image.jpg', $options);
151151

152-
expect($url)->toBe('https://images.example.com/images/image.jpg?or=90&flip=h');
152+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?or=90&flip=h');
153153
});
154154

155155
test('can build URL with device pixel ratio', function (): void {
@@ -159,7 +159,7 @@
159159
$builder = new UrlBuilder('https://images.example.com');
160160
$url = $builder->buildUrl('images/image.jpg', $options);
161161

162-
expect($url)->toBe('https://images.example.com/images/image.jpg?dpr=2');
162+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?dpr=2');
163163
});
164164

165165
test('can build URL with gamma adjustment', function (): void {
@@ -169,7 +169,7 @@
169169
$builder = new UrlBuilder('https://images.example.com');
170170
$url = $builder->buildUrl('images/image.jpg', $options);
171171

172-
expect($url)->toBe('https://images.example.com/images/image.jpg?gam=1.5');
172+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?gam=1.5');
173173
});
174174

175175
test('can build URL with pixelate effect', function (): void {
@@ -179,7 +179,7 @@
179179
$builder = new UrlBuilder('https://images.example.com');
180180
$url = $builder->buildUrl('images/image.jpg', $options);
181181

182-
expect($url)->toBe('https://images.example.com/images/image.jpg?pixel=10');
182+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?pixel=10');
183183
});
184184

185185
test('can build URL with watermark path', function (): void {
@@ -189,7 +189,7 @@
189189
$builder = new UrlBuilder('https://images.example.com');
190190
$url = $builder->buildUrl('images/image.jpg', $options);
191191

192-
expect($url)->toBe('https://images.example.com/images/image.jpg?mark=/watermarks/logo.png');
192+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?mark=/watermarks/logo.png');
193193
});
194194

195195
test('can build URL with watermark dimensions', function (): void {
@@ -200,7 +200,7 @@
200200
$builder = new UrlBuilder('https://images.example.com');
201201
$url = $builder->buildUrl('images/image.jpg', $options);
202202

203-
expect($url)->toBe('https://images.example.com/images/image.jpg?markw=100&markh=50');
203+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?markw=100&markh=50');
204204
});
205205

206206
test('can build URL with watermark offsets and padding', function (): void {
@@ -212,7 +212,7 @@
212212
$builder = new UrlBuilder('https://images.example.com');
213213
$url = $builder->buildUrl('images/image.jpg', $options);
214214

215-
expect($url)->toBe('https://images.example.com/images/image.jpg?markx=10&marky=20&markpad=5');
215+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?markx=10&marky=20&markpad=5');
216216
});
217217

218218
test('can build URL with watermark fit', function (): void {
@@ -222,7 +222,7 @@
222222
$builder = new UrlBuilder('https://images.example.com');
223223
$url = $builder->buildUrl('images/image.jpg', $options);
224224

225-
expect($url)->toBe('https://images.example.com/images/image.jpg?markfit=contain');
225+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?markfit=contain');
226226
});
227227

228228
test('can generate signed URLs', function (): void {
@@ -236,7 +236,7 @@
236236
$builder = new UrlBuilder('https://images.example.com', $secret);
237237
$url = $builder->buildUrl('images/image.jpg', $options);
238238

239-
expect($url)->toBe('https://images.example.com/images/image.jpg?w=300&h=400&signature=S0b0bvBhc0kh2L6WRhcYGaRVT1LsuzWwONsdROoBk');
239+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?w=300&h=400&signature=Qaqt9agh3JLg4LeWmRqqd9qnD9bjPbVqSzrXAXTaY');
240240
});
241241

242242
test('can generate complex signed URLs', function (): void {
@@ -251,5 +251,5 @@
251251
$builder = new UrlBuilder('https://images.example.com', $secret);
252252
$url = $builder->buildUrl('images/image.jpg', $options);
253253

254-
expect($url)->toBe('https://images.example.com/images/image.jpg?border=5,ff0000,overlay&q=80&fm=png&signature=NBzER5uyLXXVZGFMAXWjthvfYWCVaj754FoMWujdaeI');
254+
expect($url)->toBe('https://images.example.com/t/images/image.jpg?border=5,ff0000,overlay&q=80&fm=png&signature=hE2e5tLayrNP0pgSaxJLFOMfi0PES2erIzhNYR7QqQ');
255255
});

0 commit comments

Comments
 (0)