diff --git a/README.md b/README.md index d36f9c0..4a60618 100755 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ------ -A PHP package to generate Cloudflare Image Resizing URLs. based on [Cloudflare Image Resizing](https://developers.cloudflare.com/images/url-format). +A PHP package to generate Cloudflare Image Resizing URLs. based on [Cloudflare Image Resizing](https://developers.cloudflare.com/images/transform-images/transform-via-url). ### ⚡️ Installation @@ -30,7 +30,7 @@ So just by adding `/cdn-cgi/image/` to the beginning of the URL, you can utilize You can convert and resize images by requesting them via a specially-formatted URL. This way you do not need to write any code, only change HTML markup of your website to use the new URLs. -For more information, please see the [Cloudflare Image Resizing documentation](https://developers.cloudflare.com/images/url-format). +For more information, please see the [Cloudflare Image Resizing documentation](https://developers.cloudflare.com/images/transform-images/transform-via-url). This package provides a fluent API to generate Cloudflare Image Resizing URLs. @@ -74,7 +74,33 @@ $image = CFImageResizing::make($url) #### Available transformations -Please see the available transformations [here](src/Concerns/HasOptions.php). +| Transformation | Description | Cloudflare Docs | +|---------------|-------------|---------------| +| `anim(bool)` | Whether to animate the image | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#anim) | +| `background(string)` | Background color in CSS format (hex, rgb, rgba, hsl, hsla) | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#background) | +| `blur(int)` | Blur radius between 1 (slight blur) and 250 (maximum) | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#blur) | +| `brightness(string)` | Value of 1.0 equals no change, 0.5 equals half brightness, 2.0 equals twice as bright | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#brightness) | +| `compression(string)` | Compression value | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#compression) | +| `contrast(float)` | Value of 1.0 equals no change, 0.5 equals low contrast, 2.0 equals high contrast | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#contrast) | +| `dpr(int)` | Device pixel ratio multiplier for width/height | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#dpr) | +| `fit(string)` | Fit mode (scale-down, contain, cover, crop, pad) | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#fit) | +| `format(string)` | Output format (avif, webp, jpeg, baseline-jpeg, json) | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#format) | +| `avif()` | Set the format to avif. alias for `format('avif')`. | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#format) | +| `webp()` | Set the format to webp. alias for `format('webp')`. | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#format) | +| `jpeg()` | Set the format to jpeg. alias for `format('jpeg')`. | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#format) | +| `baselineJpeg()` | Set the format to baseline-jpeg. alias for `format('baseline-jpeg')`. | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#format) | +| `json()` | Set the format to json. alias for `format('json')`. | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#format) | +| `gamma(float)` | Value of 1.0 equals no change, 0.5 darkens, 2.0 lightens | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#gamma) | +| `gravity(string)` | Cropping gravity (auto, left, right, top, bottom) | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#gravity) | +| `height(int)` | Height in pixels | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#height) | +| `metadata(string)` | Metadata preservation mode (keep, copyright, none) | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#metadata) | +| `onerror(string)` | Error handling mode (redirect, none) | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#onerror) | +| `quality(int)` | Quality between 1 (lowest) and 100 (highest) | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#quality) | +| `rotate(int)` | Rotation degrees (90, 180, or 270) | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#rotate) | +| `saturation(float)` | Value of 1.0 equals no change, 0.5 equals half saturation, 2.0 equals twice as saturated | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#saturation) | +| `sharpen(float)` | Sharpening strength between 0 (none) and 10 (maximum) | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#sharpen) | +| `trim(string)` | Trim values in format "top;right;bottom;left" | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#trim) | +| `width(int)` | Width in pixels | [Docs](https://developers.cloudflare.com/images/transform-images/transform-via-url/#width) | ------ diff --git a/src/CFImageResizing.php b/src/CFImageResizing.php index 813c37f..78fca79 100755 --- a/src/CFImageResizing.php +++ b/src/CFImageResizing.php @@ -17,7 +17,7 @@ final class CFImageResizing * @param string|null $url The image URL. * @return void */ - public function __construct(string $url = null) + public function __construct(?string $url = null) { $this->url($url); } @@ -27,7 +27,7 @@ public function __construct(string $url = null) * * @param string|null $url The image URL. */ - public static function make(string $url = null): static + public static function make(?string $url = null): static { return new self($url); } diff --git a/src/Concerns/HasOptions.php b/src/Concerns/HasOptions.php index 1165b22..5b94e59 100755 --- a/src/Concerns/HasOptions.php +++ b/src/Concerns/HasOptions.php @@ -7,19 +7,19 @@ trait HasOptions { /** - * The image URL. + * The URL. */ private ?string $url = null; /** - * The image options. + * The options. */ private array $options = []; /** - * Set the image URL. + * Set the URL. * - * @param string $url The image URL. + * @param string $url The image URL */ public function url(string $url): static { @@ -29,11 +29,11 @@ public function url(string $url): static } /** - * Set the image anim. + * Set the anim. * - * @param bool $anim Whether to animate the image. + * @param bool $anim Whether to animate the image * - * @see https://developers.cloudflare.com/images/image-resizing/url-format/#anim + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#anim */ public function anim(bool $anim): static { @@ -43,11 +43,11 @@ public function anim(bool $anim): static } /** - * Set the image background. + * Set the background. * - * @param string $background The background color in css format. hex, rgb, rgba, hsl, hsla. + * @param string $background The background color in css format. hex, rgb, rgba, hsl, hsla * - * @see https://developers.cloudflare.com/images/image-resizing/url-format/#background + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#background */ public function background(string $background): static { @@ -57,11 +57,11 @@ public function background(string $background): static } /** - * Set the image blur. + * Set the blur. * - * @param int $blur The blur radius between 1 (slight blur) and 250 (maximum). + * @param int $blur The blur radius between 1 (slight blur) and 250 (maximum) * - * @see https://developers.cloudflare.com/images/image-resizing/url-format/#blur + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#blur */ public function blur(int $blur): static { @@ -71,9 +71,9 @@ public function blur(int $blur): static } /** - * Set the image brightness. + * Set the brightness. * - * @param string $brightness The brightness value of 1.0 equals no change, a value of 0.5 equals half brightness, and a value of 2.0 equals twice as bright. 0 is ignored. + * @param string $brightness The brightness value of 1.0 equals no change, a value of 0.5 equals half brightness, and a value of 2.0 equals twice as bright. 0 is ignored */ public function brightness(string $brightness): static { @@ -83,7 +83,67 @@ public function brightness(string $brightness): static } /** - * Set the image format. + * Set the compression. + * + * @param string $compression The compression value + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#compression + */ + public function compression(string $compression): static + { + $this->options['compression'] = $compression; + + return $this; + } + + /** + * Set the contrast. + * + * @param float $contrast A value of 1.0 equals no change, 0.5 equals low contrast, and 2.0 equals high contrast + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#contrast + */ + public function contrast(float $contrast): static + { + $this->options['contrast'] = $contrast; + + return $this; + } + + /** + * Set the device pixel ratio. + * + * @param int $dpr Multiplier for width/height that makes it easier to specify higher-DPI sizes + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#dpr + */ + public function dpr(int $dpr): static + { + $this->options['dpr'] = $dpr; + + return $this; + } + + /** + * Set the fit mode. + * + * @param string $fit The fit mode (scale-down, contain, cover, crop, pad) + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#fit + */ + public function fit(string $fit): static + { + $this->options['fit'] = $fit; + + return $this; + } + + /** + * Set the format. + * + * @param string $format The format (avif, webp, jpeg, baseline-jpeg, json) + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#format */ public function format(string $format): static { @@ -93,7 +153,7 @@ public function format(string $format): static } /** - * Set the image format to avif. + * Set the format to avif. alias for `format('avif')`. */ public function avif(): static { @@ -101,7 +161,7 @@ public function avif(): static } /** - * Set the image format to webp. + * Set the format to webp. alias for `format('webp')`. */ public function webp(): static { @@ -109,7 +169,7 @@ public function webp(): static } /** - * Set the image format to jpeg. + * Set the format to jpeg. alias for `format('jpeg')`. */ public function jpeg(): static { @@ -117,7 +177,7 @@ public function jpeg(): static } /** - * Set the image format to baseline-jpeg. + * Set the format to baseline-jpeg. alias for `format('baseline-jpeg')`. */ public function baselineJpeg(): static { @@ -125,7 +185,7 @@ public function baselineJpeg(): static } /** - * Set the image format to json. + * Set the format to json. alias for `format('json')`. */ public function json(): static { @@ -133,27 +193,39 @@ public function json(): static } /** - * Set the image quality. + * Set the gamma. + * + * @param float $gamma A value of 1.0 equals no change, 0.5 darkens the image, and 2.0 lightens the image. + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#gamma */ - public function quality(int $quality): static + public function gamma(float $gamma): static { - $this->options['quality'] = $quality; + $this->options['gamma'] = $gamma; return $this; } /** - * Set the image width. + * Set the gravity for cropping. + * + * @param string $gravity Either 'auto', a side ('left', 'right', 'top', 'bottom'). + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#gravity */ - public function width(int $width): static + public function gravity(string $gravity): static { - $this->options['width'] = $width; + $this->options['gravity'] = $gravity; return $this; } /** - * Set the image height. + * Set the height. + * + * @param int $height The height value in pixels + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#height */ public function height(int $height): static { @@ -161,4 +233,116 @@ public function height(int $height): static return $this; } + + /** + * Set the metadata preservation mode. + * + * @param string $metadata The metadata mode (keep, copyright, none) + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#metadata + */ + public function metadata(string $metadata): static + { + $this->options['metadata'] = $metadata; + + return $this; + } + + /** + * Set the onerror mode. + * + * @param string $onerror The onerror mode (redirect, none) + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#onerror + */ + public function onerror(string $onerror): static + { + $this->options['onerror'] = $onerror; + + return $this; + } + + /** + * Set the quality. + * + * @param int $quality The quality value between 1 (lowest quality) and 100 (highest quality) + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#quality + */ + public function quality(int $quality): static + { + $this->options['quality'] = $quality; + + return $this; + } + + /** + * Set the rotate angle. + * + * @param int $rotate Number of degrees (90, 180, or 270) to rotate the image + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#rotate + */ + public function rotate(int $rotate): static + { + $this->options['rotate'] = $rotate; + + return $this; + } + + /** + * Set the saturation level. + * + * @param float $saturation A value of 1.0 equals no change, 0.5 equals half saturation, and 2.0 equals twice as saturated + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#saturation + */ + public function saturation(float $saturation): static + { + $this->options['saturation'] = $saturation; + + return $this; + } + + /** + * Set the sharpening strength. + * + * @param float $sharpen Value between 0 (no sharpening) and 10 (maximum) + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#sharpen + */ + public function sharpen(float $sharpen): static + { + $this->options['sharpen'] = $sharpen; + + return $this; + } + + /** + * Set the trim values. + * + * @param string $trim The trim value in the form of `top;right;bottom;left` + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#trim + */ + public function trim(string $trim): static + { + $this->options['trim'] = $trim; + + return $this; + } + + /** + * Set the width. + * + * @param int $width The width value in pixels + * + * @see https://developers.cloudflare.com/images/transform-images/transform-via-url/#width + */ + public function width(int $width): static + { + $this->options['width'] = $width; + + return $this; + } }