|
| 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