From 01b819c845dd0e4296a71700aa0a605e286f6c21 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 26 Nov 2023 13:37:32 +0100 Subject: [PATCH] Add encoding shortcut methods --- src/Image.php | 177 +++++++++++++++++++++ src/Interfaces/ImageInterface.php | 256 +++++++++++++++++++++++++++++- 2 files changed, 432 insertions(+), 1 deletion(-) diff --git a/src/Image.php b/src/Image.php index d10debef..875ee9eb 100644 --- a/src/Image.php +++ b/src/Image.php @@ -11,6 +11,12 @@ use Intervention\Image\Analyzers\PixelColorsAnalyzer; use Intervention\Image\Analyzers\ProfileAnalyzer; use Intervention\Image\Analyzers\ResolutionAnalyzer; use Intervention\Image\Analyzers\WidthAnalyzer; +use Intervention\Image\Encoders\AvifEncoder; +use Intervention\Image\Encoders\BmpEncoder; +use Intervention\Image\Encoders\GifEncoder; +use Intervention\Image\Encoders\JpegEncoder; +use Intervention\Image\Encoders\PngEncoder; +use Intervention\Image\Encoders\WebpEncoder; use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Interfaces\AnalyzerInterface; @@ -19,6 +25,7 @@ use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; use Intervention\Image\Interfaces\CoreInterface; use Intervention\Image\Interfaces\DriverInterface; +use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\EncoderInterface; use Intervention\Image\Interfaces\FontInterface; use Intervention\Image\Interfaces\ImageInterface; @@ -26,12 +33,30 @@ use Intervention\Image\Interfaces\ModifierInterface; use Intervention\Image\Interfaces\ProfileInterface; use Intervention\Image\Interfaces\ResolutionInterface; use Intervention\Image\Interfaces\SizeInterface; +use Intervention\Image\Modifiers\BlurModifier; +use Intervention\Image\Modifiers\BrightnessModifier; +use Intervention\Image\Modifiers\ColorizeModifier; use Intervention\Image\Modifiers\ColorspaceModifier; +use Intervention\Image\Modifiers\ContrastModifier; +use Intervention\Image\Modifiers\CropModifier; +use Intervention\Image\Modifiers\FitDownModifier; +use Intervention\Image\Modifiers\FitModifier; +use Intervention\Image\Modifiers\FlipModifier; +use Intervention\Image\Modifiers\FlopModifier; +use Intervention\Image\Modifiers\GammaModifier; use Intervention\Image\Modifiers\GreyscaleModifier; +use Intervention\Image\Modifiers\InvertModifier; +use Intervention\Image\Modifiers\PadModifier; use Intervention\Image\Modifiers\PixelateModifier; +use Intervention\Image\Modifiers\PlaceModifier; use Intervention\Image\Modifiers\ProfileModifier; +use Intervention\Image\Modifiers\RemoveAnimationModifier; +use Intervention\Image\Modifiers\ResizeDownModifier; +use Intervention\Image\Modifiers\ResizeModifier; use Intervention\Image\Modifiers\ResolutionModifier; use Intervention\Image\Modifiers\RotateModifier; +use Intervention\Image\Modifiers\ScaleDownModifier; +use Intervention\Image\Modifiers\ScaleModifier; use Intervention\Image\Modifiers\SharpenModifier; use Intervention\Image\Modifiers\TextModifier; use Intervention\Image\Typography\FontFactory; @@ -70,6 +95,11 @@ class Image implements ImageInterface, Countable return $this->count() > 1; } + public function removeAnimation(int|string $position = 0): ImageInterface + { + return $this->modify(new RemoveAnimationModifier($position)); + } + public function loops(): int { return $this->core->loops(); @@ -155,6 +185,11 @@ class Image implements ImageInterface, Countable return $this->modify(new SharpenModifier($amount)); } + public function invert(): ImageInterface + { + return $this->modify(new InvertModifier()); + } + public function pixelate(int $size): ImageInterface { return $this->modify(new PixelateModifier($size)); @@ -165,6 +200,41 @@ class Image implements ImageInterface, Countable return $this->modify(new GreyscaleModifier()); } + public function brightness(int $level): ImageInterface + { + return $this->modify(new BrightnessModifier($level)); + } + + public function contrast(int $level): ImageInterface + { + return $this->modify(new ContrastModifier($level)); + } + + public function gamma(float $gamma): ImageInterface + { + return $this->modify(new GammaModifier($gamma)); + } + + public function colorize(int $red = 0, int $green = 0, int $blue = 0): ImageInterface + { + return $this->modify(new ColorizeModifier($red, $green, $blue)); + } + + public function flip(): ImageInterface + { + return $this->modify(new FlipModifier()); + } + + public function flop(): ImageInterface + { + return $this->modify(new FlopModifier()); + } + + public function blur(int $amount = 5): ImageInterface + { + return $this->modify(new BlurModifier($amount)); + } + public function rotate(float $angle, mixed $background = 'ffffff'): ImageInterface { return $this->modify(new RotateModifier($angle, $background)); @@ -180,4 +250,111 @@ class Image implements ImageInterface, Countable ), ); } + + public function toJpeg(int $quality = 75): EncodedImageInterface + { + return $this->encode(new JpegEncoder($quality)); + } + + public function resize(?int $width, ?int $height): ImageInterface + { + return $this->modify(new ResizeModifier($width, $height)); + } + + public function resizeDown(?int $width, ?int $height): ImageInterface + { + return $this->modify(new ResizeDownModifier($width, $height)); + } + + public function scale(?int $width, ?int $height): ImageInterface + { + return $this->modify(new ScaleModifier($width, $height)); + } + + public function scaleDown(?int $width, ?int $height): ImageInterface + { + return $this->modify(new ScaleDownModifier($width, $height)); + } + + public function fit(int $width, int $height, string $position = 'center'): ImageInterface + { + return $this->modify(new FitModifier($width, $height, $position)); + } + + public function fitDown(int $width, int $height, string $position = 'center'): ImageInterface + { + return $this->modify(new FitDownModifier($width, $height, $position)); + } + + public function pad( + int $width, + int $height, + mixed $background = 'ffffff', + string $position = 'center' + ): ImageInterface { + return $this->modify(new PadModifier($width, $height, $background, $position)); + } + + public function padDown( + int $width, + int $height, + mixed $background = 'ffffff', + string $position = 'center' + ): ImageInterface { + return $this->modify(new PadModifier($width, $height, $background, $position)); + } + + public function crop( + int $width, + int $height, + int $offset_x = 0, + int $offset_y = 0, + string $position = 'top-left' + ): ImageInterface { + return $this->modify(new CropModifier($width, $height, $offset_x, $offset_y, $position)); + } + + public function place( + mixed $element, + string $position = 'top-left', + int $offset_x = 0, + int $offset_y = 0 + ): ImageInterface { + return $this->modify(new PlaceModifier($element, $position, $offset_x, $offset_y)); + } + + public function toJpg(int $quality = 75): EncodedImageInterface + { + return $this->toJpeg($quality); + } + + public function toPng(int $color_limit = 0): EncodedImageInterface + { + return $this->encode(new PngEncoder($color_limit)); + } + + public function toGif(int $color_limit = 0): EncodedImageInterface + { + return $this->encode(new GifEncoder($color_limit)); + } + + public function toWebp(int $quality = 75): EncodedImageInterface + { + return $this->encode(new WebpEncoder($quality)); + } + + public function toBitmap(int $color_limit = 0): EncodedImageInterface + { + return $this->encode(new BmpEncoder($color_limit)); + } + + public function toBmp(int $color_limit = 0): EncodedImageInterface + { + return $this->toBitmap($color_limit); + } + + public function toAvif(int $quality = 75): EncodedImageInterface + { + return $this->encode(new AvifEncoder($quality)); + } } diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index c192a93c..77d34de0 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -4,7 +4,6 @@ namespace Intervention\Image\Interfaces; use Countable; use Intervention\Image\EncodedImage; -use Intervention\Image\Modifiers\ColorspaceModifier; use IteratorAggregate; interface ImageInterface extends IteratorAggregate, Countable @@ -75,6 +74,19 @@ interface ImageInterface extends IteratorAggregate, Countable */ public function isAnimated(): bool; + /** + * Remove all frames but keep the one at the specified position + * + * It is possible to specify the position as integer or string values. + * With the former, the exact position passed is searched for, while + * string values must represent a percentage value between '0%' and '100%' + * and the respective frame position is only determined approximately. + * + * @param int|string $position + * @return ImageInterface + */ + public function removeAnimation(int|string $position = 0): ImageInterface; + /** * Return loop count of animated image * @@ -170,6 +182,69 @@ interface ImageInterface extends IteratorAggregate, Countable */ public function greyscale(): ImageInterface; + /** + * Adjust brightness of the current image + * + * @param int $level + * @return ImageInterface + */ + public function brightness(int $level): ImageInterface; + + /** + * Adjust color contrast of the current image + * + * @param int $level + * @return ImageInterface + */ + public function contrast(int $level): ImageInterface; + + /** + * Apply gamma correction on the current image + * + * @param float $gamma + * @return ImageInterface + */ + public function gamma(float $gamma): ImageInterface; + + /** + * Adjust the intensity of the RGB color channels + * + * @param int $red + * @param int $green + * @param int $blue + * @return ImageInterface + */ + public function colorize(int $red = 0, int $green = 0, int $blue = 0): ImageInterface; + + /** + * Mirror the current image horizontally + * + * @return ImageInterface + */ + public function flip(): ImageInterface; + + /** + * Mirror the current image vertically + * + * @return ImageInterface + */ + public function flop(): ImageInterface; + + /** + * Blur current image by given strength + * + * @param int $amount + * @return ImageInterface + */ + public function blur(int $amount = 5): ImageInterface; + + /** + * Invert the colors of the current image + * + * @return ImageInterface + */ + public function invert(): ImageInterface; + /** * Apply pixelation filter effect on current image * @@ -197,4 +272,183 @@ interface ImageInterface extends IteratorAggregate, Countable * @return ImageInterface */ public function text(string $text, int $x, int $y, callable|FontInterface $font): ImageInterface; + + /** + * Resize image to the given width and/or height + * + * @param null|int $width + * @param null|int $height + * @return ImageInterface + */ + public function resize(?int $width, ?int $height): ImageInterface; + + /** + * Resize image to the given width and/or height without exceeding the original dimensions + * + * @param null|int $width + * @param null|int $height + * @return ImageInterface + */ + public function resizeDown(?int $width, ?int $height): ImageInterface; + + /** + * Resize image to the given width and/or height and keep the original aspect ratio + * + * @param null|int $width + * @param null|int $height + * @return ImageInterface + */ + public function scale(?int $width, ?int $height): ImageInterface; + + /** + * Resize image to the given width and/or height, keep the original aspect ratio + * and do not exceed the original image width or height + * + * @param null|int $width + * @param null|int $height + * @return ImageInterface + */ + public function scaleDown(?int $width, ?int $height): ImageInterface; + + /** + * Takes the given dimensions and scales it to the largest possible size matching + * the original size. Then this size is positioned on the original and cut out + * before being resized to the desired size from the arguments + * + * @param int $width + * @param int $height + * @param string $position + * @return ImageInterface + */ + public function fit(int $width, int $height, string $position = 'center'): ImageInterface; + + /** + * Same as fit() but do not exceed the original image size + * + * @param int $width + * @param int $height + * @param string $position + * @return ImageInterface + */ + public function fitDown(int $width, int $height, string $position = 'center'): ImageInterface; + + /** + * Padded resizing means that the original image is scaled until it fits the + * defined target size with unchanged aspect ratio. Compared to the fit() + * method, this call does not create cropped areas, but new empty areas + * on the sides of the result image. These are filled with the specified + * background color. + * + * @param int $width + * @param int $height + * @param string $background + * @param string $position + * @return ImageInterface + */ + public function pad( + int $width, + int $height, + mixed $background = 'ffffff', + string $position = 'center', + ): ImageInterface; + + /** + * This method does the same thing as pad() but does not exceed the size of + * the original image. You can use this if you want to prevent up-sampling. + * + * @param int $width + * @param int $height + * @param string $background + * @param string $position + * @return ImageInterface + */ + public function padDown( + int $width, + int $height, + mixed $background = 'ffffff', + string $position = 'center', + ): ImageInterface; + + /** + * Cut out a rectangular part of the current image with given width and height at a given position. + * Define optional x,y offset coordinates to move the cutout by the given amount of pixels. + * + * @param int $width + * @param int $height + * @param int $offset_x + * @param int $offset_y + * @param string $position + * @return ImageInterface + */ + public function crop( + int $width, + int $height, + int $offset_x = 0, + int $offset_y = 0, + string $position = 'top-left', + ): ImageInterface; + + /** + * Place another image into the current image instance + * + * @param mixed $element + * @param string $position + * @param int $offset_x + * @param int $offset_y + * @return ImageInterface + */ + public function place( + mixed $element, + string $position = 'top-left', + int $offset_x = 0, + int $offset_y = 0 + ): ImageInterface; + + /** + * Encode image to JPEG format + * + * @param int $quality + * @return EncodedImageInterface + */ + public function toJpeg(int $quality = 75): EncodedImageInterface; + + /** + * Encode image to Webp format + * + * @param int $quality + * @return EncodedImageInterface + */ + public function toWebp(int $quality = 75): EncodedImageInterface; + + /** + * Encode image to PNG format + * + * @param int $color_limit + * @return EncodedImageInterface + */ + public function toPng(int $color_limit = 0): EncodedImageInterface; + + /** + * Encode image to GIF format + * + * @param int $color_limit + * @return EncodedImageInterface + */ + public function toGif(int $color_limit = 0): EncodedImageInterface; + + /** + * Encode image to Bitmap format + * + * @param int $color_limit + * @return EncodedImageInterface + */ + public function toBitmap(int $color_limit = 0): EncodedImageInterface; + + /** + * Encode image to AVIF format + * + * @param int $quality + * @return EncodedImageInterface + */ + public function toAvif(int $quality = 75): EncodedImageInterface; }