Skip to content

Commit

Permalink
improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 14, 2023
1 parent 45c4de8 commit c8454d1
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 119 deletions.
2 changes: 1 addition & 1 deletion src/Drivers/Concerns/PerformsOptimizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait PerformsOptimizations

protected OptimizerChain $optimizerChain;

public function optimize(?OptimizerChain $optimizerChain = null): self
public function optimize(?OptimizerChain $optimizerChain = null): static
{
$this->optimize = true;
$this->optimizerChain = $optimizerChain ?? OptimizerChainFactory::create();
Expand Down
60 changes: 30 additions & 30 deletions src/Drivers/Gd/GdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GdDriver implements ImageDriver

protected string $originalPath;

public function new(int $width, int $height, ?string $backgroundColor = null): self
public function new(int $width, int $height, ?string $backgroundColor = null): static
{
$image = imagecreatetruecolor($width, $height);

Expand All @@ -49,14 +49,14 @@ public function new(int $width, int $height, ?string $backgroundColor = null): s
return (new self)->setImage($image);

Check failure on line 49 in src/Drivers/Gd/GdDriver.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\Image\Drivers\Gd\GdDriver::new() should return static(Spatie\Image\Drivers\Gd\GdDriver) but returns Spatie\Image\Drivers\Gd\GdDriver.
}

protected function setImage(GdImage $image): self
protected function setImage(GdImage $image): static
{
$this->image = $image;

return $this;
}

public function loadFile(string $path): self
public function loadFile(string $path): static
{
$this->optimize = false;
$this->quality = -1;
Expand Down Expand Up @@ -99,7 +99,7 @@ public function getHeight(): int
return imagesy($this->image);
}

public function brightness(int $brightness): self
public function brightness(int $brightness): static
{
// TODO: Convert value between -100 and 100 to -255 and 255
$brightness = round($brightness * 2.55);
Expand All @@ -109,7 +109,7 @@ public function brightness(int $brightness): self
return $this;
}

public function blur(int $blur): self
public function blur(int $blur): static
{
for ($i = 0; $i < $blur; $i++) {
imagefilter($this->image, IMG_FILTER_GAUSSIAN_BLUR);
Expand All @@ -118,7 +118,7 @@ public function blur(int $blur): self
return $this;
}

public function save(?string $path = null): self
public function save(?string $path = null): static
{
if (! $path) {
$path = $this->originalPath;
Expand Down Expand Up @@ -177,7 +177,7 @@ public function getSize(): Size
return new Size($this->getWidth(), $this->getHeight());
}

public function fit(Fit $fit, ?int $desiredWidth = null, ?int $desiredHeight = null): self
public function fit(Fit $fit, ?int $desiredWidth = null, ?int $desiredHeight = null): static
{
$calculatedSize = $fit->calculateSize(
$this->getWidth(),
Expand Down Expand Up @@ -209,7 +209,7 @@ protected function modify(
int $sourceY = 0,
int $sourceWidth = 0,
int $sourceHeight = 0,
): self {
): static {
$newImage = imagecreatetruecolor($desiredWidth, $desiredHeight);

$transparentColorValue = imagecolortransparent($this->image);
Expand Down Expand Up @@ -269,7 +269,7 @@ public function resizeCanvas(
?AlignPosition $position = null,
bool $relative = false,
string $backgroundColor = '#ffffff'
): self {
): static {
$position ??= AlignPosition::Center;

$originalWidth = $this->getWidth();
Expand Down Expand Up @@ -331,21 +331,21 @@ public function resizeCanvas(
return $this;
}

public function gamma(float $gamma): self
public function gamma(float $gamma): static
{
imagegammacorrect($this->image, 1, $gamma);

return $this;
}

public function contrast(float $level): self
public function contrast(float $level): static
{
imagefilter($this->image, IMG_FILTER_CONTRAST, ($level * -1));

return $this;
}

public function colorize(int $red, int $green, int $blue): self
public function colorize(int $red, int $green, int $blue): static
{
$red = round($red * 2.55);
$green = round($green * 2.55);
Expand All @@ -356,14 +356,14 @@ public function colorize(int $red, int $green, int $blue): self
return $this;
}

public function greyscale(): self
public function greyscale(): static
{
imagefilter($this->image, IMG_FILTER_GRAYSCALE);

return $this;
}

public function manualCrop(int $width, int $height, ?int $x = null, ?int $y = null): self
public function manualCrop(int $width, int $height, ?int $x = null, ?int $y = null): static
{
$cropped = new Size($width, $height);
$position = new Point($x ?? 0, $y ?? 0);
Expand Down Expand Up @@ -393,7 +393,7 @@ public function manualCrop(int $width, int $height, ?int $x = null, ?int $y = nu
return $this;
}

public function crop(int $width, int $height, CropPosition $position = CropPosition::Center): self
public function crop(int $width, int $height, CropPosition $position = CropPosition::Center): static
{
$width = min($width, $this->getWidth());
$height = min($height, $this->getHeight());
Expand All @@ -408,7 +408,7 @@ public function crop(int $width, int $height, CropPosition $position = CropPosit
return $this->manualCrop($width, $height, $offsetX, $offsetY);
}

public function focalCrop(int $width, int $height, ?int $cropCenterX = null, ?int $cropCenterY = null): self
public function focalCrop(int $width, int $height, ?int $cropCenterX = null, ?int $cropCenterY = null): static
{
[$width, $height, $cropCenterX, $cropCenterY] = $this->calculateFocalCropCoordinates(
$width,
Expand All @@ -422,7 +422,7 @@ public function focalCrop(int $width, int $height, ?int $cropCenterX = null, ?in
return $this;
}

public function sepia(): ImageDriver
public function sepia(): static
{
return $this
->greyscale()
Expand All @@ -432,7 +432,7 @@ public function sepia(): ImageDriver
->contrast(5);
}

public function sharpen(float $amount): self
public function sharpen(float $amount): static
{
$min = $amount >= 10 ? $amount * -0.01 : 0;
$max = $amount * -0.025;
Expand All @@ -449,7 +449,7 @@ public function sharpen(float $amount): self
return $this;
}

public function background(string $color): self
public function background(string $color): static
{
$width = $this->getWidth();
$height = $this->getHeight();
Expand All @@ -465,15 +465,15 @@ public function background(string $color): self
return $this;
}

public function overlay(ImageDriver $bottomImage, ImageDriver $topImage, int $x = 0, int $y = 0): self
public function overlay(ImageDriver $bottomImage, ImageDriver $topImage, int $x = 0, int $y = 0): static
{
$bottomImage->insert($topImage, AlignPosition::TopLeft, $x, $y);
$this->image = $bottomImage->image();

return $this;
}

public function orientation(?Orientation $orientation = null): self
public function orientation(?Orientation $orientation = null): static
{
if (is_null($orientation)) {
$orientation = $this->getOrientationFromExif($this->exif);
Expand Down Expand Up @@ -504,7 +504,7 @@ public function exif(): array
return $this->exif;
}

public function flip(FlipDirection $flip): self
public function flip(FlipDirection $flip): static
{
$direction = match ($flip) {
FlipDirection::Horizontal => IMG_FLIP_HORIZONTAL,
Expand All @@ -517,7 +517,7 @@ public function flip(FlipDirection $flip): self
return $this;
}

public function pixelate(int $pixelate = 50): self
public function pixelate(int $pixelate = 50): static
{
imagefilter($this->image, IMG_FILTER_PIXELATE, $pixelate, true);

Expand All @@ -529,7 +529,7 @@ public function insert(
AlignPosition $position = AlignPosition::Center,
int $x = 0,
int $y = 0,
): self {
): static {
if (is_string($otherImage)) {
$otherImage = (new self())->loadFile($otherImage);
}
Expand All @@ -554,7 +554,7 @@ public function insert(
return $this;
}

public function resize(int $width, int $height, array $constraints = []): self
public function resize(int $width, int $height, array $constraints = []): static
{
$resized = $this->getSize()->resize($width, $height, $constraints);

Expand All @@ -563,21 +563,21 @@ public function resize(int $width, int $height, array $constraints = []): self
return $this;
}

public function width(int $width, array $constraints = [Constraint::PreserveAspectRatio]): self
public function width(int $width, array $constraints = [Constraint::PreserveAspectRatio]): static
{
$this->resize($width, $this->getHeight(), $constraints);

return $this;
}

public function height(int $height, array $constraints = [Constraint::PreserveAspectRatio]): self
public function height(int $height, array $constraints = [Constraint::PreserveAspectRatio]): static
{
$this->resize($this->getWidth(), $height, $constraints);

return $this;
}

public function border(int $width, BorderType $type, string $color = '000000'): self
public function border(int $width, BorderType $type, string $color = '000000'): static
{
if ($type === BorderType::Shrink) {
$originalWidth = $this->getWidth();
Expand Down Expand Up @@ -644,7 +644,7 @@ public function border(int $width, BorderType $type, string $color = '000000'):
}

/** @param int<-1, 100> $quality */
public function quality(int $quality): self
public function quality(int $quality): static
{
$this->quality = $quality;

Expand All @@ -661,7 +661,7 @@ protected function pngCompression(): int
return (int) round((100 - $this->quality) / 10);
}

public function format(string $format): ImageDriver
public function format(string $format): static
{
ob_start();

Expand Down
56 changes: 28 additions & 28 deletions src/Drivers/ImageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

interface ImageDriver
{
public function new(int $width, int $height, ?string $backgroundColor = null): self;
public function new(int $width, int $height, ?string $backgroundColor = null): static;

public function driverName(): string;

public function save(string $path = ''): self;
public function save(string $path = ''): static;

public function getWidth(): int;

Expand All @@ -28,28 +28,28 @@ public function getHeight(): int;
/**
* @param int $brightness A value between -100 and 100
*/
public function brightness(int $brightness): self;
public function brightness(int $brightness): static;

public function gamma(float $gamma): self;
public function gamma(float $gamma): static;

public function contrast(float $level): self;
public function contrast(float $level): static;

/**
* @param int $blur A value between 0 and 100.
*/
public function blur(int $blur): self;
public function blur(int $blur): static;

public function colorize(int $red, int $green, int $blue): self;
public function colorize(int $red, int $green, int $blue): static;

public function greyscale(): self;
public function greyscale(): static;

public function sepia(): self;
public function sepia(): static;

public function sharpen(float $amount): self;
public function sharpen(float $amount): static;

public function getSize(): Size;

public function fit(Fit $fit, ?int $desiredWidth = null, ?int $desiredHeight = null): self;
public function fit(Fit $fit, ?int $desiredWidth = null, ?int $desiredHeight = null): static;

public function pickColor(int $x, int $y, ColorFormat $colorFormat): mixed;

Expand All @@ -59,51 +59,51 @@ public function resizeCanvas(
?AlignPosition $position = null,
bool $relative = false,
string $backgroundColor = '#000000'
): self;
): static;

public function manualCrop(int $width, int $height, int $x = 0, int $y = 0): self;
public function manualCrop(int $width, int $height, int $x = 0, int $y = 0): static;

public function crop(int $width, int $height, CropPosition $position = CropPosition::Center): self;
public function crop(int $width, int $height, CropPosition $position = CropPosition::Center): static;

public function focalCrop(int $width, int $height, ?int $cropCenterX = null, ?int $cropCenterY = null): self;
public function focalCrop(int $width, int $height, ?int $cropCenterX = null, ?int $cropCenterY = null): static;

public function base64(string $imageFormat): string;

public function background(string $color): self;
public function background(string $color): static;

public function overlay(ImageDriver $bottomImage, ImageDriver $topImage, int $x, int $y): self;
public function overlay(ImageDriver $bottomImage, ImageDriver $topImage, int $x, int $y): static;

public function orientation(?Orientation $orientation = null): self;
public function orientation(?Orientation $orientation = null): static;

public function exif(): array;

public function flip(FlipDirection $flip): self;
public function flip(FlipDirection $flip): static;

public function pixelate(int $pixelate): self;
public function pixelate(int $pixelate): static;

public function insert(
ImageDriver|string $otherImage,
AlignPosition $position = AlignPosition::Center,
int $x = 0,
int $y = 0,
): self;
): static;

public function image(): mixed;

/** @param array<Constraint> $constraints */
public function resize(int $width, int $height, array $constraints): self;
public function resize(int $width, int $height, array $constraints): static;

/** @param array<Constraint> $constraints */
public function width(int $width, array $constraints = []): self;
public function width(int $width, array $constraints = []): static;

/** @param array<Constraint> $constraints */
public function height(int $height, array $constraints = []): self;
public function height(int $height, array $constraints = []): static;

public function border(int $width, BorderType $type, string $color = '000000'): self;
public function border(int $width, BorderType $type, string $color = '000000'): static;

public function quality(int $quality): self;
public function quality(int $quality): static;

public function format(string $format): self;
public function format(string $format): static;

public function optimize(?OptimizerChain $optimizerChain = null): self;
public function optimize(?OptimizerChain $optimizerChain = null): static;
}
Loading

0 comments on commit c8454d1

Please sign in to comment.