Skip to content

Commit

Permalink
Merge branch 'v3' of github.com:spatie/image into v3
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 8, 2023
2 parents 4837857 + 4f0aadc commit 55b6d19
Show file tree
Hide file tree
Showing 18 changed files with 285 additions and 54 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: PHPStan

on:
push:
paths:
- '**.php'
- 'phpstan.neon.dist'
- '.github/workflows/phpstan.yml'

jobs:
phpstan:
name: phpstan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
coverage: none
tools: phpstan

- name: Install composer dependencies
uses: ramsey/composer-install@v2

- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"require-dev": {
"pestphp/pest": "^2.0",
"phpstan/phpstan": "^1.10",
"spatie/pest-plugin-snapshots": "^2.1",
"spatie/pixelmatch-php": "^1.0",
"spatie/ray": "^1.37",
Expand All @@ -44,7 +45,9 @@
}
},
"scripts": {
"test": "vendor/bin/pest"
"test": "vendor/bin/pest",
"analyse": "phpstan analyse --memory-limit=1G --no-progress",
"baseline": "./vendor/bin/phpstan analyse --memory-limit=2G --generate-baseline"
},
"config": {
"sort-packages": true,
Expand Down
171 changes: 171 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$value of method Spatie\\\\Image\\\\Drivers\\\\Color\\:\\:initFromObject\\(\\) expects ImagickPixel, object given\\.$#"
count: 1
path: src/Drivers/Color.php

-
message: "#^Method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:exif\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:pngCompression\\(\\) should return int\\<\\-1, 9\\> but returns int\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$data of function imagecreatefromstring expects string, string\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$dst_image of function imagecopyresampled expects GdImage, GdImage\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$image of function imagealphablending expects GdImage, GdImage\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$image of function imagecolorallocatealpha expects GdImage, GdImage\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$image of function imagecolorsforindex expects GdImage, GdImage\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$image of function imagecolortransparent expects GdImage, GdImage\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$image of function imagefill expects GdImage, GdImage\\|false given\\.$#"
count: 2
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$image of function imagesavealpha expects GdImage, GdImage\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$image of method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:setImage\\(\\) expects GdImage, GdImage\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$stream of function fclose expects resource, resource\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$stream of function fread expects resource, resource\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$string of function base64_encode expects string, string\\|false given\\.$#"
count: 2
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#1 \\$width of method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:manualCrop\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#2 \\$color of function imagecolorsforindex expects int, int\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#2 \\$color of function imagecolortransparent expects int\\|null, int\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#2 \\$height of method Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:manualCrop\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#2 \\$length of function fread expects int\\<0, max\\>, int\\<0, max\\>\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#4 \\$color of function imagefill expects int, int\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Parameter \\#6 \\$color of function imagefilledrectangle expects int, int\\|false given\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:\\$exif type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:\\$image \\(GdImage\\) does not accept GdImage\\|false\\.$#"
count: 2
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:\\$image \\(GdImage\\) does not accept mixed\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
path: src/Drivers/Gd/GdDriver.php

-
message: "#^Method Spatie\\\\Image\\\\Drivers\\\\ImageDriver\\:\\:exif\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Drivers/ImageDriver.php

-
message: "#^Method Spatie\\\\Image\\\\Drivers\\\\Imagick\\\\ImagickDriver\\:\\:exif\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Drivers/Imagick/ImagickDriver.php

-
message: "#^Parameter \\#1 \\$width of method Spatie\\\\Image\\\\Drivers\\\\Imagick\\\\ImagickDriver\\:\\:manualCrop\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/Drivers/Imagick/ImagickDriver.php

-
message: "#^Parameter \\#2 \\$height of method Spatie\\\\Image\\\\Drivers\\\\Imagick\\\\ImagickDriver\\:\\:manualCrop\\(\\) expects int, int\\|null given\\.$#"
count: 1
path: src/Drivers/Imagick/ImagickDriver.php

-
message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Imagick\\\\ImagickDriver\\:\\:\\$exif type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Drivers/Imagick/ImagickDriver.php

-
message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Imagick\\\\ImagickDriver\\:\\:\\$image \\(Imagick\\) does not accept mixed\\.$#"
count: 1
path: src/Drivers/Imagick/ImagickDriver.php

-
message: "#^Match expression does not handle remaining value\\: Spatie\\\\Image\\\\Enums\\\\Fit\\:\\:Crop$#"
count: 1
path: src/Enums/Fit.php

-
message: "#^Part \\$color \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
count: 1
path: src/Exceptions/InvalidColor.php
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- phpstan-baseline.neon

parameters:
level: 9
paths:
- src/

ignoreErrors:
- '#Unsafe usage of new static#'
35 changes: 19 additions & 16 deletions src/Drivers/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@

abstract class Color
{
abstract public function initFromInteger(int $value);
abstract public function initFromInteger(int $value): self;

abstract public function initFromArray(array $value);
/** @param array<int> $value */
abstract public function initFromArray(array $value): self;

abstract public function initFromString(string $value);
abstract public function initFromString(string $value): self;

abstract public function initFromObject(ImagickPixel $value);
abstract public function initFromObject(ImagickPixel $value): self;

abstract public function initFromRgb(int $red, int $green, int $blue);
abstract public function initFromRgb(int $red, int $green, int $blue): self;

abstract public function initFromRgba(int $red, int $green, int $blue, float $alpha);
abstract public function initFromRgba(int $red, int $green, int $blue, float $alpha): self;

abstract public function getInt(): int;

abstract public function getHex(string $prefix): string;

/** @return array<int, float|int> */
abstract public function getArray(): array;

abstract public function getRgba(): string;
Expand Down Expand Up @@ -60,6 +62,7 @@ public function format(ColorFormat $colorFormat): mixed
};
}

