From 8a11b6b78a36cb8eef6308de5e71bf9c1ed6579c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sat, 7 May 2022 13:57:04 +0200 Subject: [PATCH 1/7] Init Rector --- composer.json | 3 ++- rector.php | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 00e125b..de0d55c 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "require-dev": { "friendsofphp/php-cs-fixer": "^3.4", "phpunit/phpunit": "^8.5.13", - "phpstan/phpstan": "^1.3" + "phpstan/phpstan": "^1.3", + "rector/rector": "^0.12.23" }, "suggest": { "ext-exif": "Allows to read and keep images EXIF data" diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..9a3a46e --- /dev/null +++ b/rector.php @@ -0,0 +1,40 @@ +paths([ + __DIR__ . '/src' + ]); + + // register a single rule + $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); + $rectorConfig->rule(NullCoalescingOperatorRector::class); + $rectorConfig->rule(RemoveExtraParametersRector::class); + + $rectorConfig->rule(AddMethodCallBasedStrictParamTypeRector::class); + $rectorConfig->rule(AddVoidReturnTypeWhereNoReturnRector::class); + + $rectorConfig->ruleWithConfiguration(TypedPropertyRector::class, [ + TypedPropertyRector::INLINE_PUBLIC => true, + ]); + + // define sets of rules + $rectorConfig->sets([ + LevelSetList::UP_TO_PHP_80 + ]); + + $rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon'); +}; From 7c607cbd834413161b492ef93ddc4b54dea05e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Mon, 9 May 2022 21:03:36 +0200 Subject: [PATCH 2/7] Add Rector configuration --- rector.php | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/rector.php b/rector.php index 9a3a46e..b9bb247 100644 --- a/rector.php +++ b/rector.php @@ -8,33 +8,32 @@ use Rector\Php74\Rector\Assign\NullCoalescingOperatorRector; use Rector\Php74\Rector\Property\TypedPropertyRector; use Rector\Set\ValueObject\LevelSetList; +use Rector\Set\ValueObject\SetList; use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector; -use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector; -use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector; use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector; -use Rector\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector; -return static function(RectorConfig $rectorConfig): void { - $rectorConfig->paths([ +return static function(RectorConfig $config): void { + $config->paths([ __DIR__ . '/src' ]); - // register a single rule - $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); - $rectorConfig->rule(NullCoalescingOperatorRector::class); - $rectorConfig->rule(RemoveExtraParametersRector::class); - - $rectorConfig->rule(AddMethodCallBasedStrictParamTypeRector::class); - $rectorConfig->rule(AddVoidReturnTypeWhereNoReturnRector::class); + $config->import(SetList::DEAD_CODE); + $config->import(SetList::TYPE_DECLARATION_STRICT); + $config->import(SetList::TYPE_DECLARATION); + $config->import(SetList::PHP_80); + $config->import(SetList::PHP_74); + $config->import(SetList::PHP_73); + $config->import(SetList::EARLY_RETURN); + $config->import(SetList::CODE_QUALITY); - $rectorConfig->ruleWithConfiguration(TypedPropertyRector::class, [ - TypedPropertyRector::INLINE_PUBLIC => true, - ]); + // register a single rule + $config->rule(InlineConstructorDefaultToPropertyRector::class); + $config->rule(RemoveExtraParametersRector::class); // define sets of rules - $rectorConfig->sets([ + $config->sets([ LevelSetList::UP_TO_PHP_80 ]); - $rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon'); + $config->phpstanConfig(__DIR__ . '/phpstan.neon'); }; From 12378cdb412034e635cfad12a3fa0699204012fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Tue, 10 May 2022 00:12:48 +0200 Subject: [PATCH 3/7] Upgrade code to PHP 8 --- composer.json | 2 +- .../Exception/ImageWorkshopLayerException.php | 2 +- .../Exception/ImageWorkshopLibException.php | 2 +- src/Core/ImageWorkshopLayer.php | 590 +++++------------- src/Core/ImageWorkshopLib.php | 101 +-- src/Exception/ImageWorkshopBaseException.php | 20 +- src/Exception/ImageWorkshopException.php | 2 +- src/Exif/ExifOrientations.php | 18 +- src/ImageWorkshop.php | 67 +- tests/Core/ImageWorkshopLayerTest.php | 24 +- 10 files changed, 249 insertions(+), 579 deletions(-) diff --git a/composer.json b/composer.json index de0d55c..2879b3a 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } ], "require": { - "php": ">=7.2.0", + "php": "^8.0", "ext-fileinfo": "*", "ext-gd": "*" }, diff --git a/src/Core/Exception/ImageWorkshopLayerException.php b/src/Core/Exception/ImageWorkshopLayerException.php index c332a60..71fd7e6 100644 --- a/src/Core/Exception/ImageWorkshopLayerException.php +++ b/src/Core/Exception/ImageWorkshopLayerException.php @@ -2,7 +2,7 @@ namespace PHPImageWorkshop\Core\Exception; -use PHPImageWorkshop\Exception\ImageWorkshopBaseException as ImageWorkshopBaseException; +use PHPImageWorkshop\Exception\ImageWorkshopBaseException; /** * ImageWorkshopLayerException diff --git a/src/Core/Exception/ImageWorkshopLibException.php b/src/Core/Exception/ImageWorkshopLibException.php index 2f72bec..350e933 100644 --- a/src/Core/Exception/ImageWorkshopLibException.php +++ b/src/Core/Exception/ImageWorkshopLibException.php @@ -2,7 +2,7 @@ namespace PHPImageWorkshop\Core\Exception; -use PHPImageWorkshop\Exception\ImageWorkshopBaseException as ImageWorkshopBaseException; +use PHPImageWorkshop\Exception\ImageWorkshopBaseException; /** * ImageWorkshopLibException diff --git a/src/Core/ImageWorkshopLayer.php b/src/Core/ImageWorkshopLayer.php index 16377b7..bde77c6 100644 --- a/src/Core/ImageWorkshopLayer.php +++ b/src/Core/ImageWorkshopLayer.php @@ -2,6 +2,7 @@ namespace PHPImageWorkshop\Core; +use GdImage; use PHPImageWorkshop\Exception\ImageWorkshopException; use PHPImageWorkshop\Exif\ExifOrientations; use PHPImageWorkshop\ImageWorkshop; @@ -24,119 +25,109 @@ class ImageWorkshopLayer // =================================================================================== /** - * @var int - * * Width of the group model * Default: 800 */ - protected $width; + protected int $width; /** - * @var int - * * Height of the group model * Default: 600 */ - protected $height; + protected int $height; /** * @var self[] * * Layers (and groups) */ - public $layers; + public array $layers; /** * @var int[] * * Levels of the sublayers in the stack */ - protected $layerLevels; + protected array $layerLevels; /** - * @var array + * @var array * * Positions (x and y) of the sublayers in the stack */ - protected $layerPositions; + protected array $layerPositions; /** - * @var int - * * Id of the last indexed sublayer in the stack */ - protected $lastLayerId; + protected int $lastLayerId; /** - * @var int - * * The highest sublayer level */ - protected $highestLayerLevel; + protected int $highestLayerLevel; /** - * @var resource|object - * * Background Image */ - protected $image; + protected GdImage $image; /** * @var array * * Exif data */ - protected $exif; + protected array $exif; /** * @var string */ - const UNIT_PIXEL = 'pixel'; + public const UNIT_PIXEL = 'pixel'; /** * @var string */ - const UNIT_PERCENT = 'percent'; + public const UNIT_PERCENT = 'percent'; /** - * @var integer + * @var int */ - const ERROR_GD_NOT_INSTALLED = 1; + public const ERROR_GD_NOT_INSTALLED = 1; /** - * @var integer + * @var int */ - const ERROR_PHP_IMAGE_VAR_NOT_USED = 2; + public const ERROR_PHP_IMAGE_VAR_NOT_USED = 2; /** - * @var integer + * @var int */ - const ERROR_FONT_NOT_FOUND = 3; + public const ERROR_FONT_NOT_FOUND = 3; /** - * @var integer + * @var int */ - const METHOD_DEPRECATED = 4; + public const METHOD_DEPRECATED = 4; /** - * @var integer + * @var int */ - const ERROR_NEGATIVE_NUMBER_USED = 5; + public const ERROR_NEGATIVE_NUMBER_USED = 5; /** - * @var integer + * @var int */ - const ERROR_NOT_WRITABLE_FOLDER = 6; + public const ERROR_NOT_WRITABLE_FOLDER = 6; /** - * @var integer + * @var int */ - const ERROR_NOT_SUPPORTED_FORMAT = 7; + public const ERROR_NOT_SUPPORTED_FORMAT = 7; /** - * @var integer + * @var int */ - const ERROR_UNKNOW = 8; + public const ERROR_UNKNOW = 8; // =================================================================================== // Methods @@ -145,24 +136,12 @@ class ImageWorkshopLayer // Magicals // ========================================================= - /** - * Constructor - * - * @param resource|object $image - */ - public function __construct($image, array $exif = array()) + public function __construct(GdImage $image, array $exif = array()) { if (!extension_loaded('gd')) { throw new ImageWorkshopLayerException('PHPImageWorkshop requires the GD extension to be loaded.', static::ERROR_GD_NOT_INSTALLED); } - if ( - !in_array(gettype($image), array('resource', '\resource')) - && (PHP_VERSION_ID >= 80000 && gettype($image) !=='object') // GD return an object since PHP 8.0 - ) { - throw new ImageWorkshopLayerException('You must give a php image var to initialize a layer.', static::ERROR_PHP_IMAGE_VAR_NOT_USED); - } - $this->width = imagesx($image); $this->height = imagesy($image); $this->image = $image; @@ -191,15 +170,9 @@ public function __clone() * * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html * - * @param int $layerLevel - * @param ImageWorkshopLayer $layer - * @param int $positionX - * @param int $positionY - * @param string $position - * - * @return array + * @return array{layerLevel: int, id: int} */ - public function addLayer($layerLevel, $layer, $positionX = 0, $positionY = 0, $position = 'LT') + public function addLayer(int $layerLevel, ImageWorkshopLayer $layer, int $positionX = 0, int $positionY = 0, string $position = 'LT'): array { return $this->indexLayer($layerLevel, $layer, $positionX, $positionY, $position); } @@ -211,14 +184,9 @@ public function addLayer($layerLevel, $layer, $positionX = 0, $positionY = 0, $p * * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html * - * @param ImageWorkshopLayer $layer - * @param int $positionX - * @param int $positionY - * @param string $position - * - * @return array + * @return array{layerLevel: int, id: int} */ - public function addLayerOnTop($layer, $positionX = 0, $positionY = 0, $position = 'LT') + public function addLayerOnTop(ImageWorkshopLayer $layer, int $positionX = 0, int $positionY = 0, string $position = 'LT') { return $this->indexLayer($this->highestLayerLevel + 1, $layer, $positionX, $positionY, $position); } @@ -230,14 +198,9 @@ public function addLayerOnTop($layer, $positionX = 0, $positionY = 0, $position * * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html * - * @param ImageWorkshopLayer $layer - * @param int $positionX - * @param int $positionY - * @param string $position - * - * @return array + * @return array{layerLevel: int, id: int} */ - public function addLayerBelow($layer, $positionX = 0, $positionY = 0, $position = 'LT') + public function addLayerBelow(ImageWorkshopLayer $layer, int $positionX = 0, int $positionY = 0, string $position = 'LT') { return $this->indexLayer(1, $layer, $positionX, $positionY, $position); } @@ -249,10 +212,9 @@ public function addLayerBelow($layer, $positionX = 0, $positionY = 0, $position * Move a sublayer on the top of a group stack * Return new sublayer level if success or false otherwise * - * @param int $layerId - * @return mixed + * @return int|false */ - public function moveTop($layerId) + public function moveTop(int $layerId): int|bool { return $this->moveTo($layerId, $this->highestLayerLevel, false); } @@ -261,11 +223,9 @@ public function moveTop($layerId) * Move a sublayer to the level 1 of a group stack * Return new sublayer level if success or false otherwise * - * @param int $layerId - * - * @return mixed + * @return int|false */ - public function moveBottom($layerId) + public function moveBottom(int $layerId): int|bool { return $this->moveTo($layerId, 1, true); } @@ -277,13 +237,9 @@ public function moveBottom($layerId) * Set $insertUnderTargetedLayer true if you want to move the sublayer under the other sublayer at the targeted level, * or false to insert it on the top of the other sublayer at the targeted level * - * @param int $layerId - * @param int $level - * @param boolean $insertUnderTargetedLayer - * - * @return mixed + * @return int|false */ - public function moveTo($layerId, $level, $insertUnderTargetedLayer = true) + public function moveTo(int $layerId, int $level, bool $insertUnderTargetedLayer = true) { // if the sublayer exists in stack if ($this->isLayerInIndex($layerId)) { @@ -300,7 +256,7 @@ public function moveTo($layerId, $level, $insertUnderTargetedLayer = true) } // Not the same level than the current level - if ($layerOldLevel != $level) { + if ($layerOldLevel !== $level) { $isUnderAndNewLevelHigher = $isUnderAndNewLevelLower = $isOnTopAndNewLevelHigher = $isOnTopAndNewLevelLower = false; if ($insertUnderTargetedLayer) { // Under level @@ -367,11 +323,9 @@ public function moveTo($layerId, $level, $insertUnderTargetedLayer = true) * Move up a sublayer in the stack (level +1) * Return new sublayer level if success, false otherwise * - * @param int $layerId - * - * @return mixed + * @return int|false */ - public function moveUp($layerId) + public function moveUp(int $layerId): int|bool { if ($this->isLayerInIndex($layerId)) { // if the sublayer exists in the stack $layerOldLevel = $this->getLayerLevel($layerId); @@ -385,11 +339,9 @@ public function moveUp($layerId) * Move down a sublayer in the stack (level -1) * Return new sublayer level if success, false otherwise * - * @param int $layerId - * - * @return mixed + * @return int|false */ - public function moveDown($layerId) + public function moveDown(int $layerId): int|bool { if ($this->isLayerInIndex($layerId)) { // if the sublayer exists in the stack $layerOldLevel = $this->getLayerLevel($layerId); @@ -406,17 +358,12 @@ public function moveDown($layerId) * Merge a sublayer with another sublayer below it in the stack * Note: the result layer will conserve the given id * Return true if success or false if layer isn't found or doesn't have a layer under it in the stack - * - * @param int $layerId - * - * @return boolean */ - public function mergeDown($layerId) + public function mergeDown(int $layerId): bool { // if the layer exists in document if ($this->isLayerInIndex($layerId)) { $layerLevel = $this->getLayerLevel($layerId); - $layerPositions = $this->getLayerPositions($layerId); $layer = $this->getLayer($layerId); $layerWidth = $layer->getWidth(); $layerHeight = $layer->getHeight(); @@ -489,7 +436,7 @@ public function mergeDown($layerId) /** * Merge sublayers in the stack on the layer background */ - public function mergeAll() + public function mergeAll(): void { $this->image = $this->getResult(); $this->clearStack(); @@ -501,19 +448,16 @@ public function mergeAll() * Otherwise, it will be set at 0 and 0 * * @param string $unit Use one of `UNIT_*` constants, "UNIT_PIXEL" by default - * @param resource|object $image - * @param int $positionX - * @param int $positionY */ - public function pasteImage($unit, $image, $positionX = 0, $positionY = 0) + public function pasteImage(string $unit, GdImage $image, int $positionX = 0, int $positionY = 0): void { if (!in_array($unit, [self::UNIT_PIXEL, self::UNIT_PERCENT])) { throw ImageWorkshopException::invalidUnitArgument(); } - if ($unit == self::UNIT_PERCENT) { - $positionX = round(($positionX / 100) * $this->width); - $positionY = round(($positionY / 100) * $this->height); + if ($unit === self::UNIT_PERCENT) { + $positionX = (int) round(($positionX / 100) * $this->width); + $positionY = (int) round(($positionY / 100) * $this->height); } imagecopy($this->image, $image, $positionX, $positionY, 0, 0, imagesx($image), imagesy($image)); @@ -524,14 +468,8 @@ public function pasteImage($unit, $image, $positionX = 0, $positionY = 0) /** * Change the position of a sublayer for new positions - * - * @param int $layerId - * @param int $newPosX - * @param int $newPosY - * - * @return boolean */ - public function changePosition($layerId, $newPosX = null, $newPosY = null) + public function changePosition(int $layerId, int $newPosX = null, int $newPosY = null): bool { // if the sublayer exists in the stack if ($this->isLayerInIndex($layerId)) { @@ -552,13 +490,9 @@ public function changePosition($layerId, $newPosX = null, $newPosY = null) /** * Apply a translation on a sublayer that change its positions * - * @param int $layerId - * @param int $addedPosX - * @param int $addedPosY - * - * @return mixed (array of new positions or false if fail) + * @return array{x: int, y: int}|false */ - public function applyTranslation($layerId, $addedPosX = null, $addedPosY = null) + public function applyTranslation(int $layerId, int $addedPosX = null, int $addedPosY = null): array|bool { // if the sublayer exists in the stack if ($this->isLayerInIndex($layerId)) { @@ -581,12 +515,8 @@ public function applyTranslation($layerId, $addedPosX = null, $addedPosY = null) /** * Delete a layer (return true if success, false if no sublayer is found) - * - * @param int $layerId - * - * @return boolean */ - public function remove($layerId) + public function remove(int $layerId): bool { // if the layer exists in document if ($this->isLayerInIndex($layerId)) { @@ -627,9 +557,9 @@ public function remove($layerId) /** * Reset the layer stack * - * @boolean $deleteSubImgVar Delete sublayers image resource var + * @param bool $deleteSubImgVar Delete sublayers image resource var */ - public function clearStack($deleteSubImgVar = true) + public function clearStack(bool $deleteSubImgVar = true): void { if ($deleteSubImgVar) { foreach ($this->layers as $layer) { @@ -654,18 +584,11 @@ public function clearStack($deleteSubImgVar = true) /** * Resize the layer by specifying pixel * - * @param int $newWidth - * @param int $newHeight - * @param boolean $converseProportion - * @param int $positionX - * @param int $positionY - * @param string $position - * * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html * * $positionX, $positionY, $position can be ignored unless you choose a new width AND a new height AND to conserve proportion. */ - public function resizeInPixel($newWidth = null, $newHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM') + public function resizeInPixel(int $newWidth = null, int $newHeight = null, bool $converseProportion = false, int $positionX = 0, int $positionY = 0, string $position = 'MM'): void { $this->resize(self::UNIT_PIXEL, $newWidth, $newHeight, $converseProportion, $positionX, $positionY, $position); } @@ -673,30 +596,19 @@ public function resizeInPixel($newWidth = null, $newHeight = null, $converseProp /** * Resize the layer by specifying a percent * - * @param float $percentWidth - * @param float $percentHeight - * @param boolean $converseProportion - * @param int $positionX - * @param int $positionY - * @param string $position - * * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html * * $positionX, $positionY, $position can be ignored unless you choose a new width AND a new height AND to conserve proportion. */ - public function resizeInPercent($percentWidth = null, $percentHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM') + public function resizeInPercent(float $percentWidth = null, float $percentHeight = null, bool $converseProportion = false, int $positionX = 0, int $positionY = 0, string $position = 'MM'): void { $this->resize(self::UNIT_PERCENT, $percentWidth, $percentHeight, $converseProportion, $positionX, $positionY, $position); } /** * Resize the layer to fit a bounding box by specifying pixel - * - * @param int $width - * @param int $height - * @param boolean $converseProportion */ - public function resizeToFit($width, $height, $converseProportion = false) + public function resizeToFit(int $width, int $height, bool $converseProportion = false): void { if ($this->getWidth() <= $width && $this->getHeight() <= $height) { return; @@ -707,37 +619,29 @@ public function resizeToFit($width, $height, $converseProportion = false) $height = min($height, $this->getHeight()); } - $this->resize(self::UNIT_PIXEL, $width, $height, $converseProportion ? 2 : false); + $this->resize(self::UNIT_PIXEL, $width, $height, $converseProportion, createNewLayer: false); // Fix bug here } /** * Resize the layer * - * @param string $unit Use one of `UNIT_*` constants, "UNIT_PIXEL" by default - * @param mixed $newWidth (integer or float) - * @param mixed $newHeight (integer or float) - * @param boolean $converseProportion - * @param mixed $positionX (integer or float) - * @param mixed $positionY (integer or float) - * @param string $position - * * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html * * $positionX, $positionY, $position can be ignored unless you choose a new width AND a new height AND to conserve proportion. */ - public function resize($unit = self::UNIT_PIXEL, $newWidth = null, $newHeight = null, $converseProportion = false, $positionX = 0, $positionY = 0, $position = 'MM') + public function resize(string $unit = self::UNIT_PIXEL, int|float $newWidth = null, int|float $newHeight = null, bool $converseProportion = false, int $positionX = 0, int $positionY = 0, string $position = 'MM', bool $createNewLayer = true): void { if (is_numeric($newWidth) || is_numeric($newHeight)) { $widthResizePercent = 100; $heightResizePercent = 100; - if ($unit == self::UNIT_PERCENT) { + if ($unit === self::UNIT_PERCENT) { if ($newWidth) { - $newWidth = round(($newWidth / 100) * $this->width); + $newWidth = (int) round(($newWidth / 100) * $this->width); } if ($newHeight) { - $newHeight = round(($newHeight / 100) * $this->height); + $newHeight = (int) round(($newHeight / 100) * $this->height); } } @@ -767,7 +671,7 @@ public function resize($unit = self::UNIT_PIXEL, $newWidth = null, $newHeight = } } - if ($converseProportion !== 2 && ($this->getWidth() != $newWidth || $this->getHeight() != $newHeight)) { + if ($converseProportion && $createNewLayer && ($this->getWidth() !== $newWidth || $this->getHeight() !== $newHeight)) { $layerTmp = ImageWorkshop::initVirginLayer($newWidth, $newHeight); $layerTmp->addLayer(1, $this, $positionX, $positionY, $position); @@ -789,7 +693,7 @@ public function resize($unit = self::UNIT_PIXEL, $newWidth = null, $newHeight = $this->lastLayerId = $layerTmp->layers[1]->getLastLayerId(); $this->highestLayerLevel = $layerTmp->layers[1]->getHighestLayerLevel(); - $translations = $layerTmp->getLayerPositions(1); + $translations = $layerTmp->getLayerPosition(1); foreach ($this->layers as $id => $layer) { $this->applyTranslation($id, $translations['x'], $translations['y']); @@ -803,11 +707,11 @@ public function resize($unit = self::UNIT_PIXEL, $newWidth = null, $newHeight = return; } elseif ($newWidth) { $widthResizePercent = $newWidth / ($this->width / 100); - $newHeight = round(($widthResizePercent / 100) * $this->height); + $newHeight = (int) round(($widthResizePercent / 100) * $this->height); $heightResizePercent = $widthResizePercent; } elseif ($newHeight) { $heightResizePercent = $newHeight / ($this->height / 100); - $newWidth = round(($heightResizePercent / 100) * $this->width); + $newWidth = (int) round(($heightResizePercent / 100) * $this->width); $widthResizePercent = $heightResizePercent; } } elseif (($newWidth && !$newHeight) || (!$newWidth && $newHeight)) { // New width OR new height is given @@ -851,22 +755,16 @@ public function resize($unit = self::UNIT_PIXEL, $newWidth = null, $newHeight = /** * Resize the layer by its largest side by specifying pixel - * - * @param int $newLargestSideWidth - * @param boolean $converseProportion */ - public function resizeByLargestSideInPixel($newLargestSideWidth, $converseProportion = false) + public function resizeByLargestSideInPixel(int $newLargestSideWidth, bool $converseProportion = false): void { $this->resizeByLargestSide(self::UNIT_PIXEL, $newLargestSideWidth, $converseProportion); } /** * Resize the layer by its largest side by specifying percent - * - * @param int $newLargestSideWidth percent - * @param boolean $converseProportion */ - public function resizeByLargestSideInPercent($newLargestSideWidth, $converseProportion = false) + public function resizeByLargestSideInPercent(int $newLargestSideWidth, bool $converseProportion = false): void { $this->resizeByLargestSide(self::UNIT_PERCENT, $newLargestSideWidth, $converseProportion); } @@ -874,18 +772,16 @@ public function resizeByLargestSideInPercent($newLargestSideWidth, $converseProp /** * Resize the layer by its largest side * - * @param string $unit * @param int $newLargestSideWidth percent - * @param boolean $converseProportion */ - public function resizeByLargestSide($unit, $newLargestSideWidth, $converseProportion = false) + public function resizeByLargestSide(string $unit, int $newLargestSideWidth, bool $converseProportion = false): void { if (!in_array($unit, [self::UNIT_PIXEL, self::UNIT_PERCENT])) { throw ImageWorkshopException::invalidUnitArgument(); } - if ($unit == self::UNIT_PERCENT) { - $newLargestSideWidth = round(($newLargestSideWidth / 100) * $this->getLargestSideWidth()); + if ($unit === self::UNIT_PERCENT) { + $newLargestSideWidth = (int) round(($newLargestSideWidth / 100) * $this->getLargestSideWidth()); } if ($this->getWidth() > $this->getHeight()) { @@ -897,11 +793,8 @@ public function resizeByLargestSide($unit, $newLargestSideWidth, $conversePropor /** * Resize the layer by its narrow side by specifying pixel - * - * @param int $newNarrowSideWidth - * @param boolean $converseProportion */ - public function resizeByNarrowSideInPixel($newNarrowSideWidth, $converseProportion = false) + public function resizeByNarrowSideInPixel(int $newNarrowSideWidth, bool $converseProportion = false): void { $this->resizeByNarrowSide(self::UNIT_PIXEL, $newNarrowSideWidth, $converseProportion); } @@ -910,28 +803,23 @@ public function resizeByNarrowSideInPixel($newNarrowSideWidth, $converseProporti * Resize the layer by its narrow side by specifying percent * * @param int $newNarrowSideWidth percent - * @param boolean $converseProportion */ - public function resizeByNarrowSideInPercent($newNarrowSideWidth, $converseProportion = false) + public function resizeByNarrowSideInPercent(int $newNarrowSideWidth, bool $converseProportion = false): void { $this->resizeByNarrowSide(self::UNIT_PERCENT, $newNarrowSideWidth, $converseProportion); } /** * Resize the layer by its narrow side - * - * @param string $unit - * @param int $newNarrowSideWidth - * @param boolean $converseProportion */ - public function resizeByNarrowSide($unit, $newNarrowSideWidth, $converseProportion = false) + public function resizeByNarrowSide(string $unit, int $newNarrowSideWidth, bool $converseProportion = false): void { if (!in_array($unit, [self::UNIT_PIXEL, self::UNIT_PERCENT])) { throw ImageWorkshopException::invalidUnitArgument(); } - if ($unit == self::UNIT_PERCENT) { - $newNarrowSideWidth = round(($newNarrowSideWidth / 100) * $this->getNarrowSideWidth()); + if ($unit === self::UNIT_PERCENT) { + $newNarrowSideWidth = (int) round(($newNarrowSideWidth / 100) * $this->getNarrowSideWidth()); } if ($this->getWidth() < $this->getHeight()) { @@ -946,14 +834,8 @@ public function resizeByNarrowSide($unit, $newNarrowSideWidth, $converseProporti * * $backgroundColor: can be set transparent (The script will be longer to execute) * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html - * - * @param int $width - * @param int $height - * @param int $positionX - * @param int $positionY - * @param string $position */ - public function cropInPixel($width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT') + public function cropInPixel(int $width = 0, int $height = 0, int $positionX = 0, int $positionY = 0, string $position = 'LT'): void { $this->crop(self::UNIT_PIXEL, $width, $height, $positionX, $positionY, $position); } @@ -963,14 +845,8 @@ public function cropInPixel($width = 0, $height = 0, $positionX = 0, $positionY * * $backgroundColor can be set transparent (but script could be long to execute) * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html - * - * @param float $percentWidth - * @param float $percentHeight - * @param float $positionXPercent - * @param float $positionYPercent - * @param string $position */ - public function cropInPercent($percentWidth = 0.0, $percentHeight = 0.0, $positionXPercent = 0.0, $positionYPercent = 0.0, $position = 'LT') + public function cropInPercent(float $percentWidth = 0.0, float $percentHeight = 0.0, float $positionXPercent = 0.0, float $positionYPercent = 0.0, string $position = 'LT'): void { $this->crop(self::UNIT_PERCENT, $percentWidth, $percentHeight, $positionXPercent, $positionYPercent, $position); } @@ -980,34 +856,27 @@ public function cropInPercent($percentWidth = 0.0, $percentHeight = 0.0, $positi * * $backgroundColor can be set transparent (but script could be long to execute) * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html - * - * @param string $unit - * @param mixed $width (integer or float) - * @param mixed $height (integer or float) - * @param mixed $positionX (integer or float) - * @param mixed $positionY (integer or float) - * @param string $position */ - public function crop($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT') + public function crop(string $unit = self::UNIT_PIXEL, int|float $width = 0, int|float $height = 0, int|float $positionX = 0, int|float $positionY = 0, string $position = 'LT'): void { if ($width < 0 || $height < 0) { throw new ImageWorkshopLayerException('You can\'t use negative $width or $height for "'.__METHOD__.'" method.', static::ERROR_NEGATIVE_NUMBER_USED); } - if ($unit == self::UNIT_PERCENT) { - $width = round(($width / 100) * $this->width); - $height = round(($height / 100) * $this->height); + if ($unit === self::UNIT_PERCENT) { + $width = (int) round(($width / 100) * $this->width); + $height = (int) round(($height / 100) * $this->height); - $positionX = round(($positionX / 100) * $this->width); - $positionY = round(($positionY / 100) * $this->height); + $positionX = (int) round(($positionX / 100) * $this->width); + $positionY = (int) round(($positionY / 100) * $this->height); } - if (($width != $this->width || $positionX == 0) || ($height != $this->height || $positionY == 0)) { - if ($width == 0) { + if (($width !== $this->width || $positionX === 0) || ($height !== $this->height || $positionY === 0)) { + if ($width === 0) { $width = 1; } - if ($height == 0) { + if ($height === 0) { $height = 1; } @@ -1039,14 +908,8 @@ public function crop($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positio * * $backgroundColor: can be set transparent (The script will be longer to execute) * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html - * - * @param int $width - * @param int $height - * @param int $positionX - * @param int $positionY - * @param string $position */ - public function cropToAspectRatioInPixel($width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT') + public function cropToAspectRatioInPixel(int $width = 0, int $height = 0, int $positionX = 0, int $positionY = 0, string $position = 'LT'): void { $this->cropToAspectRatio(self::UNIT_PIXEL, $width, $height, $positionX, $positionY, $position); } @@ -1056,14 +919,8 @@ public function cropToAspectRatioInPixel($width = 0, $height = 0, $positionX = 0 * * $backgroundColor can be set transparent (but script could be long to execute) * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html - * - * @param int $width - * @param int $height - * @param float $positionXPercent - * @param float $positionYPercent - * @param string $position */ - public function cropToAspectRatioInPercent($width = 0, $height = 0, $positionXPercent = 0.0, $positionYPercent = 0.0, $position = 'LT') + public function cropToAspectRatioInPercent(int $width = 0, int $height = 0, float $positionXPercent = 0.0, float $positionYPercent = 0.0, string $position = 'LT'): void { $this->cropToAspectRatio(self::UNIT_PERCENT, $width, $height, $positionXPercent, $positionYPercent, $position); } @@ -1073,25 +930,18 @@ public function cropToAspectRatioInPercent($width = 0, $height = 0, $positionXPe * * $backgroundColor can be set transparent (but script could be long to execute) * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html - * - * @param string $unit - * @param int $width (integer or float) - * @param int $height (integer or float) - * @param mixed $positionX (integer or float) - * @param mixed $positionY (integer or float) - * @param string $position */ - public function cropToAspectRatio($unit = self::UNIT_PIXEL, $width = 0, $height = 0, $positionX = 0, $positionY = 0, $position = 'LT') + public function cropToAspectRatio(string $unit = self::UNIT_PIXEL, int $width = 0, int $height = 0, int|float $positionX = 0, int|float $positionY = 0, string $position = 'LT'): void { if ($width < 0 || $height < 0) { throw new ImageWorkshopLayerException('You can\'t use negative $width or $height for "'.__METHOD__.'" method.', static::ERROR_NEGATIVE_NUMBER_USED); } - if ($width == 0) { + if ($width === 0) { $width = 1; } - if ($height == 0) { + if ($height === 0) { $height = 1; } @@ -1103,9 +953,9 @@ public function cropToAspectRatio($unit = self::UNIT_PIXEL, $width = 0, $height $newHeight = $this->height; } - if ($unit == self::UNIT_PERCENT) { - $positionX = round(($positionX / 100) * ($this->width - $newWidth)); - $positionY = round(($positionY / 100) * ($this->height - $newHeight)); + if ($unit === self::UNIT_PERCENT) { + $positionX = (int) round(($positionX / 100) * ($this->width - $newWidth)); + $positionY = (int) round(($positionY / 100) * ($this->height - $newHeight)); } $this->cropInPixel($newWidth, $newHeight, $positionX, $positionY, $position); @@ -1116,12 +966,8 @@ public function cropToAspectRatio($unit = self::UNIT_PIXEL, $width = 0, $height * * $backgroundColor can be set transparent (but script could be long to execute) * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html - * - * @param int $positionX - * @param int $positionY - * @param string $position */ - public function cropMaximumInPixel($positionX = 0, $positionY = 0, $position = 'LT') + public function cropMaximumInPixel(int $positionX = 0, int $positionY = 0, string $position = 'LT'): void { $this->cropMaximum(self::UNIT_PIXEL, $positionX, $positionY, $position); } @@ -1131,12 +977,8 @@ public function cropMaximumInPixel($positionX = 0, $positionY = 0, $position = ' * * $backgroundColor can be set transparent (but script could be long to execute) * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html - * - * @param int $positionXPercent - * @param int $positionYPercent - * @param string $position */ - public function cropMaximumInPercent($positionXPercent = 0, $positionYPercent = 0, $position = 'LT') + public function cropMaximumInPercent(int $positionXPercent = 0, int $positionYPercent = 0, $position = 'LT'): void { $this->cropMaximum(self::UNIT_PERCENT, $positionXPercent, $positionYPercent, $position); } @@ -1146,19 +988,14 @@ public function cropMaximumInPercent($positionXPercent = 0, $positionYPercent = * * $backgroundColor can be set transparent (but script could be long to execute) * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html - * - * @param string $unit - * @param int $positionX - * @param int $positionY - * @param string $position */ - public function cropMaximum($unit = self::UNIT_PIXEL, $positionX = 0, $positionY = 0, $position = 'LT') + public function cropMaximum(string $unit = self::UNIT_PIXEL, int $positionX = 0, int $positionY = 0, string $position = 'LT'): void { $narrowSide = $this->getNarrowSideWidth(); - if ($unit == self::UNIT_PERCENT) { - $positionX = round(($positionX / 100) * $this->width); - $positionY = round(($positionY / 100) * $this->height); + if ($unit === self::UNIT_PERCENT) { + $positionX = (int) round(($positionX / 100) * $this->width); + $positionY = (int) round(($positionY / 100) * $this->height); } $this->cropInPixel($narrowSide, $narrowSide, $positionX, $positionY, $position); @@ -1166,14 +1003,12 @@ public function cropMaximum($unit = self::UNIT_PIXEL, $positionX = 0, $positionY /** * Rotate the layer (in degree) - * - * @param float $degrees */ - public function rotate($degrees) + public function rotate(float $degrees): void { - if ($degrees != 0) { + if ($degrees !== 0) { if ($degrees < -360 || $degrees > 360) { - $degrees = $degrees % 360; + $degrees = (float) ($degrees % 360); } if ($degrees < 0 && $degrees >= -360) { @@ -1211,7 +1046,7 @@ public function rotate($degrees) $this->layers[$layerId]->rotate($degrees); - $ro = sqrt(pow($smallImageCenter['x'], 2) + pow($smallImageCenter['y'], 2)); + $ro = sqrt($smallImageCenter['x'] ** 2 + $smallImageCenter['y'] ** 2); $teta = (acos($smallImageCenter['x'] / $ro)) * 180 / pi(); @@ -1243,11 +1078,8 @@ public function rotate($degrees) /** * Change the opacity of the layer * $recursive: apply it on sublayers - * - * @param int $opacity - * @param boolean $recursive */ - public function opacity($opacity, $recursive = true) + public function opacity(int $opacity, bool $recursive = true): void { if ($recursive) { $layers = $this->layers; @@ -1272,19 +1104,14 @@ public function opacity($opacity, $recursive = true) * Be careful: some filters can damage transparent images, use it sparingly ! (A good pratice is to use mergeAll on your layer before applying a filter) * * @param int $filterType (http://www.php.net/manual/en/function.imagefilter.php) - * @param int $arg1 - * @param int $arg2 - * @param int $arg3 - * @param int $arg4 - * @param boolean $recursive */ - public function applyFilter($filterType, $arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null, $recursive = false) + public function applyFilter(int $filterType, int $arg1 = null, int $arg2 = null, int $arg3 = null, int $arg4 = null, bool $recursive = false): void { - if ($filterType == IMG_FILTER_COLORIZE) { + if ($filterType === IMG_FILTER_COLORIZE) { imagefilter($this->image, $filterType, $arg1, $arg2, $arg3, $arg4); - } elseif ($filterType == IMG_FILTER_BRIGHTNESS || $filterType == IMG_FILTER_CONTRAST || $filterType == IMG_FILTER_SMOOTH) { + } elseif ($filterType === IMG_FILTER_BRIGHTNESS || $filterType === IMG_FILTER_CONTRAST || $filterType === IMG_FILTER_SMOOTH) { imagefilter($this->image, $filterType, $arg1); - } elseif ($filterType == IMG_FILTER_PIXELATE) { + } elseif ($filterType === IMG_FILTER_PIXELATE) { imagefilter($this->image, $filterType, $arg1, $arg2); } else { imagefilter($this->image, $filterType); @@ -1301,10 +1128,8 @@ public function applyFilter($filterType, $arg1 = null, $arg2 = null, $arg3 = nul /** * Apply horizontal or vertical flip (Transformation) - * - * @param string $type */ - public function flip($type = 'horizontal') + public function flip(string $type = 'horizontal'): void { $layers = $this->layers; @@ -1315,14 +1140,14 @@ public function flip($type = 'horizontal') $temp = ImageWorkshopLib::generateImage($this->width, $this->height); - if ($type == 'horizontal') { + if ($type === 'horizontal') { imagecopyresampled($temp, $this->image, 0, 0, $this->width - 1, 0, $this->width, $this->height, -$this->width, $this->height); $this->image = $temp; foreach ($this->layerPositions as $layerId => $layerPositions) { $this->changePosition($layerId, $this->width - $this->layers[$layerId]->getWidth() - $layerPositions['x'], $layerPositions['y']); } - } elseif ($type == 'vertical') { + } elseif ($type === 'vertical') { imagecopyresampled($temp, $this->image, 0, 0, 0, $this->height - 1, $this->width, $this->height, $this->width, -1 * $this->height); $this->image = $temp; @@ -1336,20 +1161,13 @@ public function flip($type = 'horizontal') /** * Add a text on the background image of the layer using a default font registered in GD - * - * @param string $text - * @param int $font - * @param string $color - * @param int $positionX - * @param int $positionY - * @param string $align */ - public function writeText($text, $font = 1, $color = 'ffffff', $positionX = 0, $positionY = 0, $align = 'horizontal') + public function writeText(string $text, int $font = 1, string $color = 'ffffff', int $positionX = 0, int $positionY = 0, string $align = 'horizontal'): void { $RGBTextColor = ImageWorkshopLib::convertHexToRGB($color); $textColor = imagecolorallocate($this->image, $RGBTextColor['R'], $RGBTextColor['G'], $RGBTextColor['B']); - if ($align == 'horizontal') { + if ($align === 'horizontal') { imagestring($this->image, $font, $positionX, $positionY, $text, $textColor); } else { imagestringup($this->image, $font, $positionX, $positionY, $text, $textColor); @@ -1360,17 +1178,9 @@ public function writeText($text, $font = 1, $color = 'ffffff', $positionX = 0, $ * Add a text on the background image of the layer using a font localized at $fontPath * Return the text coordonates * - * @param string $text - * @param string $fontPath - * @param int $fontSize - * @param string $color - * @param int $positionX - * @param int $positionY - * @param int $fontRotation - * - * @return array + * @return array|false */ - public function write($text, $fontPath, $fontSize = 13, $color = 'ffffff', $positionX = 0, $positionY = 0, $fontRotation = 0) + public function write(string $text, string $fontPath, int $fontSize = 13, string $color = 'ffffff', int $positionX = 0, int $positionY = 0, int $fontRotation = 0): array|bool { if (!file_exists($fontPath)) { throw new ImageWorkshopLayerException('Can\'t find a font file at this path : "'.$fontPath.'".', static::ERROR_FONT_NOT_FOUND); @@ -1390,12 +1200,8 @@ public function write($text, $fontPath, $fontSize = 13, $color = 'ffffff', $posi * * $backgroundColor is really usefull if you want to save a JPG or GIF, because the transparency of the background * would be remove for a colored background, so you should choose a color like "ffffff" (white) - * - * @param string $backgroundColor - * - * @return resource|object */ - public function getResult($backgroundColor = null) + public function getResult(string $backgroundColor = null): GdImage { $imagesToMerge = array(); ksort($this->layerLevels); @@ -1404,7 +1210,7 @@ public function getResult($backgroundColor = null) $imagesToMerge[$layerLevel] = $this->layers[$layerId]->getResult(); // Layer positions - if ($this->layerPositions[$layerId]['x'] != 0 || $this->layerPositions[$layerId]['y'] != 0) { + if ($this->layerPositions[$layerId]['x'] !== 0 || $this->layerPositions[$layerId]['y'] !== 0) { $virginLayoutImageTmp = ImageWorkshopLib::generateImage($this->width, $this->height); ImageWorkshopLib::mergeTwoImages($virginLayoutImageTmp, $imagesToMerge[$layerLevel], $this->layerPositions[$layerId]['x'], $this->layerPositions[$layerId]['y'], 0, 0); $imagesToMerge[$layerLevel] = $virginLayoutImageTmp; @@ -1412,22 +1218,20 @@ public function getResult($backgroundColor = null) } } - $iterator = 1; $mergedImage = $this->image; ksort($imagesToMerge); - foreach ($imagesToMerge as $imageLevel => $image) { + foreach ($imagesToMerge as $image) { ImageWorkshopLib::mergeTwoImages($mergedImage, $image); - $iterator++; } $opacity = 127; - if ($backgroundColor && $backgroundColor != 'transparent') { + if ($backgroundColor !== 'transparent') { $opacity = 0; } - $backgroundImage = ImageWorkshopLib::generateImage($this->width, $this->height, $backgroundColor, $opacity); + $backgroundImage = ImageWorkshopLib::generateImage($this->width, $this->height, (string) $backgroundColor, $opacity); ImageWorkshopLib::mergeTwoImages($backgroundImage, $mergedImage); $mergedImage = $backgroundImage; unset($backgroundImage); @@ -1450,15 +1254,8 @@ public function getResult($backgroundColor = null) * $createFolders = true * $imageQuality = 95 * $backgroundColor = "ffffff" - * - * @param string $folder - * @param string $imageName - * @param boolean $createFolders - * @param string $backgroundColor - * @param int $imageQuality - * @param boolean|int $interlace */ - public function save($folder, $imageName, $createFolders = true, $backgroundColor = null, $imageQuality = 75, $interlace = false) + public function save(string $folder, string $imageName, bool $createFolders = true, string $backgroundColor = null, int $imageQuality = 75, bool $interlace = false): void { if (is_file($folder)) { throw new ImageWorkshopLayerException(sprintf('Destination folder "%s" is a file.', $folder), self::ERROR_NOT_WRITABLE_FOLDER); @@ -1489,7 +1286,7 @@ public function save($folder, $imageName, $createFolders = true, $backgroundColo chmod($dirname, 0777); } - if (($extension == 'jpg' || $extension == 'jpeg' || $extension == 'gif') && (!$backgroundColor || $backgroundColor == 'transparent')) { + if (($extension === 'jpg' || $extension === 'jpeg' || $extension === 'gif') && (!$backgroundColor || $backgroundColor === 'transparent')) { $backgroundColor = 'ffffff'; } @@ -1497,21 +1294,21 @@ public function save($folder, $imageName, $createFolders = true, $backgroundColo imageinterlace($image, $interlace); - if ($extension == 'jpg' || $extension == 'jpeg') { + if ($extension === 'jpg' || $extension === 'jpeg') { $isSaved = imagejpeg($image, $filename, $imageQuality); - } elseif ($extension == 'gif') { + } elseif ($extension === 'gif') { $isSaved = imagegif($image, $filename); - } elseif ($extension == 'png') { + } elseif ($extension === 'png') { if ($imageQuality >= 100) { $imageQuality = 0; } elseif ($imageQuality <= 0) { $imageQuality = 10; } else { - $imageQuality = round((100 - $imageQuality) / 10); + $imageQuality = (int) round((100 - $imageQuality) / 10); } $isSaved = imagepng($image, $filename, intval($imageQuality)); - } elseif ($extension == 'webp') { + } elseif ($extension === 'webp') { if (!function_exists('imagewebp')) { throw new ImageWorkshopLayerException(sprintf('Image format "%s" not supported by PHP version', $extension), self::ERROR_NOT_SUPPORTED_FORMAT); } @@ -1533,18 +1330,10 @@ public function save($folder, $imageName, $createFolders = true, $backgroundColo /** * Check if a sublayer exists in the stack for a given id - * - * @param int $layerId - * - * @return boolean */ - public function isLayerInIndex($layerId) + public function isLayerInIndex(int $layerId): bool { - if (array_key_exists($layerId, $this->layers)) { - return true; - } - - return false; + return array_key_exists($layerId, $this->layers); } // Getter / Setter @@ -1552,10 +1341,8 @@ public function isLayerInIndex($layerId) /** * Return the narrow side width of the layer - * - * @return integer */ - public function getNarrowSideWidth() + public function getNarrowSideWidth(): int { $narrowSideWidth = $this->getWidth(); @@ -1568,10 +1355,8 @@ public function getNarrowSideWidth() /** * Return the largest side width of the layer - * - * @return integer */ - public function getLargestSideWidth() + public function getLargestSideWidth(): int { $largestSideWidth = $this->getWidth(); @@ -1586,11 +1371,9 @@ public function getLargestSideWidth() * Get the level of a sublayer * Return sublayer level if success or false if layer isn't found * - * @param int $layerId - * - * @return mixed (integer or boolean) + * @return int|false */ - public function getLayerLevel($layerId) + public function getLayerLevel(int $layerId): int|bool { if ($this->isLayerInIndex($layerId)) { // if the layer exists in document return array_search($layerId, $this->layerLevels); @@ -1602,42 +1385,32 @@ public function getLayerLevel($layerId) /** * Get a sublayer in the stack * Don't forget to use clone method: $b = clone $a->getLayer(3); - * - * @param int $layerId - * - * @return self */ - public function getLayer($layerId) + public function getLayer(int $layerId): self { return $this->layers[$layerId]; } /** * Getter width - * - * @return integer */ - public function getWidth() + public function getWidth(): int { return $this->width; } /** * Getter height - * - * @return integer */ - public function getHeight() + public function getHeight(): int { return $this->height; } /** * Getter image - * - * @return resource|object */ - public function getImage() + public function getImage(): GdImage { return $this->image; } @@ -1645,9 +1418,9 @@ public function getImage() /** * Getter layers * - * @return array + * @return self[] */ - public function getLayers() + public function getLayers(): array { return $this->layers; } @@ -1655,29 +1428,31 @@ public function getLayers() /** * Getter layerLevels * - * @return array + * @return int[] */ - public function getLayerLevels() + public function getLayerLevels(): array { return $this->layerLevels; } /** - * Getter layerPositions - * - * Get all the positions of the sublayers, - * or when specifying $layerId, get the position of this sublayer + * Get all the positions of the sublayers * - * @param int $layerId - * - * @return mixed (array or boolean) + * @return array */ - public function getLayerPositions($layerId = null) + public function getLayerPositions(): array { - if (!$layerId) { - return $this->layerPositions; - } elseif ($this->isLayerInIndex($layerId)) { // if the sublayer exists in the stack + return $this->layerPositions; + } + /** + * Get the position of this sublayer + * + * @return array{x: int, y: int}|false + */ + public function getLayerPosition(int $layerId): array|bool + { + if ($this->isLayerInIndex($layerId)) { // if the sublayer exists in the stack return $this->layerPositions[$layerId]; } @@ -1686,20 +1461,16 @@ public function getLayerPositions($layerId = null) /** * Getter highestLayerLevel - * - * @return int */ - public function getHighestLayerLevel() + public function getHighestLayerLevel(): int { return $this->highestLayerLevel; } /** * Getter lastLayerId - * - * @return int */ - public function getLastLayerId() + public function getLastLayerId(): int { return $this->lastLayerId; } @@ -1710,7 +1481,7 @@ public function getLastLayerId() /** * Delete the current object */ - public function delete() + public function delete(): void { imagedestroy($this->image); $this->clearStack(); @@ -1719,7 +1490,7 @@ public function delete() /** * Create a new background image var from the old background image var */ - public function createNewVarFromBackgroundImage() + public function createNewVarFromBackgroundImage(): void { $virginImage = ImageWorkshopLib::generateImage($this->getWidth(), $this->getHeight()); // New background image @@ -1741,15 +1512,9 @@ public function createNewVarFromBackgroundImage() * Return an array containing the generated sublayer id and its final level: * array("layerLevel" => integer, "id" => integer) * - * @param int $layerLevel - * @param ImageWorkshopLayer $layer - * @param int $positionX - * @param int $positionY - * @param string $position - * - * @return array + * @return array{layerLevel: int, id: int} */ - protected function indexLayer($layerLevel, $layer, $positionX, $positionY, $position) + protected function indexLayer(int $layerLevel, ImageWorkshopLayer $layer, int $positionX, int $positionY, string $position): array { // Choose an id for the added layer $layerId = $this->lastLayerId + 1; @@ -1778,13 +1543,8 @@ protected function indexLayer($layerLevel, $layer, $positionX, $positionY, $posi /** * Index a layer level and update the layers levels in the document * Return the corrected level of the layer - * - * @param int $layerLevel - * @param int $layerId - * - * @return integer */ - protected function indexLevelInDocument($layerLevel, $layerId) + protected function indexLevelInDocument(int $layerLevel, int $layerId): int { if (array_key_exists($layerLevel, $this->layerLevels)) { // Level already exists @@ -1812,11 +1572,8 @@ protected function indexLevelInDocument($layerLevel, $layerId) /** * Update the positions of layers in the stack after cropping - * - * @param int $positionX - * @param int $positionY */ - public function updateLayerPositionsAfterCropping($positionX, $positionY) + public function updateLayerPositionsAfterCropping(int $positionX, int $positionY): void { foreach ($this->layers as $layerId => $layer) { $oldLayerPosX = $this->layerPositions[$layerId]['x']; @@ -1831,11 +1588,8 @@ public function updateLayerPositionsAfterCropping($positionX, $positionY) /** * Resize the background of a layer - * - * @param int $newWidth - * @param int $newHeight */ - public function resizeBackground($newWidth, $newHeight) + public function resizeBackground(int $newWidth, int $newHeight): void { $oldWidth = $this->width; $oldHeight = $this->height; @@ -1854,9 +1608,9 @@ public function resizeBackground($newWidth, $newHeight) /** * Fix image orientation based on exif data */ - public function fixOrientation() + public function fixOrientation(): void { - if (!isset($this->exif['Orientation']) || 0 == $this->exif['Orientation']) { + if (!isset($this->exif['Orientation']) || 0 === $this->exif['Orientation']) { return; } diff --git a/src/Core/ImageWorkshopLib.php b/src/Core/ImageWorkshopLib.php index 430f0e3..27f8cf1 100644 --- a/src/Core/ImageWorkshopLib.php +++ b/src/Core/ImageWorkshopLib.php @@ -2,7 +2,8 @@ namespace PHPImageWorkshop\Core; -use PHPImageWorkshop\Core\Exception\ImageWorkshopLibException as ImageWorkshopLibException; +use GdImage; +use PHPImageWorkshop\Core\Exception\ImageWorkshopLibException; /** * ImageWorkshopLib class @@ -17,48 +18,40 @@ class ImageWorkshopLib { /** - * @var integer + * @var int */ - const ERROR_FONT_NOT_FOUND = 3; + public const ERROR_FONT_NOT_FOUND = 3; /** * Calculate the left top positions of a layer inside a parent layer container * $position: http://phpimageworkshop.com/doc/22/corners-positions-schema-of-an-image.html * - * @param int $containerWidth - * @param int $containerHeight - * @param int $layerWidth - * @param int $layerHeight - * @param int $layerPositionX - * @param int $layerPositionY - * @param string $position - * - * @return array + * @return array{x: int, y: int} */ - public static function calculatePositions($containerWidth, $containerHeight, $layerWidth, $layerHeight, $layerPositionX, $layerPositionY, $position = 'LT') + public static function calculatePositions(int $containerWidth, int $containerHeight, int $layerWidth, int $layerHeight, int $layerPositionX, int $layerPositionY, string $position = 'LT'): array { $position = strtolower($position); - if ($position == 'rt') { + if ($position === 'rt') { $layerPositionX = $containerWidth - $layerWidth - $layerPositionX; - } elseif ($position == 'lb') { + } elseif ($position === 'lb') { $layerPositionY = $containerHeight - $layerHeight - $layerPositionY; - } elseif ($position == 'rb') { + } elseif ($position === 'rb') { $layerPositionX = $containerWidth - $layerWidth - $layerPositionX; $layerPositionY = $containerHeight - $layerHeight - $layerPositionY; - } elseif ($position == 'mm') { - $layerPositionX = (($containerWidth - $layerWidth) / 2) + $layerPositionX; - $layerPositionY = (($containerHeight - $layerHeight) / 2) + $layerPositionY; - } elseif ($position == 'mt') { - $layerPositionX = (($containerWidth - $layerWidth) / 2) + $layerPositionX; - } elseif ($position == 'mb') { - $layerPositionX = (($containerWidth - $layerWidth) / 2) + $layerPositionX; + } elseif ($position === 'mm') { + $layerPositionX = (int) ((($containerWidth - $layerWidth) / 2) + $layerPositionX); + $layerPositionY = (int) ((($containerHeight - $layerHeight) / 2) + $layerPositionY); + } elseif ($position === 'mt') { + $layerPositionX = (int) ((($containerWidth - $layerWidth) / 2) + $layerPositionX); + } elseif ($position === 'mb') { + $layerPositionX = (int) ((($containerWidth - $layerWidth) / 2) + $layerPositionX); $layerPositionY = $containerHeight - $layerHeight - $layerPositionY; - } elseif ($position == 'lm') { - $layerPositionY = (($containerHeight - $layerHeight) / 2) + $layerPositionY; - } elseif ($position == 'rm') { + } elseif ($position === 'lm') { + $layerPositionY = (int) ((($containerHeight - $layerHeight) / 2) + $layerPositionY); + } elseif ($position === 'rm') { $layerPositionX = $containerWidth - $layerWidth - $layerPositionX; - $layerPositionY = (($containerHeight - $layerHeight) / 2) + $layerPositionY; + $layerPositionY = (int) ((($containerHeight - $layerHeight) / 2) + $layerPositionY); } return array( @@ -70,11 +63,9 @@ public static function calculatePositions($containerWidth, $containerHeight, $la /** * Convert Hex color to RGB color format * - * @param string|null $hex - * - * @return array + * @return array{R: int, G: int, B: int} */ - public static function convertHexToRGB($hex) + public static function convertHexToRGB(?string $hex): array { return array( 'R' => (int) base_convert(substr($hex ?? '', 0, 2), 16, 10), @@ -84,16 +75,9 @@ public static function convertHexToRGB($hex) } /** - * Generate a new image resource var - * - * @param int $width - * @param int $height - * @param string $color - * @param int $opacity - * - * @return resource|object + * Generate a new image */ - public static function generateImage($width = 100, $height = 100, $color = 'ffffff', $opacity = 127) + public static function generateImage(int $width = 100, int $height = 100, string $color = 'ffffff', int $opacity = 127): GdImage { $RGBColors = ImageWorkshopLib::convertHexToRGB($color); @@ -108,14 +92,9 @@ public static function generateImage($width = 100, $height = 100, $color = 'ffff /** * Return dimension of a text * - * @param float $fontSize - * @param float $fontAngle - * @param string $fontFile - * @param string $text - * - * @return array|false + * @return array{left: int, top: int, width: int, height: int}|false */ - public static function getTextBoxDimension($fontSize, $fontAngle, $fontFile, $text) + public static function getTextBoxDimension(float $fontSize, float $fontAngle, string $fontFile, string $text): array|bool { if (!file_exists($fontFile)) { throw new ImageWorkshopLibException('Can\'t find a font file at this path : "'.$fontFile.'".', static::ERROR_FONT_NOT_FOUND); @@ -174,26 +153,9 @@ public static function getTextBoxDimension($fontSize, $fontAngle, $fontFile, $te /** * Copy an image on another one and converse transparency - * - * @param resource|object $destImg - * @param resource|object $srcImg - * @param int $destX - * @param int $destY - * @param int $srcX - * @param int $srcY - * @param int $srcW - * @param int $srcH - * @param int $pct */ - public static function imageCopyMergeAlpha(&$destImg, &$srcImg, $destX, $destY, $srcX, $srcY, $srcW, $srcH, $pct = 0) + public static function imageCopyMergeAlpha(GdImage $destImg, GdImage $srcImg, int $destX, int $destY, int $srcX, int $srcY, int $srcW, int $srcH, int $pct = 0): void { - $destX = (int) $destX; - $destY = (int) $destY; - $srcX = (int) $srcX; - $srcY = (int) $srcY; - $srcW = (int) $srcW; - $srcH = (int) $srcH; - $pct = (int) $pct; $destW = imageSX($destImg); $destH = imageSY($destImg); $alpha = 0; @@ -258,15 +220,8 @@ public static function imageCopyMergeAlpha(&$destImg, &$srcImg, $destX, $destY, /** * Merge two image var - * - * @param resource|object $destinationImage - * @param resource|object $sourceImage - * @param int $destinationPosX - * @param int $destinationPosY - * @param int $sourcePosX - * @param int $sourcePosY */ - public static function mergeTwoImages(&$destinationImage, $sourceImage, $destinationPosX = 0, $destinationPosY = 0, $sourcePosX = 0, $sourcePosY = 0) + public static function mergeTwoImages(GdImage $destinationImage, GdImage $sourceImage, int $destinationPosX = 0, int $destinationPosY = 0, int $sourcePosX = 0, int $sourcePosY = 0): void { imageCopy($destinationImage, $sourceImage, $destinationPosX, $destinationPosY, $sourcePosX, $sourcePosY, imageSX($sourceImage), imageSY($sourceImage)); } diff --git a/src/Exception/ImageWorkshopBaseException.php b/src/Exception/ImageWorkshopBaseException.php index 2bd42f6..a8e270a 100644 --- a/src/Exception/ImageWorkshopBaseException.php +++ b/src/Exception/ImageWorkshopBaseException.php @@ -12,27 +12,15 @@ * @license http://en.wikipedia.org/wiki/MIT_License * @copyright Clément Guillemain */ -class ImageWorkshopBaseException extends \Exception +class ImageWorkshopBaseException extends \Exception implements \Stringable { - /** - * Constructor - * - * @param string $message - * @param integer $code - * @param \Exception $previous - */ - public function __construct($message, $code = 0, \Exception $previous = null) + public function __construct(string $message, int $code = 0, \Exception $previous = null) { parent::__construct($message, $code, $previous); } - /** - * __toString method - * - * @return string - */ - public function __toString() + public function __toString(): string { - return __CLASS__.": [{$this->code}]: {$this->message}\n"; + return self::class.": [{$this->code}]: {$this->message}\n"; } } diff --git a/src/Exception/ImageWorkshopException.php b/src/Exception/ImageWorkshopException.php index 241318d..1a418f0 100644 --- a/src/Exception/ImageWorkshopException.php +++ b/src/Exception/ImageWorkshopException.php @@ -16,7 +16,7 @@ */ class ImageWorkshopException extends ImageWorkshopBaseException { - public static function invalidUnitArgument() + public static function invalidUnitArgument(): self { return new self("Invalid unit value: should be ImageWorkshopLayer::UNIT_PIXEL or ImageWorkshopLayer::UNIT_PERCENT"); } diff --git a/src/Exif/ExifOrientations.php b/src/Exif/ExifOrientations.php index 94fa5df..087b7f7 100644 --- a/src/Exif/ExifOrientations.php +++ b/src/Exif/ExifOrientations.php @@ -7,13 +7,13 @@ */ final class ExifOrientations { - const UNDEFINED = 0; - const TOP_LEFT = 1; - const TOP_RIGHT = 2; - const BOTTOM_RIGHT = 3; - const BOTTOM_LEFT = 4; - const LEFT_TOP = 5; - const RIGHT_TOP = 6; - const RIGHT_BOTTOM = 7; - const LEFT_BOTTOM = 8; + public const UNDEFINED = 0; + public const TOP_LEFT = 1; + public const TOP_RIGHT = 2; + public const BOTTOM_RIGHT = 3; + public const BOTTOM_LEFT = 4; + public const LEFT_TOP = 5; + public const RIGHT_TOP = 6; + public const RIGHT_BOTTOM = 7; + public const LEFT_BOTTOM = 8; } diff --git a/src/ImageWorkshop.php b/src/ImageWorkshop.php index 8bbea5d..f6d954c 100644 --- a/src/ImageWorkshop.php +++ b/src/ImageWorkshop.php @@ -2,8 +2,9 @@ namespace PHPImageWorkshop; -use PHPImageWorkshop\Core\ImageWorkshopLayer as ImageWorkshopLayer; -use PHPImageWorkshop\Core\ImageWorkshopLib as ImageWorkshopLib; +use GdImage; +use PHPImageWorkshop\Core\ImageWorkshopLayer; +use PHPImageWorkshop\Core\ImageWorkshopLib; use PHPImageWorkshop\Exception\ImageWorkshopException; /** @@ -19,38 +20,33 @@ class ImageWorkshop { /** - * @var integer + * @var int */ - const ERROR_NOT_AN_IMAGE_FILE = 1; + public const ERROR_NOT_AN_IMAGE_FILE = 1; /** - * @var integer + * @var int */ - const ERROR_IMAGE_NOT_FOUND = 2; + public const ERROR_IMAGE_NOT_FOUND = 2; /** - * @var integer + * @var int */ - const ERROR_NOT_READABLE_FILE = 3; + public const ERROR_NOT_READABLE_FILE = 3; /** - * @var integer + * @var int */ - const ERROR_CREATE_IMAGE_FROM_STRING = 4; + public const ERROR_CREATE_IMAGE_FROM_STRING = 4; /** * Initialize a layer from a given image path * * From an upload form, you can give the "tmp_name" path * - * @param string $path - * @param bool $fixOrientation - * - * @return ImageWorkshopLayer - * * @throws ImageWorkshopException */ - public static function initFromPath($path, $fixOrientation = false) + public static function initFromPath(string $path, bool $fixOrientation = false): ImageWorkshopLayer { if (false === filter_var($path, FILTER_VALIDATE_URL) && !file_exists($path)) { throw new ImageWorkshopException(sprintf('File "%s" not exists.', $path), static::ERROR_IMAGE_NOT_FOUND); @@ -62,7 +58,7 @@ public static function initFromPath($path, $fixOrientation = false) $mimeContentType = explode('/', $imageSizeInfos['mime']); if (!isset($mimeContentType[1])) { - $givenType = isset($mimeContentType[1]) ? $mimeContentType[1] : 'none'; + $givenType = $mimeContentType[1] ?? 'none'; throw new ImageWorkshopException('Not an image file (jpeg/png/gif) at "'.$path.'" (given format: "'.$givenType.'")', static::ERROR_NOT_AN_IMAGE_FILE); } @@ -87,7 +83,7 @@ public static function initFromPath($path, $fixOrientation = false) break; case 'webp': - $image = imagecreatefromwebp($path); + $image = imageCreateFromWebp($path); break; default: @@ -109,17 +105,8 @@ public static function initFromPath($path, $fixOrientation = false) /** * Initialize a text layer - * - * @param string $text - * @param string $fontPath - * @param int $fontSize - * @param string $fontColor - * @param int $textRotation - * @param string $backgroundColor - * - * @return ImageWorkshopLayer */ - public static function initTextLayer($text, $fontPath, $fontSize = 13, $fontColor = 'ffffff', $textRotation = 0, $backgroundColor = null) + public static function initTextLayer(string $text, string $fontPath, int $fontSize = 13, string $fontColor = 'ffffff', int $textRotation = 0, string $backgroundColor = null): ImageWorkshopLayer { $textDimensions = ImageWorkshopLib::getTextBoxDimension($fontSize, $textRotation, $fontPath, $text); @@ -131,18 +118,12 @@ public static function initTextLayer($text, $fontPath, $fontSize = 13, $fontColo /** * Initialize a new virgin layer - * - * @param int $width - * @param int $height - * @param string $backgroundColor - * - * @return ImageWorkshopLayer */ - public static function initVirginLayer($width = 100, $height = 100, $backgroundColor = null) + public static function initVirginLayer(int $width = 100, int $height = 100, string $backgroundColor = null): ImageWorkshopLayer { $opacity = 0; - if (null === $backgroundColor || $backgroundColor == 'transparent') { + if (null === $backgroundColor || $backgroundColor === 'transparent') { $opacity = 127; $backgroundColor = 'ffffff'; } @@ -152,12 +133,8 @@ public static function initVirginLayer($width = 100, $height = 100, $backgroundC /** * Initialize a layer from a resource image var - * - * @param resource $image - * - * @return ImageWorkshopLayer */ - public static function initFromResourceVar($image) + public static function initFromResourceVar(GdImage $image): ImageWorkshopLayer { return new ImageWorkshopLayer($image); } @@ -165,13 +142,9 @@ public static function initFromResourceVar($image) /** * Initialize a layer from a string (obtains with file_get_contents, cURL...) * - * This not recommanded to initialize JPEG string with this method, GD displays bugs ! - * - * @param string $imageString - * - * @return ImageWorkshopLayer + * This not recommended to initialize JPEG string with this method, GD displays bugs ! */ - public static function initFromString($imageString) + public static function initFromString(string $imageString): ImageWorkshopLayer { if (!$image = @imageCreateFromString($imageString)) { throw new ImageWorkshopException('Can\'t generate an image from the given string.', static::ERROR_CREATE_IMAGE_FROM_STRING); diff --git a/tests/Core/ImageWorkshopLayerTest.php b/tests/Core/ImageWorkshopLayerTest.php index 3e66b49..2a0c282 100644 --- a/tests/Core/ImageWorkshopLayerTest.php +++ b/tests/Core/ImageWorkshopLayerTest.php @@ -805,13 +805,13 @@ public function testResizeInPixel() $layer = $this->initializeLayer(1); - $layer->resizeInPixel(20, 10, null); + $layer->resizeInPixel(20, 10, false); $this->assertTrue($layer->getWidth() == 20, 'Expect $layer to have a width of 20px'); $this->assertTrue($layer->getHeight() == 10, 'Expect $layer to have a height of 10px'); $layer = $this->initializeLayer(1); - $layer->resizeInPixel(20, null, null); + $layer->resizeInPixel(20, null, false); $this->assertTrue($layer->getWidth() == 20, 'Expect $layer to have a width of 20px'); $this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px'); @@ -823,7 +823,7 @@ public function testResizeInPixel() $layer = $this->initializeLayer(1); - $layer->resizeInPixel(null, 20, null); + $layer->resizeInPixel(null, 20, false); $this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px'); $this->assertTrue($layer->getHeight() == 20, 'Expect $layer to have a height of 20px'); @@ -835,19 +835,19 @@ public function testResizeInPixel() $layer = $this->initializeLayer(1); - $layer->resizeInPixel(null, null, null); + $layer->resizeInPixel(null, null, false); $this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px'); $this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px'); $layer = $this->initializeLayer(1); - $layer->resizeInPixel(0, 0, null); + $layer->resizeInPixel(0, 0, false); $this->assertTrue($layer->getWidth() == 1, 'Expect $layer to have a width of 1px'); $this->assertTrue($layer->getHeight() == 1, 'Expect $layer to have a height of 1px'); $layer = $this->initializeLayer(1); - $layer->resizeInPixel(-1, -1, null); + $layer->resizeInPixel(-1, -1, false); $this->assertTrue($layer->getWidth() == 1, 'Expect $layer to have a width of 1px'); $this->assertTrue($layer->getHeight() == 1, 'Expect $layer to have a height of 1px'); } @@ -883,13 +883,13 @@ public function testResizeInPercent() $layer = $this->initializeLayer(1); - $layer->resizeInPercent(20, 10, null); + $layer->resizeInPercent(20, 10, false); $this->assertTrue($layer->getWidth() == 20, 'Expect $layer to have a width of 20px'); $this->assertTrue($layer->getHeight() == 8, 'Expect $layer to have a height of 8px'); $layer = $this->initializeLayer(1); - $layer->resizeInPercent(20, null, null); + $layer->resizeInPercent(20, null, false); $this->assertTrue($layer->getWidth() == 20, 'Expect $layer to have a width of 20px'); $this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px'); @@ -901,7 +901,7 @@ public function testResizeInPercent() $layer = $this->initializeLayer(1); - $layer->resizeInPercent(null, 20, null); + $layer->resizeInPercent(null, 20, false); $this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px'); $this->assertTrue($layer->getHeight() == 15, 'Expect $layer to have a height of 15px'); @@ -913,19 +913,19 @@ public function testResizeInPercent() $layer = $this->initializeLayer(1); - $layer->resizeInPercent(null, null, null); + $layer->resizeInPercent(null, null, false); $this->assertTrue($layer->getWidth() == 100, 'Expect $layer to have a width of 100px'); $this->assertTrue($layer->getHeight() == 75, 'Expect $layer to have a height of 75px'); $layer = $this->initializeLayer(1); - $layer->resizeInPercent(0, 0, null); + $layer->resizeInPercent(0, 0, false); $this->assertTrue($layer->getWidth() == 1, 'Expect $layer to have a width of 1px'); $this->assertTrue($layer->getHeight() == 1, 'Expect $layer to have a height of 1px'); $layer = $this->initializeLayer(1); - $layer->resizeInPercent(-1, -1, null); + $layer->resizeInPercent(-1, -1, false); $this->assertTrue($layer->getWidth() == 1, 'Expect $layer to have a width of 1px'); $this->assertTrue($layer->getHeight() == 1, 'Expect $layer to have a height of 1px'); } From 108e863bb47f8661850f037e4cfcce479f94ae79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sat, 14 May 2022 14:37:57 +0200 Subject: [PATCH 4/7] Skip some rotate tests is not needed anymore --- tests/Core/ImageWorkshopLayerTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/Core/ImageWorkshopLayerTest.php b/tests/Core/ImageWorkshopLayerTest.php index 2a0c282..6bfeffd 100644 --- a/tests/Core/ImageWorkshopLayerTest.php +++ b/tests/Core/ImageWorkshopLayerTest.php @@ -1425,12 +1425,6 @@ public function testRotate() $layer = $this->initializeLayer(1); - - if (version_compare(PHP_VERSION, '5.5', '>=')) { - // see https://bugs.php.net/bug.php?id=65148 - $this->markTestIncomplete('Disabling some tests while bug #65148 is open'); - } - $layer->rotate(40); $this->assertTrue($layer->getWidth() <= 126 && $layer->getWidth() >= 124, 'Expect $layer to have a width around 125px'); $this->assertTrue($layer->getHeight() <= 124 && $layer->getHeight() >= 122, 'Expect $layer to have a height around 123px'); From d0570b744ad3f264b9ff2505207b2b7b4166e4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sat, 14 May 2022 14:41:09 +0200 Subject: [PATCH 5/7] Remove PHPStan false positive --- phpstan.neon | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index ada3bfc..5764e21 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,3 +2,8 @@ parameters: level: 5 bootstrapFiles: - %rootDir%/../../../vendor/autoload.php + + ignoreErrors: + - + message: '#Left side of && is always true#' + path: src/Core/ImageWorkshopLayer.php From b008b5323e1f4771c9dae338a34e9d2491f9471c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sat, 14 May 2022 14:46:02 +0200 Subject: [PATCH 6/7] Remove unmaintained PHP version from CI --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 40c2e99..3c38cb8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - php-version: ['7.2', '7.3', '7.4', '8.0', '8.1'] + php-version: ['8.0', '8.1'] name: 'PHPUnit - PHP/${{ matrix.php-version }} - OS/${{ matrix.os }}' steps: - name: Checkout From 46d1cdf95ee40a927122e9951b51e58a2832421f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20DECOOL?= Date: Sun, 15 May 2022 09:38:45 +0200 Subject: [PATCH 7/7] Update README --- README.md | 76 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 2c6e833..692c3c6 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,40 @@ -# ================================ -# ImageWorkshop class -# ================================ - -[![Test status](https://secure.travis-ci.org/Sybio/ImageWorkshop.png?branch=master)](https://travis-ci.org/Sybio/ImageWorkshop) -[![Latest Stable Version](https://poser.pugx.org/sybio/image-workshop/v/stable)](https://packagist.org/packages/sybio/image-workshop) -[![Total Downloads](https://poser.pugx.org/sybio/image-workshop/downloads)](https://packagist.org/packages/sybio/image-workshop) -[![Monthly Downloads](https://poser.pugx.org/sybio/image-workshop/d/monthly)](https://packagist.org/packages/sybio/image-workshop) -[![License](https://poser.pugx.org/sybio/image-workshop/license)](https://packagist.org/packages/sybio/image-workshop) - -### Summary and features -Really flexible and easy-to-use PHP class to work with images using the GD Library - -http://phpimageworkshop.com/ - -### Installation - -The class is designed for PHP 5.3+, but it can work with older PHP versions... Check how to install the class here: http://phpimageworkshop.com/installation.html - -### Usage - -- Learn how to use the class in 5 minutes: http://phpimageworkshop.com/quickstart.html -- The complete documentation: http://phpimageworkshop.com/documentation.html -- Usefull tutorials: http://phpimageworkshop.com/tutorials.html -- Changelog: [CHANGELOG.md](CHANGELOG.md) - -**What's new in the doc' ?** - -- Installation guide: http://phpimageworkshop.com/installation.html -- Adding the flip documentation: http://phpimageworkshop.com/doc/25/flip-vertical-horizontal-mirror.html -- Adding the opacity documentation which was omitted: http://phpimageworkshop.com/doc/24/opacity-transparency.html -- Tutorial "Manage animated GIF with ImageWorkshop (and GiFFrameExtractor & GifCreator)": http://phpimageworkshop.com/tutorial/5/manage-animated-gif-with-imageworkshop.html - -### @todo -- Adding a method to add easily borders to a layer (external, inside and middle border) -- Check given hexa' color and remove # if exists. +# ================================ +# ImageWorkshop class +# ================================ + +[![Test status](https://secure.travis-ci.org/Sybio/ImageWorkshop.png?branch=master)](https://travis-ci.org/Sybio/ImageWorkshop) +[![Latest Stable Version](https://poser.pugx.org/sybio/image-workshop/v/stable)](https://packagist.org/packages/sybio/image-workshop) +[![Total Downloads](https://poser.pugx.org/sybio/image-workshop/downloads)](https://packagist.org/packages/sybio/image-workshop) +[![Monthly Downloads](https://poser.pugx.org/sybio/image-workshop/d/monthly)](https://packagist.org/packages/sybio/image-workshop) +[![License](https://poser.pugx.org/sybio/image-workshop/license)](https://packagist.org/packages/sybio/image-workshop) + +### Summary and features +Really flexible and easy-to-use PHP class to work with images using the GD Library + +http://phpimageworkshop.com/ + +Current `master` branch correspond to the next major release (v3) which only support PHP 8.0+. + +### Installation + +The class is designed for PHP 8.0+... Check how to install the class here: http://phpimageworkshop.com/installation.html + +For older PHP versions support, install the [2.x](https://github.com/Sybio/ImageWorkshop/tree/2.x) version branch. + +### Usage + +- Learn how to use the class in 5 minutes: http://phpimageworkshop.com/quickstart.html +- The complete documentation: http://phpimageworkshop.com/documentation.html +- Usefull tutorials: http://phpimageworkshop.com/tutorials.html +- Changelog: [CHANGELOG.md](CHANGELOG.md) + +**What's new in the doc' ?** + +- Installation guide: http://phpimageworkshop.com/installation.html +- Adding the flip documentation: http://phpimageworkshop.com/doc/25/flip-vertical-horizontal-mirror.html +- Adding the opacity documentation which was omitted: http://phpimageworkshop.com/doc/24/opacity-transparency.html +- Tutorial "Manage animated GIF with ImageWorkshop (and GiFFrameExtractor & GifCreator)": http://phpimageworkshop.com/tutorial/5/manage-animated-gif-with-imageworkshop.html + +### @todo +- Adding a method to add easily borders to a layer (external, inside and middle border) +- Check given hexa' color and remove # if exists.