|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AceOfAces\LaravelImageTransformUrl; |
| 6 | + |
| 7 | +use AceOfAces\LaravelImageTransformUrl\Exceptions\InvalidConfigurationException; |
| 8 | +use DateInterval; |
| 9 | +use DateTimeInterface; |
| 10 | +use Illuminate\Support\Facades\URL; |
| 11 | + |
| 12 | +class ImageTransformUrl |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Generate a signed URL for the image transformation. |
| 16 | + * |
| 17 | + * @param string $path The path to the image. |
| 18 | + * @param array $options The transformation options. |
| 19 | + * @param string|null $pathPrefix The path prefix to use. Defaults to the default path prefix. |
| 20 | + * @param DateTimeInterface|\DateInterval|int|null $expiration The expiration time for the signed URL. |
| 21 | + * @return string The signed URL. |
| 22 | + * |
| 23 | + * @throws InvalidConfigurationException If signed URLs are not enabled in the configuration. |
| 24 | + */ |
| 25 | + public function signedUrl(string $path, array|string $options = [], ?string $pathPrefix = null, DateTimeInterface|DateInterval|int|null $expiration = null, ?bool $absolute = true): string |
| 26 | + { |
| 27 | + if (! config()->boolean('image-transform-url.signed_urls.enabled')) { |
| 28 | + throw new InvalidConfigurationException('Signed URLs are not enabled. Please check your configuration.'); |
| 29 | + } |
| 30 | + |
| 31 | + if (is_array($options)) { |
| 32 | + $options = collect($options) |
| 33 | + ->map(fn ($value, $key) => "$key=$value") |
| 34 | + ->implode(','); |
| 35 | + } |
| 36 | + |
| 37 | + if (empty($pathPrefix)) { |
| 38 | + return URL::signedRoute( |
| 39 | + 'image.transform.default', |
| 40 | + ['options' => $options, 'path' => $path], |
| 41 | + $expiration, |
| 42 | + $absolute |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + return URL::signedRoute( |
| 47 | + 'image.transform', |
| 48 | + ['pathPrefix' => $pathPrefix, 'options' => $options, 'path' => $path], |
| 49 | + $expiration, |
| 50 | + $absolute |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Create a temporary signed URL for the image transformation. |
| 56 | + * |
| 57 | + * @param string $path The path to the image. |
| 58 | + * @param array|string $options The transformation options. |
| 59 | + * @param DateTimeInterface|DateInterval|int $expiration The expiration time for the signed URL. |
| 60 | + * @param string|null $pathPrefix The path prefix to use. Defaults to the default path prefix. |
| 61 | + * @param bool|null $absolute Whether the URL should be absolute. Defaults to true. |
| 62 | + * @return string The temporary signed URL. |
| 63 | + * |
| 64 | + * @throws InvalidConfigurationException If signed URLs are not enabled in the configuration. |
| 65 | + */ |
| 66 | + public function temporarySignedUrl(string $path, array|string $options, DateTimeInterface|DateInterval|int $expiration, ?string $pathPrefix, ?bool $absolute = true): string |
| 67 | + { |
| 68 | + return $this->signedUrl($path, $options, $pathPrefix, $expiration, $absolute); |
| 69 | + } |
| 70 | +} |
0 commit comments