Skip to content

Commit a1377fa

Browse files
committed
docs: document signed URLs, update error handling
1 parent baf21fe commit a1377fa

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

docs/.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export default defineConfig({
5151
{
5252
text: 'Advanced',
5353
items: [
54+
{ text: 'Signed URLs', link: '/signed-urls' },
5455
{ text: 'Image Caching', link: '/image-caching' },
5556
{ text: 'Rate Limiting', link: '/rate-limiting' },
5657
{ text: 'CDN Usage', link: '/cdn-usage' },

docs/pages/error-handling.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
The route handler of this package is designed to be robust against invalid options, paths and file names, while also not exposing additional information of your applications public directory structure.
44

5+
## HTTP Status Codes
6+
57
This is why the route handler will return a plain `404` response if:
68

79
- a requested image does not exist at the specified path
810
- the requested image is not a valid image file
911
- the provided options are not in the correct format (`key=value`, no trailing comma, etc.)
1012

11-
The only other HTTP error that can be returned is a `429` response, which indicates that the request was rate-limited.
13+
The only two other HTTP errors that can be returned are:
14+
- a `429` response, which indicates that the request was rate-limited
15+
- a `403` response, which indicates that the request was unauthorized (e.g. when using signed URLs and the signature is invalid or expired)
16+
17+
## Invalid options
1218

1319
If parts of the given route options are invalid, the route handler will ignore them and only apply the valid options.
1420

docs/pages/signed-urls.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Signed URLs
2+
3+
This package provides the option to generate signed URLs for images from specific source directories powered by [Laravel's URL signing feature](https://laravel.com/docs/urls#signed-urls).
4+
5+
This can be useful for securing access to images that should not be publicly accessible without proper authorization or only in a scaled down version.
6+
7+
::: info
8+
Signed URLs also ensure that the provided options cannot be modified client-side.
9+
:::
10+
11+
::: warning
12+
The Signed URL feature does not restrict access to public images.
13+
If you want to secure access to images, ensure that the source directories you want signed URLs for are not publicly accessible.
14+
:::
15+
16+
## Setup
17+
18+
To enable signed URLs, set the `signed_urls.enabled` option to `true` in your `image-transform-url.php` configuration.
19+
20+
You then need to specify the source directories for which signed URLs should apply to in the `signed_urls.source_directories` array.
21+
22+
For example:
23+
24+
```php
25+
'source_directories' => [
26+
'images' => public_path('images'),
27+
'protected' => storage_path('app/private/protected-images'),
28+
],
29+
30+
// ...
31+
32+
'signed_urls' => [
33+
'enabled' => env('IMAGE_TRANSFORM_SIGNED_URLS_ENABLED', false),
34+
'for_source_directories' => env('IMAGE_TRANSFORM_SIGNED_URLS_FOR_SOURCE_DIRECTORIES', [
35+
'protected-images',
36+
]),
37+
],
38+
```
39+
40+
## Generating Signed URLs
41+
42+
To generate a signed URL for an image, you can use the `ImageTransformUrl` facade:
43+
44+
```php
45+
use AceOfAces\LaravelImageTransformUrl\Facades\ImageTransformUrl;
46+
47+
$options = [
48+
'blur' => 50,
49+
'width' 500,
50+
];
51+
52+
$blurredImage = ImageTransformUrl::signedUrl(
53+
'example.jpg',
54+
$options,
55+
'protected'
56+
);
57+
```
58+
59+
## Temporary Signed URLs
60+
61+
If you would like to to generate a signed URL that expires after a certain time, you can use the `temporarySignedUrl` method:
62+
63+
```php
64+
use AceOfAces\LaravelImageTransformUrl\Facades\ImageTransformUrl;
65+
66+
$options = [
67+
'blur' => 50,
68+
'width' 500,
69+
];
70+
71+
$temporarySignedUrl = ImageTransformUrl::temporarySignedUrl(
72+
'example.jpg',
73+
$options,
74+
now()->addMinutes(60),
75+
'protected'
76+
);
77+
```
78+
79+
::: info
80+
You can also use the generic `signedUrl` method to generate temporary signed URLs.
81+
This method accepts an `$expiration` parameter, which defaults to `null`. If you provide a value, it will generate a temporary signed URL.
82+
:::

0 commit comments

Comments
 (0)