Skip to content

Commit 33d8876

Browse files
committed
feat: add make and url methods to programmatically generate unsigned URLs
1 parent 76ca1db commit 33d8876

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

src/Facades/ImageTransformUrl.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use Illuminate\Support\Facades\Facade;
88

99
/**
10-
* @see \AceOfAces\LaravelImageTransformUrl\LaravelImageTransformUrl;
11-
*
10+
* @method static string make(string $path, array|string $options = [], ?string $pathPrefix = null)
11+
* @method static string url(string $path, array|string $options = [], ?string $pathPrefix = null)
1212
* @method static string signedUrl(string $path, array|string $options = [], ?string $pathPrefix = null, \DateTimeInterface|\DateInterval|int|null $expiration = null, ?bool $absolute = true)
1313
* @method static string temporarySignedUrl(string $path, array|string $options = [], \DateTimeInterface|\DateInterval|int $expiration, ?string $pathPrefix = null, ?bool $absolute = true)
14+
*
15+
* @see \AceOfAces\LaravelImageTransformUrl\ImageTransformUrl
1416
*/
1517
class ImageTransformUrl extends Facade
1618
{

src/ImageTransformUrl.php

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,43 @@
1111

1212
class ImageTransformUrl
1313
{
14+
/**
15+
* Generate a regular URL for the image transformation.
16+
*
17+
* @param string $path The path to the image.
18+
* @param array|string $options The transformation options.
19+
* @param string|null $pathPrefix The path prefix to use. Defaults to the default path prefix.
20+
* @return string The generated URL.
21+
*/
22+
public function make(string $path, array|string $options = [], ?string $pathPrefix = null): string
23+
{
24+
$options = $this->optionsToString($options);
25+
26+
if (empty($pathPrefix)) {
27+
return URL::route('image.transform.default', ['options' => $options, 'path' => $path]);
28+
}
29+
30+
return URL::route('image.transform', ['pathPrefix' => $pathPrefix, 'options' => $options, 'path' => $path]);
31+
}
32+
33+
/**
34+
* Generate a regular URL for the image transformation.
35+
*
36+
* @param string $path The path to the image.
37+
* @param array|string $options The transformation options.
38+
* @param string|null $pathPrefix The path prefix to use. Defaults to the default path prefix.
39+
* @return string The generated URL.
40+
*/
41+
public function url(string $path, array|string $options = [], ?string $pathPrefix = null): string
42+
{
43+
return $this->make($path, $options, $pathPrefix);
44+
}
45+
1446
/**
1547
* Generate a signed URL for the image transformation.
1648
*
1749
* @param string $path The path to the image.
18-
* @param array $options The transformation options.
50+
* @param array|string $options The transformation options.
1951
* @param string|null $pathPrefix The path prefix to use. Defaults to the default path prefix.
2052
* @param DateTimeInterface|\DateInterval|int|null $expiration The expiration time for the signed URL.
2153
* @return string The signed URL.
@@ -28,11 +60,7 @@ public function signedUrl(string $path, array|string $options = [], ?string $pat
2860
throw new InvalidConfigurationException('Signed URLs are not enabled. Please check your configuration.');
2961
}
3062

31-
if (is_array($options)) {
32-
$options = collect($options)
33-
->map(fn ($value, $key) => "$key=$value")
34-
->implode(',');
35-
}
63+
$options = $this->optionsToString($options);
3664

3765
if (empty($pathPrefix)) {
3866
return URL::signedRoute(
@@ -52,7 +80,7 @@ public function signedUrl(string $path, array|string $options = [], ?string $pat
5280
}
5381

5482
/**
55-
* Create a temporary signed URL for the image transformation.
83+
* Generate a temporary signed URL for the image transformation.
5684
*
5785
* @param string $path The path to the image.
5886
* @param array|string $options The transformation options.
@@ -67,4 +95,18 @@ public function temporarySignedUrl(string $path, array|string $options, DateTime
6795
{
6896
return $this->signedUrl($path, $options, $pathPrefix, $expiration, $absolute);
6997
}
98+
99+
/**
100+
* Convert array options to a string format suitable for URL generation.
101+
*/
102+
protected function optionsToString(array|string $options): string
103+
{
104+
if (is_array($options)) {
105+
return collect($options)
106+
->map(fn ($value, $key) => "$key=$value")
107+
->implode(',');
108+
}
109+
110+
return $options;
111+
}
70112
}

tests/Feature/FacadeTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AceOfAces\LaravelImageTransformUrl\Exceptions\InvalidConfigurationException;
66
use AceOfAces\LaravelImageTransformUrl\Facades\ImageTransformUrl;
77
use AceOfAces\LaravelImageTransformUrl\Tests\TestCase;
8+
use Illuminate\Support\Facades\URL;
89

910
it('produces identical signed URLs for the signedUrl and temporarySignedUrl methods when given the same parameters', function () {
1011
/** @var TestCase $this */
@@ -33,3 +34,21 @@
3334

3435
ImageTransformUrl::signedUrl('path/to/image.jpg', ['width' => 100, 'height' => 200]);
3536
})->throws(InvalidConfigurationException::class, 'Signed URLs are not enabled. Please check your configuration.');
37+
38+
it('can use the make method to generate a regular unsigned URL', function () {
39+
/** @var TestCase $this */
40+
$url = ImageTransformUrl::make('path/to/image.jpg', ['width' => 100, 'height' => 200], 'images');
41+
42+
expect($url)->toBe(URL::route('image.transform', [
43+
'pathPrefix' => 'images',
44+
'options' => 'width=100,height=200',
45+
'path' => 'path/to/image.jpg',
46+
]));
47+
});
48+
49+
it('can use the url method as an alias for make', function () {
50+
/** @var TestCase $this */
51+
$url = ImageTransformUrl::url('path/to/image.jpg', ['width' => 100, 'height' => 200], 'images');
52+
53+
expect($url)->toBe(ImageTransformUrl::make('path/to/image.jpg', ['width' => 100, 'height' => 200], 'images'));
54+
});

0 commit comments

Comments
 (0)