/** @return array<int> */
protected function rgbaFromString(string $colorValue): array
{
// parse color string in hexidecimal format like #cccccc or cccccc or ccc
Expand All @@ -73,22 +76,22 @@ protected function rgbaFromString(string $colorValue): array

if (preg_match($hexPattern, $colorValue, $matches)) {
$result = [];
$result[0] = strlen($matches[1]) == '1' ? hexdec($matches[1].$matches[1]) : hexdec($matches[1]);
$result[1] = strlen($matches[2]) == '1' ? hexdec($matches[2].$matches[2]) : hexdec($matches[2]);
$result[2] = strlen($matches[3]) == '1' ? hexdec($matches[3].$matches[3]) : hexdec($matches[3]);
$result[0] = strlen($matches[1]) == '1' ? (int) hexdec($matches[1].$matches[1]) : (int) hexdec($matches[1]);
$result[1] = strlen($matches[2]) == '1' ? (int) hexdec($matches[2].$matches[2]) : (int) hexdec($matches[2]);
$result[2] = strlen($matches[3]) == '1' ? (int) hexdec($matches[3].$matches[3]) : (int) hexdec($matches[3]);
$result[3] = 1;
} elseif (preg_match($rgbPattern, $colorValue, $matches)) {
$result = [];
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0;
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? (int) ($matches[1]) : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? (int) ($matches[2]) : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? (int) ($matches[3]) : 0;
$result[3] = 1;
} elseif (preg_match($rgbaPattern, $colorValue, $matches)) {
$result = [];
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0;
$result[3] = ($matches[4] >= 0 && $matches[4] <= 1) ? $matches[4] : 0;
$result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? (int) ($matches[1]) : 0;
$result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? (int) ($matches[2]) : 0;
$result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? (int) ($matches[3]) : 0;
$result[3] = ($matches[4] >= 0 && $matches[4] <= 1) ? (int) ($matches[4]) : 0;
} else {
throw InvalidColor::make($colorValue);
}
Expand Down
1 change: 1 addition & 0 deletions src/Drivers/Concerns/CalculatesCropOffsets.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/** @mixin \Spatie\Image\Drivers\ImageDriver */
trait CalculatesCropOffsets
{
/** @return array<int> */
protected function calculateCropOffsets(int $width, int $height, CropPosition $position): array
{
[$offsetPercentageX, $offsetPercentageY] = $position->offsetPercentages();
Expand Down
7 changes: 4 additions & 3 deletions src/Drivers/Concerns/CalculatesFocalCropCoordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
/** @mixin \Spatie\Image\Drivers\ImageDriver */
trait CalculatesFocalCropCoordinates
{
protected function calculateFocalCropCoordinates(int $width, int $height, $cropCenterX, $cropCenterY): array
/** @return array<int, int|null> */
protected function calculateFocalCropCoordinates(int $width, int $height, ?int $cropCenterX, ?int $cropCenterY): array
{
$width = min($width, $this->getWidth());
$height = min($height, $this->getHeight());

if ($cropCenterX > 0) {
$maxCropCenterX = $this->getWidth() - $width;
$cropCenterX = (int) $cropCenterX - ($width / 2);
$cropCenterX = (int) ($cropCenterX - ($width / 2));
$cropCenterX = min($maxCropCenterX, $cropCenterX);
$cropCenterX = max(0, $cropCenterX);
}

if ($cropCenterY > 0) {
$maxCropCenterY = $this->getHeight() - $height;
$cropCenterY = (int) $cropCenterY - ($height / 2);
$cropCenterY = (int) ($cropCenterY - ($height / 2));
$cropCenterY = min($maxCropCenterY, $cropCenterY);
$cropCenterY = max(0, $cropCenterY);
}
Expand Down
1 change: 1 addition & 0 deletions src/Drivers/Concerns/GetsOrientationFromExif.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

trait GetsOrientationFromExif
{
/** @param array{'Orientation'?: int} $exif */
public function getOrientationFromExif(array $exif): Orientation
{
if (! isset($exif['Orientation'])) {
Expand Down
4 changes: 3 additions & 1 deletion src/Drivers/Gd/GdColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function initFromArray(array $value): self
[$red, $green, $blue] = $value;
$this->alpha = 0;

} else {
throw InvalidColor::make($value);
}

$this->red = $red;
Expand Down Expand Up @@ -143,6 +145,6 @@ private function alpha2gd(float $input): int
$newMin = 127;
$newMax = 0;

return ceil(((($input - $oldMin) * ($newMax - $newMin)) / ($oldMax - $oldMin)) + $newMin);
return (int) ceil(((($input - $oldMin) * ($newMax - $newMin)) / ($oldMax - $oldMin)) + $newMin);
}
}
Loading

0 comments on commit 55b6d19

Please sign in to comment.