updating jdenticon library to 2.0.0, minimum PHP version 7.4

This commit is contained in:
El RIDO 2025-07-14 22:01:11 +02:00
parent 3ba29ea29e
commit 13869e46be
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92
47 changed files with 764 additions and 912 deletions

View file

@ -22,7 +22,7 @@ jobs:
continue-on-error: "${{ matrix.experimental }}" continue-on-error: "${{ matrix.experimental }}"
strategy: strategy:
matrix: matrix:
php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
experimental: [false] experimental: [false]
# uncomment this to start testing on development release # uncomment this to start testing on development release
# include: # include:

View file

@ -3,6 +3,8 @@
## 2.0.0 (not yet released) ## 2.0.0 (not yet released)
* CHANGED: Remove page template (#265) * CHANGED: Remove page template (#265)
* CHANGED: Jdenticons are now used as the default icons * CHANGED: Jdenticons are now used as the default icons
* CHANGED: Upgrading libraries to: jdenticon 2.0.0
* CHANGED: Minimum required PHP version is 7.4, due to a change in the jdenticon library
* FIXED: Name mismatches in attached files (#1584) * FIXED: Name mismatches in attached files (#1584)
* FIXED: Unable to paste attachments from clipboard (#1589) * FIXED: Unable to paste attachments from clipboard (#1589)

View file

@ -24,8 +24,8 @@
"docs" : "https://privatebin.info/codedoc/" "docs" : "https://privatebin.info/codedoc/"
}, },
"require" : { "require" : {
"php": "^7.3 || ^8.0", "php": "^7.4 || ^8.0",
"jdenticon/jdenticon": "1.0.2", "jdenticon/jdenticon": "2.0.0",
"mlocati/ip-lib": "1.20.0", "mlocati/ip-lib": "1.20.0",
"symfony/polyfill-php80": "1.31.0", "symfony/polyfill-php80": "1.31.0",
"yzalis/identicon": "2.0.0" "yzalis/identicon": "2.0.0"
@ -48,7 +48,7 @@
"preferred-install": "dist", "preferred-install": "dist",
"sort-packages": true, "sort-packages": true,
"platform": { "platform": {
"php": "7.3" "php": "7.4"
} }
} }
} }

74
composer.lock generated
View file

@ -4,27 +4,27 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "6c7e6dea19e8bfd5641b220cb68c4b65", "content-hash": "cc778a671eac2ba1ec70bf9398b2e1bf",
"packages": [ "packages": [
{ {
"name": "jdenticon/jdenticon", "name": "jdenticon/jdenticon",
"version": "1.0.2", "version": "2.0.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/dmester/jdenticon-php.git", "url": "https://github.com/dmester/jdenticon-php.git",
"reference": "cabb7a44c413c318392a341c5d3ca30fcdd57a6f" "reference": "fb39a98a0a54982a130b7e7b06305d4fd8c9717e"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/dmester/jdenticon-php/zipball/cabb7a44c413c318392a341c5d3ca30fcdd57a6f", "url": "https://api.github.com/repos/dmester/jdenticon-php/zipball/fb39a98a0a54982a130b7e7b06305d4fd8c9717e",
"reference": "cabb7a44c413c318392a341c5d3ca30fcdd57a6f", "reference": "fb39a98a0a54982a130b7e7b06305d4fd8c9717e",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=5.3.0" "php": ">=7.4.0"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^5.7" "phpunit/phpunit": "^9"
}, },
"type": "library", "type": "library",
"autoload": { "autoload": {
@ -53,7 +53,7 @@
"issues": "https://github.com/dmester/jdenticon-php/issues", "issues": "https://github.com/dmester/jdenticon-php/issues",
"source": "https://github.com/dmester/jdenticon-php" "source": "https://github.com/dmester/jdenticon-php"
}, },
"time": "2022-10-30T17:15:02+00:00" "time": "2025-07-14T18:30:29+00:00"
}, },
{ {
"name": "mlocati/ip-lib", "name": "mlocati/ip-lib",
@ -337,16 +337,16 @@
}, },
{ {
"name": "myclabs/deep-copy", "name": "myclabs/deep-copy",
"version": "1.13.0", "version": "1.13.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/myclabs/DeepCopy.git", "url": "https://github.com/myclabs/DeepCopy.git",
"reference": "024473a478be9df5fdaca2c793f2232fe788e414" "reference": "faed855a7b5f4d4637717c2b3863e277116beb36"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/024473a478be9df5fdaca2c793f2232fe788e414", "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/faed855a7b5f4d4637717c2b3863e277116beb36",
"reference": "024473a478be9df5fdaca2c793f2232fe788e414", "reference": "faed855a7b5f4d4637717c2b3863e277116beb36",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -385,7 +385,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/myclabs/DeepCopy/issues", "issues": "https://github.com/myclabs/DeepCopy/issues",
"source": "https://github.com/myclabs/DeepCopy/tree/1.13.0" "source": "https://github.com/myclabs/DeepCopy/tree/1.13.3"
}, },
"funding": [ "funding": [
{ {
@ -393,29 +393,31 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2025-02-12T12:17:51+00:00" "time": "2025-07-05T12:25:42+00:00"
}, },
{ {
"name": "nikic/php-parser", "name": "nikic/php-parser",
"version": "v4.19.4", "version": "v5.5.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/nikic/PHP-Parser.git", "url": "https://github.com/nikic/PHP-Parser.git",
"reference": "715f4d25e225bc47b293a8b997fe6ce99bf987d2" "reference": "ae59794362fe85e051a58ad36b289443f57be7a9"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/715f4d25e225bc47b293a8b997fe6ce99bf987d2", "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/ae59794362fe85e051a58ad36b289443f57be7a9",
"reference": "715f4d25e225bc47b293a8b997fe6ce99bf987d2", "reference": "ae59794362fe85e051a58ad36b289443f57be7a9",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-ctype": "*",
"ext-json": "*",
"ext-tokenizer": "*", "ext-tokenizer": "*",
"php": ">=7.1" "php": ">=7.4"
}, },
"require-dev": { "require-dev": {
"ircmaxell/php-yacc": "^0.0.7", "ircmaxell/php-yacc": "^0.0.7",
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" "phpunit/phpunit": "^9.0"
}, },
"bin": [ "bin": [
"bin/php-parse" "bin/php-parse"
@ -423,7 +425,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "4.9-dev" "dev-master": "5.0-dev"
} }
}, },
"autoload": { "autoload": {
@ -447,9 +449,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/nikic/PHP-Parser/issues", "issues": "https://github.com/nikic/PHP-Parser/issues",
"source": "https://github.com/nikic/PHP-Parser/tree/v4.19.4" "source": "https://github.com/nikic/PHP-Parser/tree/v5.5.0"
}, },
"time": "2024-09-29T15:01:53+00:00" "time": "2025-05-31T08:24:38+00:00"
}, },
{ {
"name": "phar-io/manifest", "name": "phar-io/manifest",
@ -890,16 +892,16 @@
}, },
{ {
"name": "phpunit/phpunit", "name": "phpunit/phpunit",
"version": "9.6.22", "version": "9.6.23",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git", "url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "f80235cb4d3caa59ae09be3adf1ded27521d1a9c" "reference": "43d2cb18d0675c38bd44982a5d1d88f6d53d8d95"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f80235cb4d3caa59ae09be3adf1ded27521d1a9c", "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/43d2cb18d0675c38bd44982a5d1d88f6d53d8d95",
"reference": "f80235cb4d3caa59ae09be3adf1ded27521d1a9c", "reference": "43d2cb18d0675c38bd44982a5d1d88f6d53d8d95",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -910,7 +912,7 @@
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-xml": "*", "ext-xml": "*",
"ext-xmlwriter": "*", "ext-xmlwriter": "*",
"myclabs/deep-copy": "^1.12.1", "myclabs/deep-copy": "^1.13.1",
"phar-io/manifest": "^2.0.4", "phar-io/manifest": "^2.0.4",
"phar-io/version": "^3.2.1", "phar-io/version": "^3.2.1",
"php": ">=7.3", "php": ">=7.3",
@ -973,7 +975,7 @@
"support": { "support": {
"issues": "https://github.com/sebastianbergmann/phpunit/issues", "issues": "https://github.com/sebastianbergmann/phpunit/issues",
"security": "https://github.com/sebastianbergmann/phpunit/security/policy", "security": "https://github.com/sebastianbergmann/phpunit/security/policy",
"source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.22" "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.23"
}, },
"funding": [ "funding": [
{ {
@ -984,12 +986,20 @@
"url": "https://github.com/sebastianbergmann", "url": "https://github.com/sebastianbergmann",
"type": "github" "type": "github"
}, },
{
"url": "https://liberapay.com/sebastianbergmann",
"type": "liberapay"
},
{
"url": "https://thanks.dev/u/gh/sebastianbergmann",
"type": "thanks_dev"
},
{ {
"url": "https://tidelift.com/funding/github/packagist/phpunit/phpunit", "url": "https://tidelift.com/funding/github/packagist/phpunit/phpunit",
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2024-12-05T13:48:26+00:00" "time": "2025-05-02T06:40:34+00:00"
}, },
{ {
"name": "sebastian/cli-parser", "name": "sebastian/cli-parser",
@ -2011,11 +2021,11 @@
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": "^7.3 || ^8.0" "php": "^7.4 || ^8.0"
}, },
"platform-dev": [], "platform-dev": [],
"platform-overrides": { "platform-overrides": {
"php": "7.3" "php": "7.4"
}, },
"plugin-api-version": "2.3.0" "plugin-api-version": "2.3.0"
} }

View file

@ -21,7 +21,7 @@ for more information.
### Minimal Requirements ### Minimal Requirements
- PHP version 7.3 or above - PHP version 7.4 or above
- GD extension (when using identicon or vizhash icons, jdenticon works without it) - GD extension (when using identicon or vizhash icons, jdenticon works without it)
- zlib extension - zlib extension
- some disk space or a database supported by [PDO](https://php.net/manual/book.pdo.php) - some disk space or a database supported by [PDO](https://php.net/manual/book.pdo.php)

View file

@ -118,7 +118,7 @@ class Configuration
'js/kjua-0.9.0.js' => 'sha512-CVn7af+vTMBd9RjoS4QM5fpLFEOtBCoB0zPtaqIDC7sF4F8qgUSRFQQpIyEDGsr6yrjbuOLzdf20tkHHmpaqwQ==', 'js/kjua-0.9.0.js' => 'sha512-CVn7af+vTMBd9RjoS4QM5fpLFEOtBCoB0zPtaqIDC7sF4F8qgUSRFQQpIyEDGsr6yrjbuOLzdf20tkHHmpaqwQ==',
'js/legacy.js' => 'sha512-UxW/TOZKon83n6dk/09GsYKIyeO5LeBHokxyIq+r7KFS5KMBeIB/EM7NrkVYIezwZBaovnyNtY2d9tKFicRlXg==', 'js/legacy.js' => 'sha512-UxW/TOZKon83n6dk/09GsYKIyeO5LeBHokxyIq+r7KFS5KMBeIB/EM7NrkVYIezwZBaovnyNtY2d9tKFicRlXg==',
'js/prettify.js' => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==', 'js/prettify.js' => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
'js/privatebin.js' => 'sha512-ErR4jczmxS4WnJ878GLIFYVmVzfDD9BYrdwBM3oUe86kanUD7DfS+fYW23Jg4OHKgrmmJkpBRus9ylVoWqhrFw==', 'js/privatebin.js' => 'sha512-mPPCBJRbTT7LCOhjv2xL01yhJqwKOgwalktUuQVgsno14vXAs8iAr7qQ6aYh9jUqTarUoXpZJSqxijpziSzhuA==',
'js/purify-3.2.6.js' => 'sha512-zqwL4OoBLFx89QPewkz4Lz5CSA2ktU+f31fuECkF0iK3Id5qd3Zpq5dMby8KwHjIEpsUgOqwF58cnmcaNem0EA==', 'js/purify-3.2.6.js' => 'sha512-zqwL4OoBLFx89QPewkz4Lz5CSA2ktU+f31fuECkF0iK3Id5qd3Zpq5dMby8KwHjIEpsUgOqwF58cnmcaNem0EA==',
'js/rawinflate-0.3.js' => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==', 'js/rawinflate-0.3.js' => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==',
'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==', 'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',

View file

@ -34,7 +34,7 @@ class Controller
* *
* @const string * @const string
*/ */
const MIN_PHP_VERSION = '7.3.0'; const MIN_PHP_VERSION = '7.4.0';
/** /**
* show the same error message if the paste expired or does not exist * show the same error message if the paste expired or does not exist

View file

@ -65,7 +65,6 @@ return array(
'Jdenticon\\Shapes\\Shape' => $vendorDir . '/jdenticon/jdenticon/src/Shapes/Shape.php', 'Jdenticon\\Shapes\\Shape' => $vendorDir . '/jdenticon/jdenticon/src/Shapes/Shape.php',
'Jdenticon\\Shapes\\ShapeCategory' => $vendorDir . '/jdenticon/jdenticon/src/Shapes/ShapeCategory.php', 'Jdenticon\\Shapes\\ShapeCategory' => $vendorDir . '/jdenticon/jdenticon/src/Shapes/ShapeCategory.php',
'Jdenticon\\Shapes\\ShapeDefinitions' => $vendorDir . '/jdenticon/jdenticon/src/Shapes/ShapeDefinitions.php', 'Jdenticon\\Shapes\\ShapeDefinitions' => $vendorDir . '/jdenticon/jdenticon/src/Shapes/ShapeDefinitions.php',
'Jdenticon\\Shapes\\ShapePosition' => $vendorDir . '/jdenticon/jdenticon/src/Shapes/ShapePosition.php',
'PhpToken' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/PhpToken.php', 'PhpToken' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/PhpToken.php',
'PrivateBin\\Configuration' => $baseDir . '/lib/Configuration.php', 'PrivateBin\\Configuration' => $baseDir . '/lib/Configuration.php',
'PrivateBin\\Controller' => $baseDir . '/lib/Controller.php', 'PrivateBin\\Controller' => $baseDir . '/lib/Controller.php',

View file

@ -113,7 +113,6 @@ class ComposerStaticInitDontChange
'Jdenticon\\Shapes\\Shape' => __DIR__ . '/..' . '/jdenticon/jdenticon/src/Shapes/Shape.php', 'Jdenticon\\Shapes\\Shape' => __DIR__ . '/..' . '/jdenticon/jdenticon/src/Shapes/Shape.php',
'Jdenticon\\Shapes\\ShapeCategory' => __DIR__ . '/..' . '/jdenticon/jdenticon/src/Shapes/ShapeCategory.php', 'Jdenticon\\Shapes\\ShapeCategory' => __DIR__ . '/..' . '/jdenticon/jdenticon/src/Shapes/ShapeCategory.php',
'Jdenticon\\Shapes\\ShapeDefinitions' => __DIR__ . '/..' . '/jdenticon/jdenticon/src/Shapes/ShapeDefinitions.php', 'Jdenticon\\Shapes\\ShapeDefinitions' => __DIR__ . '/..' . '/jdenticon/jdenticon/src/Shapes/ShapeDefinitions.php',
'Jdenticon\\Shapes\\ShapePosition' => __DIR__ . '/..' . '/jdenticon/jdenticon/src/Shapes/ShapePosition.php',
'PhpToken' => __DIR__ . '/..' . '/symfony/polyfill-php80/Resources/stubs/PhpToken.php', 'PhpToken' => __DIR__ . '/..' . '/symfony/polyfill-php80/Resources/stubs/PhpToken.php',
'PrivateBin\\Configuration' => __DIR__ . '/../..' . '/lib/Configuration.php', 'PrivateBin\\Configuration' => __DIR__ . '/../..' . '/lib/Configuration.php',
'PrivateBin\\Controller' => __DIR__ . '/../..' . '/lib/Controller.php', 'PrivateBin\\Controller' => __DIR__ . '/../..' . '/lib/Controller.php',

View file

@ -3,7 +3,7 @@
'name' => 'privatebin/privatebin', 'name' => 'privatebin/privatebin',
'pretty_version' => 'dev-master', 'pretty_version' => 'dev-master',
'version' => 'dev-master', 'version' => 'dev-master',
'reference' => '7825471d70c39baf6042c52a453c8fe705d9ed75', 'reference' => '3ba29ea29e04d8a16d64e0f49994ba416c1b008f',
'type' => 'project', 'type' => 'project',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
@ -11,9 +11,9 @@
), ),
'versions' => array( 'versions' => array(
'jdenticon/jdenticon' => array( 'jdenticon/jdenticon' => array(
'pretty_version' => '1.0.2', 'pretty_version' => '2.0.0',
'version' => '1.0.2.0', 'version' => '2.0.0.0',
'reference' => 'cabb7a44c413c318392a341c5d3ca30fcdd57a6f', 'reference' => 'fb39a98a0a54982a130b7e7b06305d4fd8c9717e',
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../jdenticon/jdenticon', 'install_path' => __DIR__ . '/../jdenticon/jdenticon',
'aliases' => array(), 'aliases' => array(),
@ -31,7 +31,7 @@
'privatebin/privatebin' => array( 'privatebin/privatebin' => array(
'pretty_version' => 'dev-master', 'pretty_version' => 'dev-master',
'version' => 'dev-master', 'version' => 'dev-master',
'reference' => '7825471d70c39baf6042c52a453c8fe705d9ed75', 'reference' => '3ba29ea29e04d8a16d64e0f49994ba416c1b008f',
'type' => 'project', 'type' => 'project',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),

View file

@ -4,8 +4,8 @@
$issues = array(); $issues = array();
if (!(PHP_VERSION_ID >= 70300)) { if (!(PHP_VERSION_ID >= 70400)) {
$issues[] = 'Your Composer dependencies require a PHP version ">= 7.3.0". You are running ' . PHP_VERSION . '.'; $issues[] = 'Your Composer dependencies require a PHP version ">= 7.4.0". You are running ' . PHP_VERSION . '.';
} }
if ($issues) { if ($issues) {

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -11,7 +11,6 @@
namespace Jdenticon\Canvas; namespace Jdenticon\Canvas;
use Jdenticon\Canvas\Rasterization\Edge;
use Jdenticon\Canvas\Rasterization\EdgeTable; use Jdenticon\Canvas\Rasterization\EdgeTable;
use Jdenticon\Canvas\Rasterization\Rasterizer; use Jdenticon\Canvas\Rasterization\Rasterizer;
use Jdenticon\Canvas\Png\PngPalette; use Jdenticon\Canvas\Png\PngPalette;
@ -21,15 +20,15 @@ use Jdenticon\Canvas\ColorUtils;
class Canvas class Canvas
{ {
private $edges; private EdgeTable $edges;
/** /**
* Creates a new canvas with the specified dimensions given in pixels. * Creates a new canvas with the specified dimensions given in pixels.
* *
* @param integer $width Canvas width in pixels. * @param int $width Canvas width in pixels.
* @param integer $height Canvas height in pixels. * @param int $height Canvas height in pixels.
*/ */
public function __construct($width, $height) public function __construct(int $width, int $height)
{ {
$this->width = $width; $this->width = $width;
$this->height = $height; $this->height = $height;
@ -38,17 +37,13 @@ class Canvas
/** /**
* The width of the canvas in pixels. * The width of the canvas in pixels.
*
* @var integer
*/ */
public $width = 0; public int $width = 0;
/** /**
* The height of the canvas in pixels. * The height of the canvas in pixels.
*
* @var integer
*/ */
public $height = 0; public int $height = 0;
/** /**
* Specifies the background color. Allowed values are: * Specifies the background color. Allowed values are:
@ -57,16 +52,16 @@ class Canvas
* - strings on the format #RRGGBB * - strings on the format #RRGGBB
* - strings on the format #RRGGBBAA * - strings on the format #RRGGBBAA
* *
* @var integer|string * @var int|string
*/ */
public $backColor = 0x00000000; public $backColor = 0x00000000;
/** /**
* Gets a context used to draw polygons on this canvas. * Gets a context used to draw polygons on this canvas.
* *
* @returns \Jdenticon\Canvas\CanvasContext * @return \Jdenticon\Canvas\CanvasContext
*/ */
public function getContext() public function getContext(): CanvasContext
{ {
return new CanvasContext($this, $this->edges); return new CanvasContext($this, $this->edges);
} }
@ -74,13 +69,13 @@ class Canvas
/** /**
* Renders the canvas as a PNG data stream. * Renders the canvas as a PNG data stream.
* *
* @param array $keywords Keywords to be written to the PNG stream. * @param array<string, string> $keywords Keywords to be written to the PNG stream.
* See https://www.w3.org/TR/PNG/#11keywords. * See https://www.w3.org/TR/PNG/#11keywords.
* @returns string * @return string
*/ */
public function toPng($keywords = array()) public function toPng(array $keywords = []): string
{ {
$colorRanges = array(); $colorRanges = [];
Rasterizer::rasterize( Rasterizer::rasterize(
$colorRanges, $this->edges, $colorRanges, $this->edges,

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -12,26 +12,27 @@
namespace Jdenticon\Canvas; namespace Jdenticon\Canvas;
use Jdenticon\Canvas\ColorUtils; use Jdenticon\Canvas\ColorUtils;
use Jdenticon\Canvas\CanvasState;
use Jdenticon\Canvas\Rasterization\EdgeTable; use Jdenticon\Canvas\Rasterization\EdgeTable;
use Jdenticon\Canvas\Rasterization\Edge; use Jdenticon\Canvas\Rasterization\Edge;
use Jdenticon\Canvas\Matrix; use Jdenticon\Canvas\Matrix;
class CanvasContext class CanvasContext
{ {
private $savedStates = array(); /** @var array<string, array> */
private $edges; private array $savedStates = [];
private $transform; private EdgeTable $edges;
private $paths; private Matrix $transform;
private $canvas; /** @var array<float> */
private array $paths;
private Canvas $canvas;
/** /**
* Creates a new canvas with the specified dimensions given in pixels. * Creates a new canvas with the specified dimensions given in pixels.
* *
* @param \Jdenticon\Canvas\Canvas $canvas The owner canvas. * @param \Jdenticon\Canvas\Canvas $canvas The owner canvas.
* @param array $edges The owner canvas' edge buffer. * @param \Jdenticon\Canvas\Rasterization\EdgeTable $edges The owner canvas' edge buffer.
*/ */
public function __construct($canvas, &$edges) public function __construct(Canvas $canvas, EdgeTable &$edges)
{ {
$this->edges = $edges; $this->edges = $edges;
$this->canvas = $canvas; $this->canvas = $canvas;
@ -46,25 +47,25 @@ class CanvasContext
* - strings on the format #RRGGBB * - strings on the format #RRGGBB
* - strings on the format #RRGGBBAA * - strings on the format #RRGGBBAA
* *
* @var integer|string * @var int|string
*/ */
public $fillStyle = 0x000000ff; public $fillStyle = 0x000000ff;
/** /**
* Saves the current state to the state stack. * Saves the current state to the state stack.
*/ */
public function save() public function save(): void
{ {
array_push($this->savedStates, array( array_push($this->savedStates, [
'transform' => $this->transform, 'transform' => $this->transform,
'fillStyle' => $this->fillStyle 'fillStyle' => $this->fillStyle
)); ]);
} }
/** /**
* Restores the last saved state of the CanvasContext. * Restores the last saved state of the CanvasContext.
*/ */
public function restore() public function restore(): void
{ {
$state = array_pop($this->savedStates); $state = array_pop($this->savedStates);
if ($state != NULL) { if ($state != NULL) {
@ -76,7 +77,7 @@ class CanvasContext
/** /**
* Resets the internal path buffer and begins a new path. * Resets the internal path buffer and begins a new path.
*/ */
public function resetTransform() public function resetTransform(): void
{ {
$this->transform = new Matrix(1, 0, 0, 1, 0, 0); $this->transform = new Matrix(1, 0, 0, 1, 0, 0);
} }
@ -84,36 +85,16 @@ class CanvasContext
/** /**
* Multiplies the current transformation matrix with the specified values. * Multiplies the current transformation matrix with the specified values.
*/ */
public function transform($a, $b, $c, $d, $e, $f) public function transform(float $a, float $b, float $c, float $d, float $e, float $f): void
{ {
if (gettype($a) != 'integer' ||
gettype($b) != 'integer' ||
gettype($c) != 'integer' ||
gettype($d) != 'integer' ||
gettype($e) != 'integer' ||
gettype($f) != 'integer'
) {
return;
}
$this->transform = $this->transform->multiply($a, $b, $c, $d, $e, $f); $this->transform = $this->transform->multiply($a, $b, $c, $d, $e, $f);
} }
/** /**
* Sets the transformation matrix to the specified matrix. * Sets the transformation matrix to the specified matrix.
*/ */
public function setTransform($a, $b, $c, $d, $e, $f) public function setTransform(float $a, float $b, float $c, float $d, float $e, float $f): void
{ {
if (gettype($a) != 'integer' ||
gettype($b) != 'integer' ||
gettype($c) != 'integer' ||
gettype($d) != 'integer' ||
gettype($e) != 'integer' ||
gettype($f) != 'integer'
) {
return;
}
$this->transform = new Matrix($a, $b, $c, $d, $e, $f); $this->transform = new Matrix($a, $b, $c, $d, $e, $f);
} }
@ -123,7 +104,7 @@ class CanvasContext
* @param float $x Distance to move in the horizontal direction in pixels. * @param float $x Distance to move in the horizontal direction in pixels.
* @param float $y Distance to move in the vertical direction in pixels. * @param float $y Distance to move in the vertical direction in pixels.
*/ */
public function translate($x, $y) public function translate(float $x, float $y): void
{ {
$this->transform = $this->transform->translate($x, $y); $this->transform = $this->transform->translate($x, $y);
} }
@ -134,7 +115,7 @@ class CanvasContext
* @param float $x Scale in the horizontal direction. 1 means no scale. * @param float $x Scale in the horizontal direction. 1 means no scale.
* @param float $y Scale in the vertical direction. 1 means no scale. * @param float $y Scale in the vertical direction. 1 means no scale.
*/ */
public function scale($x, $y) public function scale(float $x, float $y): void
{ {
$this->transform = $this->transform->scale($x, $y); $this->transform = $this->transform->scale($x, $y);
} }
@ -142,10 +123,9 @@ class CanvasContext
/** /**
* Applies a rotation transformation to the canvas around its current origo. * Applies a rotation transformation to the canvas around its current origo.
* *
* @param float $angle Angle in radians measured clockwise from the * @param float $angle Angle in radians measured clockwise from the positive x axis.
* positive x axis.
*/ */
public function rotate($angle) public function rotate(float $angle): void
{ {
$this->transform = $this->transform->rotate($angle); $this->transform = $this->transform->rotate($angle);
} }
@ -153,16 +133,16 @@ class CanvasContext
/** /**
* Removes all existing subpaths and begins a new path. * Removes all existing subpaths and begins a new path.
*/ */
public function beginPath() public function beginPath(): void
{ {
$this->paths = array(); $this->paths = [];
} }
/** /**
* Starts a new subpath that begins in the same point as the start and end * Starts a new subpath that begins in the same point as the start and end
* point of the previous one. * point of the previous one.
*/ */
public function closePath() public function closePath(): void
{ {
$pathsCount = count($this->paths); $pathsCount = count($this->paths);
if ($pathsCount > 0) { if ($pathsCount > 0) {
@ -179,7 +159,7 @@ class CanvasContext
} }
// Begin a new path // Begin a new path
$this->paths[] = array($path[0], $path[1]); $this->paths[] = [$path[0], $path[1]];
} }
} }
} }
@ -190,10 +170,10 @@ class CanvasContext
* @param float $x X coordinate. * @param float $x X coordinate.
* @param float $y Y coordinate. * @param float $y Y coordinate.
*/ */
public function moveTo($x, $y) public function moveTo(float $x, float $y): void
{ {
$p = $this->transform->multiplyPoint($x, $y); $p = $this->transform->multiplyPoint($x, $y);
$this->paths[] = array($p->x, $p->y); $this->paths[] = [$p->x, $p->y];
} }
/** /**
@ -201,13 +181,12 @@ class CanvasContext
* *
* @param float $x Target X coordinate. * @param float $x Target X coordinate.
* @param float $y Target Y coordinate. * @param float $y Target Y coordinate.
* @public
*/ */
public function lineTo($x, $y) public function lineTo(float $x, float $y): void
{ {
$pathsCount = count($this->paths); $pathsCount = count($this->paths);
if ($pathsCount == 0) { if ($pathsCount == 0) {
$this->paths[] = array(); $this->paths[] = [];
$pathsCount++; $pathsCount++;
} }
@ -227,10 +206,10 @@ class CanvasContext
* measured clockwise from the positive x axis. * measured clockwise from the positive x axis.
* @param float $endAngle The angle in radians at which the arc end, * @param float $endAngle The angle in radians at which the arc end,
* measured clockwise from the positive x axis. * measured clockwise from the positive x axis.
* @param boolean $anticlockwise Specifies whether the arc will be drawn * @param bool $anticlockwise Specifies whether the arc will be drawn
* counter clockwise. Default is clockwise. * counter clockwise. Default is clockwise.
*/ */
public function arc($x, $y, $radius, $startAngle, $endAngle, $anticlockwise) public function arc(float $x, float $y, float $radius, float $startAngle, float $endAngle, bool $anticlockwise): void
{ {
$TARGET_CHORD_LENGTH_PIXELS = 3; $TARGET_CHORD_LENGTH_PIXELS = 3;
@ -269,8 +248,6 @@ class CanvasContext
} }
} }
$dx;
$dy;
$sectors = ($endAngle - $startAngle) / $sectorAngle; $sectors = ($endAngle - $startAngle) / $sectorAngle;
$angle = $startAngle; $angle = $startAngle;
@ -296,7 +273,7 @@ class CanvasContext
* @param float $width Width of the rectangle. * @param float $width Width of the rectangle.
* @param float $height Height of the rectangle. * @param float $height Height of the rectangle.
*/ */
public function clearRect($x, $y, $width, $height) public function clearRect(float $x, float $y, float $width, float $height): void
{ {
$fullCanvas = false; $fullCanvas = false;
@ -330,23 +307,23 @@ class CanvasContext
* @param float $width Width of the rectangle. * @param float $width Width of the rectangle.
* @param float $height Height of the rectangle. * @param float $height Height of the rectangle.
*/ */
public function fillRect($x, $y, $width, $height) public function fillRect(float $x, float $y, float $width, float $height): void
{ {
$fillColor = ColorUtils::parse($this->fillStyle); $fillColor = ColorUtils::parse($this->fillStyle);
$this->_fillRect($fillColor, $x, $y, $width, $height); $this->_fillRect($fillColor, $x, $y, $width, $height);
} }
private function _fillRect($fillColor, $x, $y, $width, $height) private function _fillRect(int $fillColor, float $x, float $y, float $width, float $height): void
{ {
$polygonId = $this->edges->getNextPolygonId(); $polygonId = $this->edges->getNextPolygonId();
$points = array( $points = [
$this->transform->multiplyPoint($x, $y), $this->transform->multiplyPoint($x, $y),
$this->transform->multiplyPoint($x + $width, $y), $this->transform->multiplyPoint($x + $width, $y),
$this->transform->multiplyPoint($x + $width, $y + $height), $this->transform->multiplyPoint($x + $width, $y + $height),
$this->transform->multiplyPoint($x, $y + $height), $this->transform->multiplyPoint($x, $y + $height),
$this->transform->multiplyPoint($x, $y) $this->transform->multiplyPoint($x, $y)
); ];
$pointsCount = count($points); $pointsCount = count($points);
for ($i = 1; $i < $pointsCount; $i++) { for ($i = 1; $i < $pointsCount; $i++) {
@ -367,7 +344,7 @@ class CanvasContext
* which areas are covered by the current path. Valid values are * which areas are covered by the current path. Valid values are
* "evenodd" and "nonzero". Default is "nonzero". * "evenodd" and "nonzero". Default is "nonzero".
*/ */
public function fill($windingRule = "nonzero") public function fill(string $windingRule = "nonzero"): void
{ {
$polygonId = $this->edges->getNextPolygonId(); $polygonId = $this->edges->getNextPolygonId();
$fillColor = ColorUtils::parse($this->fillStyle); $fillColor = ColorUtils::parse($this->fillStyle);

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -15,9 +15,9 @@ class ColorUtils
{ {
/** /**
* Transparent color. * Transparent color.
* @var integer * @var int
*/ */
const TRANSPARENT = 0; public const TRANSPARENT = 0;
/** /**
* Specifies a transparent color that will not blend with layers below the * Specifies a transparent color that will not blend with layers below the
@ -25,15 +25,15 @@ class ColorUtils
* *
* @var float * @var float
*/ */
const FORCE_TRANSPARENT = INF; public const FORCE_TRANSPARENT = INF;
/** /**
* Creates a color on the format 0xRRGGBBAA from the specified * Creates a color on the format 0xRRGGBBAA from the specified
* color components. * color components.
* *
* @return integer * @return int
*/ */
public static function from($a, $r, $g, $b) public static function from(int $a, int $r, int $g, int $b): int
{ {
return ($r << 24) | ($g << 16) | ($b << 8) | $a; return ($r << 24) | ($g << 16) | ($b << 8) | $a;
} }
@ -41,10 +41,10 @@ class ColorUtils
/** /**
* Gets the alpha component of a color. * Gets the alpha component of a color.
* *
* @param integer $color 32-bit color value on the format 0xRRGGBBAA. * @param int $color 32-bit color value on the format 0xRRGGBBAA.
* @return integer Alpha in the range [0, 255]. * @return int Alpha in the range [0, 255].
*/ */
public static function alpha($color) public static function alpha(int $color): int
{ {
return $color & 0xff; return $color & 0xff;
} }
@ -52,10 +52,10 @@ class ColorUtils
/** /**
* Gets the red component of a color. * Gets the red component of a color.
* *
* @param integer $color 32-bit color value on the format 0xRRGGBBAA. * @param int $color 32-bit color value on the format 0xRRGGBBAA.
* @return integer Red component in the range [0, 255]. * @return int Red component in the range [0, 255].
*/ */
public static function red($color) public static function red(int $color): int
{ {
return ($color >> 24) & 0xff; return ($color >> 24) & 0xff;
} }
@ -63,10 +63,10 @@ class ColorUtils
/** /**
* Gets the green component of a color. * Gets the green component of a color.
* *
* @param integer $color 32-bit color value on the format 0xRRGGBBAA. * @param int $color 32-bit color value on the format 0xRRGGBBAA.
* @return integer Green component in the range [0, 255]. * @return int Green component in the range [0, 255].
*/ */
public static function green($color) public static function green(int $color): int
{ {
return ($color >> 16) & 0xff; return ($color >> 16) & 0xff;
} }
@ -74,10 +74,10 @@ class ColorUtils
/** /**
* Gets the blue component of a color. * Gets the blue component of a color.
* *
* @param integer $color 32-bit color value on the format 0xRRGGBBAA. * @param int $color 32-bit color value on the format 0xRRGGBBAA.
* @return integer Blue component in the range [0, 255]. * @return int Blue component in the range [0, 255].
*/ */
public static function blue($color) public static function blue(int $color): int
{ {
return ($color >> 8) & 0xff; return ($color >> 8) & 0xff;
} }
@ -85,10 +85,10 @@ class ColorUtils
/** /**
* Formats a color as a string. * Formats a color as a string.
* *
* @param integer $color Color to format. * @param int $color Color to format.
* @return string * @return string
*/ */
public static function format($color) public static function format(int $color): string
{ {
return bin2hex(pack('N', $color)); return bin2hex(pack('N', $color));
} }
@ -97,13 +97,13 @@ class ColorUtils
* Computes a mix of the two specified colors, with the proportion given * Computes a mix of the two specified colors, with the proportion given
* by the specified weight. * by the specified weight.
* *
* @param integer $color1 First color to mix. * @param int $color1 First color to mix.
* @param integer $color2 Second color to mix. * @param int $color2 Second color to mix.
* @param float $weight Weight in the range [0,1]. * @param float $weight Weight in the range [0,1].
* 0 gives $color1, 1 gives $color2. * 0 gives $color1, 1 gives $color2.
* @return integer Mixed color. * @return int Mixed color.
*/ */
public static function mix($color1, $color2, $weight) public static function mix(int $color1, int $color2, float $weight): int
{ {
if ($weight < 0) { if ($weight < 0) {
$weight = 0; $weight = 0;
@ -142,10 +142,10 @@ class ColorUtils
/** /**
* Parses a value to a 32-bit color on the format 0xRRGGBBAA. * Parses a value to a 32-bit color on the format 0xRRGGBBAA.
* *
* @param integer|string $color The value to parse. * @param int|string $color The value to parse.
* @return integer * @return int
*/ */
public static function parse($color) public static function parse($color): int
{ {
if (gettype($color) == "integer") { if (gettype($color) == "integer") {
return $color & 0xffffffff; return $color & 0xffffffff;
@ -187,11 +187,11 @@ class ColorUtils
/** /**
* Blends this color with another color using the over blending operation. * Blends this color with another color using the over blending operation.
* *
* @param integer $fore The foreground color. * @param int $fore The foreground color.
* @param integer $back The background color. * @param int $back The background color.
* @return integer * @return int
*/ */
public static function over($fore, $back) public static function over(int $fore, int $back): int
{ {
$foreA = ($fore & 0xff); $foreA = ($fore & 0xff);
$backA = ($back & 0xff); $backA = ($back & 0xff);

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -15,17 +15,17 @@ use Jdenticon\Canvas\Point;
class Matrix class Matrix
{ {
private $a; private float $a;
private $b; private float $b;
private $c; private float $c;
private $d; private float $d;
private $e; private float $e;
private $f; private float $f;
/** /**
* Creates a new transformation matrix. * Creates a new transformation matrix.
*/ */
public function __construct($a, $b, $c, $d, $e, $f) public function __construct(float $a, float $b, float $c, float $d, float $e, float $f)
{ {
$this->a = $a; $this->a = $a;
$this->b = $b; $this->b = $b;
@ -40,7 +40,7 @@ class Matrix
* *
* @return boolean * @return boolean
*/ */
public function hasSkewing() public function hasSkewing(): bool
{ {
return $this->b || $this->c; return $this->b || $this->c;
} }
@ -50,7 +50,7 @@ class Matrix
* *
* @return boolean * @return boolean
*/ */
public function hasTranslation() public function hasTranslation(): bool
{ {
return $this->e || $this->f; return $this->e || $this->f;
} }
@ -60,7 +60,7 @@ class Matrix
* *
* @return boolean * @return boolean
*/ */
public function hasScaling() public function hasScaling(): bool
{ {
return $this->a != 1 || $this->d != 1; return $this->a != 1 || $this->d != 1;
} }
@ -71,7 +71,7 @@ class Matrix
* *
* @return \Jdenticon\Canvas\Matrix * @return \Jdenticon\Canvas\Matrix
*/ */
public function multiply($a, $b, $c, $d, $e, $f) public function multiply(float $a, float $b, float $c, float $d, float $e, float $f): Matrix
{ {
return new Matrix( return new Matrix(
$this->a * $a + $this->c * $b, $this->a * $a + $this->c * $b,
@ -91,7 +91,7 @@ class Matrix
* @param float $y Y coordinate. * @param float $y Y coordinate.
* @return \Jdenticon\Canvas\Point * @return \Jdenticon\Canvas\Point
*/ */
public function multiplyPoint($x, $y) public function multiplyPoint(float $x, float $y): Point
{ {
return new Point( return new Point(
$this->a * $x + $this->c * $y + $this->e, $this->a * $x + $this->c * $y + $this->e,
@ -106,7 +106,7 @@ class Matrix
* @param float $angle Rotation angle in radians. * @param float $angle Rotation angle in radians.
* @return \Jdenticon\Canvas\Matrix * @return \Jdenticon\Canvas\Matrix
*/ */
public function rotate($angle) public function rotate(float $angle): Matrix
{ {
$sin = sin($angle); $sin = sin($angle);
$cos = cos($angle); $cos = cos($angle);
@ -121,7 +121,7 @@ class Matrix
* @param float $y Vertical move distance. * @param float $y Vertical move distance.
* @return \Jdenticon\Canvas\Matrix * @return \Jdenticon\Canvas\Matrix
*/ */
public function translate($x, $y) public function translate(float $x, float $y): Matrix
{ {
return $this->multiply(1, 0, 0, 1, $x, $y); return $this->multiply(1, 0, 0, 1, $x, $y);
} }
@ -134,7 +134,7 @@ class Matrix
* @param float $y Vertical scale. * @param float $y Vertical scale.
* @return \Jdenticon\Canvas\Matrix * @return \Jdenticon\Canvas\Matrix
*/ */
public function scale($x, $y) public function scale(float $x, float $y): Matrix
{ {
return $this->multiply($x, 0, 0, $y, 0, 0); return $this->multiply($x, 0, 0, $y, 0, 0);
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -13,15 +13,15 @@ namespace Jdenticon\Canvas\Png;
class PngBuffer class PngBuffer
{ {
private $buffer = ''; private string $buffer = '';
private $chunkPreviousBuffer = ''; private string $chunkPreviousBuffer = '';
/** /**
* Writes a string to the buffer. * Writes a string to the buffer.
* *
* @param string $str String to write. * @param string $str String to write.
*/ */
public function writeString($str) public function writeString(string $str): void
{ {
$this->buffer .= $str; $this->buffer .= $str;
} }
@ -31,7 +31,7 @@ class PngBuffer
* *
* @param integer $value Value to write. * @param integer $value Value to write.
*/ */
public function writeUInt32BE($value) public function writeUInt32BE(int $value): void
{ {
$this->buffer .= pack('N', $value); $this->buffer .= pack('N', $value);
} }
@ -41,7 +41,7 @@ class PngBuffer
* *
* @param integer $value Value to write. * @param integer $value Value to write.
*/ */
public function writeUInt8($value) public function writeUInt8(int $value): void
{ {
$this->buffer .= pack('C', $value); $this->buffer .= pack('C', $value);
} }
@ -52,7 +52,7 @@ class PngBuffer
* @param string $type Name of the chunk. Must contain exactly 4 * @param string $type Name of the chunk. Must contain exactly 4
* ASCII characters. * ASCII characters.
*/ */
public function startChunk($type) public function startChunk(string $type): void
{ {
$this->chunkPreviousBuffer = $this->buffer; $this->chunkPreviousBuffer = $this->buffer;
$this->buffer = $type; $this->buffer = $type;
@ -61,7 +61,7 @@ class PngBuffer
/** /**
* Closes the current PNG chunk. * Closes the current PNG chunk.
*/ */
public function endChunk() public function endChunk(): void
{ {
// Compute Crc32 for type + data // Compute Crc32 for type + data
$data = $this->buffer; $data = $this->buffer;
@ -85,7 +85,7 @@ class PngBuffer
* *
* @return string * @return string
*/ */
public function getBuffer() public function getBuffer(): string
{ {
return $this->buffer; return $this->buffer;
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -13,7 +13,6 @@ namespace Jdenticon\Canvas\Png;
use Jdenticon\Canvas\Png\PngPalette; use Jdenticon\Canvas\Png\PngPalette;
use Jdenticon\Canvas\Png\PngBuffer; use Jdenticon\Canvas\Png\PngBuffer;
use Jdenticon\Canvas\ColorUtils;
class PngEncoder class PngEncoder
{ {
@ -23,7 +22,7 @@ class PngEncoder
const GRAYSCALE_WITH_ALPHA = 4; const GRAYSCALE_WITH_ALPHA = 4;
const TRUE_COLOR_WITH_ALPHA = 6; const TRUE_COLOR_WITH_ALPHA = 6;
private $buffer; private PngBuffer $buffer;
public function __construct() public function __construct()
{ {
@ -36,10 +35,10 @@ class PngEncoder
* *
* @param int $width Image width in pixels. * @param int $width Image width in pixels.
* @param int $height Image height in pixels. * @param int $height Image height in pixels.
* @param int $colorType Color depth, speocfy one of the constants in * @param int $colorType Color depth, specify one of the constants in
* PngEncoder. * PngEncoder.
*/ */
public function writeImageHeader($width, $height, $colorType) public function writeImageHeader(int $width, int $height, int $colorType): void
{ {
$this->buffer->startChunk("IHDR"); $this->buffer->startChunk("IHDR");
$this->buffer->writeUInt32BE($width); $this->buffer->writeUInt32BE($width);
@ -57,7 +56,7 @@ class PngEncoder
* *
* @param int $gamma Gamma value. * @param int $gamma Gamma value.
*/ */
public function writeImageGamma($gamma = 45455) public function writeImageGamma(int $gamma = 45455): void
{ {
$this->buffer->startChunk("gAMA"); $this->buffer->startChunk("gAMA");
$this->buffer->writeUInt32BE($gamma); $this->buffer->writeUInt32BE($gamma);
@ -68,12 +67,11 @@ class PngEncoder
* Writes an IDAT chunk of truecolor encoded image data. * Writes an IDAT chunk of truecolor encoded image data.
* *
* @param array $colorRanges Image data on the format * @param array $colorRanges Image data on the format
* array(count0, color0, count1, color1, ...) * [count0, color0, count1, color1, ...]
* @param int $width Image width in pixels. * @param int $width Image width in pixels.
* @param int $height Image height in pixels. * @param int $height Image height in pixels.
*/ */
public function writeTrueColorWithAlpha( public function writeTrueColorWithAlpha(array &$colorRanges, int $width, int $height): void
array & $colorRanges, $width, $height)
{ {
$this->buffer->startChunk("IDAT"); $this->buffer->startChunk("IDAT");
@ -111,16 +109,13 @@ class PngEncoder
* Writes an IDAT chunk of indexed image data. * Writes an IDAT chunk of indexed image data.
* *
* @param array $colorRanges Image data on the format * @param array $colorRanges Image data on the format
* array(count0, color0, count1, color1, ...) * [count0, color0, count1, color1, ...]
* @param \Jdenticon\Canvas\Png\PngPalette $palette Palette containing the * @param \Jdenticon\Canvas\Png\PngPalette $palette Palette containing the
* indexed colors. * indexed colors.
* @param int $width Image width in pixels. * @param int $width Image width in pixels.
* @param int $height Image height in pixels. * @param int $height Image height in pixels.
*/ */
public function writeIndexed( public function writeIndexed(array &$colorRanges, PngPalette $palette, int $width, int $height): void
array & $colorRanges,
PngPalette $palette,
$width, $height)
{ {
$this->buffer->startChunk("IDAT"); $this->buffer->startChunk("IDAT");
@ -162,7 +157,7 @@ class PngEncoder
* @param \Jdenticon\Canvas\Png\PngPalette $palette Palette containing the * @param \Jdenticon\Canvas\Png\PngPalette $palette Palette containing the
* indexed colors. * indexed colors.
*/ */
public function writePalette(PngPalette $palette) public function writePalette(PngPalette $palette): void
{ {
if ($palette && $palette->isValid) { if ($palette && $palette->isValid) {
$this->buffer->startChunk("PLTE"); $this->buffer->startChunk("PLTE");
@ -184,7 +179,7 @@ class PngEncoder
* @param \Jdenticon\Canvas\Png\PngPalette $palette Palette containing the * @param \Jdenticon\Canvas\Png\PngPalette $palette Palette containing the
* indexed colors. * indexed colors.
*/ */
public function writeTransparency(PngPalette $palette) public function writeTransparency(PngPalette $palette): void
{ {
if ($palette && $palette->isValid && $palette->hasAlphaChannel) { if ($palette && $palette->isValid && $palette->hasAlphaChannel) {
$this->buffer->startChunk("tRNS"); $this->buffer->startChunk("tRNS");
@ -208,7 +203,7 @@ class PngEncoder
* {@link https://www.w3.org/TR/2003/REC-PNG-20031110/#11keywords} * {@link https://www.w3.org/TR/2003/REC-PNG-20031110/#11keywords}
* @param string $value Value. * @param string $value Value.
*/ */
public function writeTextualData($key, $value) public function writeTextualData(string $key, string $value): void
{ {
$this->buffer->startChunk("tEXt"); $this->buffer->startChunk("tEXt");
$this->buffer->writeString($key); $this->buffer->writeString($key);
@ -220,7 +215,7 @@ class PngEncoder
/** /**
* Writes an IEND chunk to the png data stream. * Writes an IEND chunk to the png data stream.
*/ */
public function writeImageEnd() public function writeImageEnd(): void
{ {
$this->buffer->startChunk("IEND"); $this->buffer->startChunk("IEND");
$this->buffer->endChunk(); $this->buffer->endChunk();
@ -231,7 +226,7 @@ class PngEncoder
* *
* @return string * @return string
*/ */
public function getBuffer() public function getBuffer(): string
{ {
return $this->buffer->getBuffer(); return $this->buffer->getBuffer();
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -11,8 +11,6 @@
namespace Jdenticon\Canvas\Png; namespace Jdenticon\Canvas\Png;
use Jdenticon\Canvas\ColorUtils;
/** /**
* Contains the colors of a PNG color palette. * Contains the colors of a PNG color palette.
*/ */
@ -21,13 +19,13 @@ class PngPalette
/** /**
* Creates a PNG color palette for the specified bitmap data. * Creates a PNG color palette for the specified bitmap data.
* *
* @param array(integer) $colorRanges Array of interleaved values on the * @param array<int> $colorRanges Array of interleaved values on the
* format array(count0, color0, count1, color1, ...). * format [count0, color0, count1, color1, ...]
*/ */
function __construct(& $colorRanges) function __construct(array &$colorRanges)
{ {
$lookup = array(); $lookup = [];
$colors = array(); $colors = [];
$hasAlphaChannel = false; $hasAlphaChannel = false;
$colorsCount = 0; $colorsCount = 0;
@ -63,30 +61,22 @@ class PngPalette
/** /**
* Specifies if the palette is valid to be used for encoding a PNG image. * Specifies if the palette is valid to be used for encoding a PNG image.
*
* @var boolean
*/ */
public $isValid; public bool $isValid;
/** /**
* Specifies if the palette has any partial or fully transparent * Specifies if the palette has any partial or fully transparent
* colors. * colors.
*
* @var boolean
*/ */
public $hasAlphaChannel; public bool $hasAlphaChannel;
/** /**
* Array of colors in the palette. * Array of colors in the palette.
*
* @var array
*/ */
public $colors; public array $colors;
/** /**
* Lookup table from 32-bit color value to color index. * Lookup table from 32-bit color value to color index.
*
* @var array
*/ */
public $lookup; public array $lookup;
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -15,17 +15,13 @@ class Point
{ {
/** /**
* X coordinate. * X coordinate.
*
* @var float
*/ */
public $x; public float $x;
/** /**
* Y coordinate. * Y coordinate.
*
* @var float
*/ */
public $y; public float $y;
/** /**
* Creates a new 2D point. * Creates a new 2D point.
@ -33,7 +29,7 @@ class Point
* @param float $x X coordinate. * @param float $x X coordinate.
* @param float $y Y coordinate. * @param float $y Y coordinate.
*/ */
public function __construct($x, $y) public function __construct(float $x, float $y)
{ {
$this->x = $x; $this->x = $x;
$this->y = $y; $this->y = $y;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -13,16 +13,22 @@ namespace Jdenticon\Canvas\Rasterization;
class Edge class Edge
{ {
public $polygonId; public int $polygonId;
public $x0; public float $x0;
public $x1; public float $x1;
public $y0; public float $y0;
public $y1; public float $y1;
public $color; public int $color;
public $windingRule; public string $windingRule;
public function __construct( public function __construct(
$polygonId, $x0, $y0, $x1, $y1, $color, $windingRule = null) int $polygonId,
float $x0,
float $y0,
float $x1,
float $y1,
int $color,
string $windingRule = "nonzero")
{ {
$this->polygonId = $polygonId; $this->polygonId = $polygonId;
$this->x0 = $x0; $this->x0 = $x0;
@ -33,7 +39,7 @@ class Edge
$this->windingRule = $windingRule; $this->windingRule = $windingRule;
} }
public function intersection($y) public function intersection(float $y): float
{ {
$dx = $dx =
($this->x1 - $this->x0) * ($this->y0 - $y) / ($this->x1 - $this->x0) * ($this->y0 - $y) /

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -13,11 +13,11 @@ namespace Jdenticon\Canvas\Rasterization;
class EdgeIntersection class EdgeIntersection
{ {
public $fromX; public int $fromX;
public $width; public int $width;
public $edge; public Edge $edge;
public function __construct($fromX, $width, $edge) public function __construct(int $fromX, int $width, Edge $edge)
{ {
$this->fromX = $fromX; $this->fromX = $fromX;
$this->width = $width; $this->width = $width;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -13,10 +13,10 @@ namespace Jdenticon\Canvas\Rasterization;
class EdgeSuperSampleIntersection class EdgeSuperSampleIntersection
{ {
public $x; public float $x;
public $edge; public Edge $edge;
public function __construct($x, $edge) public function __construct(float $x, Edge $edge)
{ {
$this->x = $x; $this->x = $x;
$this->edge = $edge; $this->edge = $edge;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -11,20 +11,25 @@
namespace Jdenticon\Canvas\Rasterization; namespace Jdenticon\Canvas\Rasterization;
use Jdenticon\Canvas\Rasterization\Edge;
class EdgeTable class EdgeTable
{ {
private $scanlines; /**
private $nextPolygonId; * @var array<array<EdgeIntersection>>
private $width; */
private $height; private array $scanlines;
private int $nextPolygonId;
private int $width;
private int $height;
/** /**
* Keeps a list of edges per scanline. * Keeps a list of edges per scanline.
* *
* @param integer $width Clipping width. * @param int $width Clipping width.
* @param integer $height Clipping height. * @param int $height Clipping height.
*/ */
public function __construct($width, $height) public function __construct(int $width, int $height)
{ {
$this->width = $width; $this->width = $width;
$this->height = $height; $this->height = $height;
@ -34,9 +39,9 @@ class EdgeTable
/** /**
* Sorts the edges of each scanline in ascending x coordinates. * Sorts the edges of each scanline in ascending x coordinates.
*/ */
public function clear() public function clear(): void
{ {
$this->scanlines = array(); $this->scanlines = [];
$this->nextPolygonId = 1; $this->nextPolygonId = 1;
} }
@ -45,18 +50,19 @@ class EdgeTable
* *
* @return int * @return int
*/ */
public function getNextPolygonId() public function getNextPolygonId(): int
{ {
return $this->nextPolygonId++; return $this->nextPolygonId++;
} }
/** /**
* Gets the scaline for the specified Y coordinate, or NULL if there are * Gets the scanline for the specified Y coordinate, or NULL if there are
* no edges for the specified Y coordinate. * no edges for the specified Y coordinate.
* *
* @return array|null. * @param int $y
* @return array<EdgeIntersection>|null
*/ */
public function getScanline($y) public function getScanline(int $y): ?array
{ {
return isset($this->scanlines[$y]) ? $this->scanlines[$y] : null; return isset($this->scanlines[$y]) ? $this->scanlines[$y] : null;
} }
@ -66,7 +72,7 @@ class EdgeTable
* *
* @param \Jdenticon\Canvas\Rasterization\Edge $edge * @param \Jdenticon\Canvas\Rasterization\Edge $edge
*/ */
public function add(\Jdenticon\Canvas\Rasterization\Edge $edge) public function add(Edge $edge): void
{ {
$minY = 0; $minY = 0;
$maxY = 0; $maxY = 0;
@ -121,7 +127,7 @@ class EdgeTable
if ($fromX < $this->width) { if ($fromX < $this->width) {
if (!isset($this->scanlines[$y])) { if (!isset($this->scanlines[$y])) {
$this->scanlines[$y] = array(); $this->scanlines[$y] = [];
} }
$this->scanlines[$y][] = new EdgeIntersection( $this->scanlines[$y][] = new EdgeIntersection(
@ -134,7 +140,7 @@ class EdgeTable
} }
} }
private static function edge_cmp($x, $y) private static function edge_cmp(EdgeIntersection $x, EdgeIntersection $y): int
{ {
if ($x->fromX < $y->fromX) { if ($x->fromX < $y->fromX) {
return -1; return -1;
@ -148,11 +154,10 @@ class EdgeTable
/** /**
* Sorts the edges of each scanline in ascending x coordinates. * Sorts the edges of each scanline in ascending x coordinates.
*/ */
public function sort() public function sort(): void
{ {
foreach ($this->scanlines as $i => &$scanline) { foreach ($this->scanlines as $i => &$scanline) {
usort($scanline, array( usort($scanline, [self::class, 'edge_cmp']);
'Jdenticon\\Canvas\\Rasterization\\EdgeTable', 'edge_cmp'));
} }
} }
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -13,14 +13,21 @@ namespace Jdenticon\Canvas\Rasterization;
class Layer class Layer
{ {
public $polygonId; public int $polygonId;
public $color; public int $color;
public $winding; public int $winding;
public $windingRule; public string $windingRule;
public ?Layer $nextLayer = null;
public $nextLayer; /**
* Creates a new layer.
public function __construct($polygonId, $color, $winding, $windingRule) *
* @param int $polygonId Unique id for this layer.
* @param int $color Color on the format 0xRRGGBBAA.
* @param int $winding Differential winding value, either 1 or -1.
* @param string $windingRule Winding rule for the polygon, either "evenodd" or "nonzero".
*/
public function __construct(int $polygonId, int $color, int $winding, string $windingRule)
{ {
$this->polygonId = $polygonId; $this->polygonId = $polygonId;
$this->color = $color; $this->color = $color;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -21,13 +21,12 @@ use Jdenticon\Canvas\Rasterization\Edge;
*/ */
class LayerManager class LayerManager
{ {
public $topLayer; public ?Layer $topLayer = null;
/** /**
* The current visible color. * The current visible color.
* @var integer
*/ */
public $color; public int $color;
public function __construct() public function __construct()
{ {
@ -40,7 +39,7 @@ class LayerManager
* @param \Jdenticon\Canvas\Rasterization\LayerManager $other The * @param \Jdenticon\Canvas\Rasterization\LayerManager $other The
* LayerManager to copy all layers to. * LayerManager to copy all layers to.
*/ */
public function copyTo(LayerManager $other) public function copyTo(LayerManager $other): void
{ {
$other->color = $this->color; $other->color = $this->color;
@ -72,7 +71,7 @@ class LayerManager
* *
* @param \Jdenticon\Canvas\Rasterization\Edge edge * @param \Jdenticon\Canvas\Rasterization\Edge edge
*/ */
public function add(Edge $edge) public function add(Edge $edge): void
{ {
$dwinding = $edge->y0 < $edge->y1 ? 1 : -1; $dwinding = $edge->y0 < $edge->y1 ? 1 : -1;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -37,15 +37,18 @@ class Rasterizer
/** /**
* Rasterizes the edges in the edge table to a list of color ranges. No * Rasterizes the edges in the edge table to a list of color ranges. No
* range will span multiple scanlines. * range will span multiple scanlines.
*
* @param array<int> $colorData
* @return array<int>
*/ */
public static function rasterize(& $colorData, $edgeTable, $width, $height) public static function rasterize(array &$colorData, EdgeTable $edgeTable, int $width, int $height): array
{ {
$edgeTable->sort(); $edgeTable->sort();
$superSampleBuffer = new SuperSampleBuffer( $superSampleBuffer = new SuperSampleBuffer(
$width, self::SAMPLES_PER_PIXEL_X); $width, self::SAMPLES_PER_PIXEL_X);
$layers = array(); $layers = [];
$color = 0; $color = 0;
// Keeps track of how many of the subpixellayers that are used for // Keeps track of how many of the subpixellayers that are used for
@ -278,7 +281,7 @@ class Rasterizer
return $colorData; return $colorData;
} }
private static function intersection_cmp($a, $b) private static function intersection_cmp(EdgeSuperSampleIntersection $a, EdgeSuperSampleIntersection $b): int
{ {
if ($a->x < $b->x) { if ($a->x < $b->x) {
return -1; return -1;
@ -296,14 +299,14 @@ class Rasterizer
* y coordinate. For each intersecting edge the intersecting x coordinate is * y coordinate. For each intersecting edge the intersecting x coordinate is
* returned. * returned.
* *
* @param array $edges Array of edges in the current scanline. * @param array<Edge> $edges Array of edges in the current scanline.
* @param int $y Y coordinate of the current scanline. * @param float $y Y coordinate of the current scanline.
* @return array Array containing EdgeSuperSampleIntersection. Objects * @return array<EdgeSuperSampleIntersection> Intersections sorted ascending by x coordinate.
* are sorted ascending by x coordinate.
*/ */
private static function getIntersections($edges, $y) private static function getIntersections(array $edges, float $y): array
{ {
$intersections = array(); /** @var array<EdgeSuperSampleIntersection> */
$intersections = [];
foreach ($edges as $edge) { foreach ($edges as $edge) {
if ($edge->y0 < $y && $edge->y1 >= $y || if ($edge->y0 < $y && $edge->y1 >= $y ||
@ -317,9 +320,7 @@ class Rasterizer
} }
} }
usort($intersections, array( usort($intersections, [self::class, 'intersection_cmp']);
'Jdenticon\\Canvas\\Rasterization\\Rasterizer',
'intersection_cmp'));
return $intersections; return $intersections;
} }
@ -327,12 +328,13 @@ class Rasterizer
/** /**
* Determines what ranges of a scanline that needs to be supersampled. * Determines what ranges of a scanline that needs to be supersampled.
* *
* @param array $scanline Array of edges in the current scanline. * @param array<EdgeIntersection> $scanline Array of edges in the current scanline.
* @return array Array of SuperSampleRange. * @param int $width
* @return array<SuperSampleRange>
*/ */
private static function getSuperSampleRanges(&$scanline, $width) private static function getSuperSampleRanges(array &$scanline, int $width): array
{ {
$superSampleRanges = array(); $superSampleRanges = [];
$rangeIndex = 0; $rangeIndex = 0;
$scanlineCount = count($scanline); $scanlineCount = count($scanline);

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -21,25 +21,25 @@ class SuperSampleBuffer
const IDX_G = 3; const IDX_G = 3;
const IDX_B = 4; const IDX_B = 4;
private $samples; private array $samples;
private $samplesPerPixel; private int $samplesPerPixel;
private $pixelOffset; private int $pixelOffset;
private $subPixelOffset; private int $subPixelOffset;
private $width; private int $width;
private $used; private int $used;
/** /**
* Creates a color buffer keeping an average color out of several * Creates a color buffer keeping an average color out of several
* color samples per pixel. * color samples per pixel.
* *
* @param integer $width Width of the buffer in pixels. * @param int $width Width of the buffer in pixels.
* @param integer $samplesPerPixel Number of samples to keep per pixel. * @param int $samplesPerPixel Number of samples to keep per pixel.
*/ */
public function __construct($width, $samplesPerPixel) public function __construct(int $width, int $samplesPerPixel)
{ {
$this->samples = array(); $this->samples = [];
$this->samplesPerPixel = $samplesPerPixel; $this->samplesPerPixel = $samplesPerPixel;
$this->pixelOffset = 0; $this->pixelOffset = 0;
@ -52,7 +52,7 @@ class SuperSampleBuffer
/** /**
* Rewinds the cursor to the beginning of the buffer. * Rewinds the cursor to the beginning of the buffer.
*/ */
public function rewind() public function rewind(): void
{ {
$this->pixelOffset = 0; $this->pixelOffset = 0;
$this->subPixelOffset = 0; $this->subPixelOffset = 0;
@ -61,7 +61,7 @@ class SuperSampleBuffer
/** /**
* Clears the samples in this buffer. * Clears the samples in this buffer.
*/ */
public function clear() public function clear(): void
{ {
$this->pixelOffset = 0; $this->pixelOffset = 0;
$this->subPixelOffset = 0; $this->subPixelOffset = 0;
@ -71,11 +71,11 @@ class SuperSampleBuffer
/** /**
* Writes the average color of each pixel to a specified color array. * Writes the average color of each pixel to a specified color array.
* *
* @param array $colorData The average colors will be written to this * @param array<int> $colorData The average colors will be written to this
* color array. * color array.
* @param integer $count Number of pixels to write. * @param int $count Number of pixels to write.
*/ */
public function emptyTo(& $colorData, $count) public function emptyTo(array &$colorData, int $count): void
{ {
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
$sampleCount = $this->samples[$i * 5 + self::IDX_COUNT]; $sampleCount = $this->samples[$i * 5 + self::IDX_COUNT];
@ -100,10 +100,10 @@ class SuperSampleBuffer
/** /**
* Gets the average color of the pixel at a specified index. * Gets the average color of the pixel at a specified index.
* *
* @param integer $index The index of the pixel. * @param int $index The index of the pixel.
* @return integer * @return int
*/ */
public function colorAt($index) public function colorAt(int $index): int
{ {
$sampleCount = $this->samples[$index * 5 + self::IDX_COUNT]; $sampleCount = $this->samples[$index * 5 + self::IDX_COUNT];
$alphaSum = $this->samples[$index * 5 + self::IDX_A]; $alphaSum = $this->samples[$index * 5 + self::IDX_A];
@ -119,10 +119,14 @@ class SuperSampleBuffer
/** /**
* Adds a color to the current pixel in the buffer. * Adds a color to the current pixel in the buffer.
* *
* @param integer $count Number of samples of the color to be added to * @param int $count Number of samples of the color to be added to
* the buffer. * the buffer.
* @param int $a Alpha value.
* @param int $r Red value.
* @param int $g Green value.
* @param int $b Blue value.
*/ */
private function _add($count, $a, $r, $g, $b) private function _add(int $count, int $a, int $r, int $g, int $b): void
{ {
if ($this->used < $this->pixelOffset) { if ($this->used < $this->pixelOffset) {
$this->used = $this->pixelOffset; $this->used = $this->pixelOffset;
@ -147,11 +151,11 @@ class SuperSampleBuffer
/** /**
* Adds a color to the buffer up until the specified x index. * Adds a color to the buffer up until the specified x index.
* *
* @param integer $color Color to write. * @param int $color Color to write.
* @param float $untilX Samples of the color will be added the buffer until * @param float $untilX Samples of the color will be added to the buffer until
* the cursor reaches this coordinate. * the cursor reaches this coordinate.
*/ */
public function add($color, $untilX) public function add(int $color, float $untilX): void
{ {
$samplesLeft = $samplesLeft =
(int)($untilX * $this->samplesPerPixel) - (int)($untilX * $this->samplesPerPixel) -

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -13,15 +13,16 @@ namespace Jdenticon\Canvas\Rasterization;
class SuperSampleRange class SuperSampleRange
{ {
public $fromX; public int $fromX;
public $toXExcl; public int $toXExcl;
public $edges; /** @var array<Edge> */
public $width; public array $edges;
public int $width;
public function __construct($fromX, $toXExcl) public function __construct(int $fromX, int $toXExcl)
{ {
$this->fromX = $fromX; $this->fromX = $fromX;
$this->toXExcl = $toXExcl; $this->toXExcl = $toXExcl;
$this->edges = array(); $this->edges = [];
} }
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -16,32 +16,28 @@ namespace Jdenticon;
*/ */
class Color class Color
{ {
private static $lightnessCompensations = array( /** @var array<float> */
0.55, 0.5, 0.5, 0.46, 0.6, 0.55, 0.55); private static array $lightnessCompensations = [0.55, 0.5, 0.5, 0.46, 0.6, 0.55, 0.55];
/** /**
* The red component of the color in the range [0, 255]. * The red component of the color in the range [0, 255].
* @var int
*/ */
public $r; public int $r;
/** /**
* The green component of the color in the range [0, 255]. * The green component of the color in the range [0, 255].
* @var int
*/ */
public $g; public int $g;
/** /**
* The blue component of the color in the range [0, 255]. * The blue component of the color in the range [0, 255].
* @var int
*/ */
public $b; public int $b;
/** /**
* The alpha component of the color in the range [0, 255]. * The alpha component of the color in the range [0, 255].
* @var int
*/ */
public $a; public int $a;
// Users of the struct should use the static factory methods // Users of the struct should use the static factory methods
// to create Color value. // to create Color value.
@ -52,12 +48,13 @@ class Color
/** /**
* Creates a Color from an RGB value. * Creates a Color from an RGB value.
* *
* @param int $alpha Alpha channel value in the range [0, 255].
* @param int $red Red component in the range [0, 255]. * @param int $red Red component in the range [0, 255].
* @param int $green GReen component in the range [0, 255]. * @param int $green Green component in the range [0, 255].
* @param int $blue Blue component in the range [0, 255]. * @param int $blue Blue component in the range [0, 255].
* @param int $alpha Alpha channel value in the range [0, 255].
* @return \Jdenticon\Color
*/ */
public static function fromRgb($red, $green, $blue, $alpha = 255) public static function fromRgb(int $red, int $green, int $blue, int $alpha = 255): Color
{ {
$color = new Color(); $color = new Color();
$color->r = $red; $color->r = $red;
@ -70,12 +67,13 @@ class Color
/** /**
* Creates a Color instance from HSL color parameters. * Creates a Color instance from HSL color parameters.
* *
* @param float $hue Hue in the range [0, 1] * @param float $hue Hue in the range [0, 1].
* @param float $saturation Saturation in the range [0, 1] * @param float $saturation Saturation in the range [0, 1].
* @param float $lightness Lightness in the range [0, 1] * @param float $lightness Lightness in the range [0, 1].
* @param float $alpha Alpha channel value in the range [0, 1]. * @param float $alpha Alpha channel value in the range [0, 1].
* @return \Jdenticon\Color
*/ */
public static function fromHsl($hue, $saturation, $lightness, $alpha = 1.0) public static function fromHsl(float $hue, float $saturation, float $lightness, float $alpha = 1.0): Color
{ {
if ($hue < 0) $hue = 0; if ($hue < 0) $hue = 0;
if ($hue > 1) $hue = 1; if ($hue > 1) $hue = 1;
@ -111,15 +109,16 @@ class Color
} }
/** /**
* Creates a Color> instance from HSL color parameters and will compensate * Creates a Color instance from HSL color parameters and compensates
* the lightness for hues that appear to be darker than others. * the lightness for hues that appear to be darker than others.
* *
* @param float $hue Hue in the range [0, 1]. * @param float $hue Hue in the range [0, 1].
* @param float $saturation Saturation in the range [0, 1]. * @param float $saturation Saturation in the range [0, 1].
* @param float $lightness Lightness in the range [0, 1]. * @param float $lightness Lightness in the range [0, 1].
* @param float $alpha Alpha channel value in the range [0, 1]. * @param float $alpha Alpha channel value in the range [0, 1].
* @return \Jdenticon\Color
*/ */
public static function fromHslCompensated($hue, $saturation, $lightness, $alpha = 1.0) public static function fromHslCompensated(float $hue, float $saturation, float $lightness, float $alpha = 1.0): Color
{ {
if ($hue < 0) $hue = 0; if ($hue < 0) $hue = 0;
if ($hue > 1) $hue = 1; if ($hue > 1) $hue = 1;
@ -135,7 +134,7 @@ class Color
} }
// Helper method for FromHsl // Helper method for FromHsl
private static function hueToRgb($m1, $m2, $h) private static function hueToRgb(float $m1, float $m2, float $h): int
{ {
if ($h < 0) { if ($h < 0) {
$h = $h + 6; $h = $h + 6;
@ -161,7 +160,7 @@ class Color
* *
* @return int * @return int
*/ */
public function toRgba() public function toRgba(): int
{ {
return return
($this->r << 24) | ($this->r << 24) |
@ -175,7 +174,7 @@ class Color
* *
* @return string * @return string
*/ */
public function __toString() public function __toString(): string
{ {
return '#' . bin2hex(pack('N', $this->toRgba())); return '#' . bin2hex(pack('N', $this->toRgba()));
} }
@ -183,9 +182,10 @@ class Color
/** /**
* Gets a hexadecimal representation of this color on the format #rrggbbaa. * Gets a hexadecimal representation of this color on the format #rrggbbaa.
* *
* @param int $length Length of the hex string (6 or 8).
* @return string * @return string
*/ */
public function toHexString($length = 8) public function toHexString(int $length = 8): string
{ {
if ($length === 8) { if ($length === 8) {
return $this->__toString(); return $this->__toString();
@ -197,10 +197,11 @@ class Color
* Tries to parse a value as a Color. * Tries to parse a value as a Color.
* *
* @param mixed $value Value to parse. * @param mixed $value Value to parse.
* @throws InvalidArgumentException * @throws \InvalidArgumentException
* @return \Jdenticon\Color * @return \Jdenticon\Color
*/ */
public static function parse($value) { public static function parse($value): Color
{
if ($value instanceof Color) { if ($value instanceof Color) {
return $value; return $value;
} }
@ -252,7 +253,7 @@ class Color
* *
* @return boolean * @return boolean
*/ */
private static function parsePercent($input, &$result) private static function parsePercent(string $input, ?float &$result): bool
{ {
// Detect and remove percent sign // Detect and remove percent sign
if (preg_match('/^\\s*(\\d*(?:\\.\\d*)?)%\\s*$/', $input, $matches)) { if (preg_match('/^\\s*(\\d*(?:\\.\\d*)?)%\\s*$/', $input, $matches)) {
@ -274,7 +275,7 @@ class Color
* *
* @return boolean * @return boolean
*/ */
private static function parseAlpha($input, &$result) private static function parseAlpha(?string $input, ?float &$result): bool
{ {
if ($input === null || if ($input === null ||
$input === '' $input === ''
@ -304,20 +305,20 @@ class Color
* Parses an RGB component. * Parses an RGB component.
* *
* @param string $input Input string. * @param string $input Input string.
* @param float $result Hue in range [0, 255]. * @param int $result RGB component in range [0, 255].
* *
* @return boolean * @return boolean
*/ */
private static function parseRgbComponent($input, &$result) private static function parseRgbComponent(string $input, ?int &$result): bool
{ {
if (preg_match('/^\\s*(\\d*(?:\\.\\d*)?)(%?)\\s*$/', $input, $matches)) { if (preg_match('/^\\s*(\\d*(?:\\.\\d*)?)(%?)\\s*$/', $input, $matches)) {
$result = floatval($matches[1]); $float = floatval($matches[1]);
if ($matches[2] === '%') { if ($matches[2] === '%') {
$result = 255 * $result / 100; $float = 255 * $float / 100;
} }
$result = (int)$result; $result = (int)$float;
if ($result < 0) $result = 0; if ($result < 0) $result = 0;
if ($result > 255) $result = 255; if ($result > 255) $result = 255;
@ -335,7 +336,7 @@ class Color
* *
* @return boolean * @return boolean
*/ */
private static function parseHue($input, &$result) private static function parseHue(string $input, ?float &$result): bool
{ {
if (preg_match( if (preg_match(
'/^\s*(\d*(?:\.\d*)?)(deg|grad|rad|turn|)\s*$/', '/^\s*(\d*(?:\.\d*)?)(deg|grad|rad|turn|)\s*$/',
@ -355,7 +356,7 @@ class Color
break; break;
case "turn": case "turn":
// Turns: range 0 - 1 // Turns: range 0 - 1
$result = $result; // No change
break; break;
default: default:
// Degree: range 0 - 360 // Degree: range 0 - 360
@ -378,11 +379,11 @@ class Color
* Parses a hex color string. * Parses a hex color string.
* *
* @param string $input Input string. * @param string $input Input string.
* @param float $result Hue in range [0, 1]. * @param \Jdenticon\Color $result Parsed color.
* *
* @return boolean * @return boolean
*/ */
private static function parseHexColor($input, &$result) private static function parseHexColor(string $input, ?Color &$result): bool
{ {
if ($input[0] === '#') { if ($input[0] === '#') {
$input = substr($input, 1); $input = substr($input, 1);
@ -444,9 +445,9 @@ class Color
* *
* @param string $input Input string. * @param string $input Input string.
* *
* @return \Jdenticon\Color * @return \Jdenticon\Color|null
*/ */
private static function parseNamedColor($input) private static function parseNamedColor(string $input): ?Color
{ {
// Source: https://www.w3.org/TR/css-color-4/#named-colors // Source: https://www.w3.org/TR/css-color-4/#named-colors
switch ($input) { switch ($input) {

View file

@ -3,7 +3,8 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* Copyright (c) 2024 Peter Putzer
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -29,37 +30,17 @@ class Identicon
*/ */
private $value; private $value;
/** private bool $valueSet = false;
* @var boolean
*/
private $valueSet = false;
/** /**
* Defaults to hash of an empty string. * Defaults to hash of an empty string.
*
* @var string
*/ */
private $hash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; private string $hash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
/** private int $size = 100;
* @var integer private IconGenerator $iconGenerator;
*/ private ?IdenticonStyle $style = null;
private $size = 100; private ?bool $enableImageMagick = null;
/**
* @var Jdenticon\Rendering\IconGenerator
*/
private $iconGenerator;
/**
* @var Jdenticon\IdenticonStyle
*/
private $style;
/**
* @var bool
*/
private $enableImageMagick;
/** /**
* Creates an Identicon instance with the specified hash. * Creates an Identicon instance with the specified hash.
@ -69,7 +50,7 @@ class Identicon
* @param int|float|double $size The size of the icon in pixels (the icon * @param int|float|double $size The size of the icon in pixels (the icon
* is quadratic). * is quadratic).
*/ */
public function __construct($options = null) public function __construct(?array $options = null)
{ {
$this->iconGenerator = IconGenerator::getDefaultGenerator(); $this->iconGenerator = IconGenerator::getDefaultGenerator();
@ -90,9 +71,9 @@ class Identicon
* @param int $size The size of the icon in pixels (the icon is quadratic). * @param int $size The size of the icon in pixels (the icon is quadratic).
* @return \Jdenticon\Identicon * @return \Jdenticon\Identicon
*/ */
public static function fromHash($hash, $size) public static function fromHash(string $hash, int $size): self
{ {
return new Identicon(array('hash' => $hash, 'size' => $size)); return new Identicon(['hash' => $hash, 'size' => $size]);
} }
/** /**
@ -104,19 +85,19 @@ class Identicon
* @param int $size The size of the icon in pixels (the icon is quadratic). * @param int $size The size of the icon in pixels (the icon is quadratic).
* @return \Jdenticon\Identicon * @return \Jdenticon\Identicon
*/ */
public static function fromValue($value, $size) public static function fromValue($value, int $size): self
{ {
return new Identicon(array('value' => $value, 'size' => $size)); return new Identicon(['value' => $value, 'size' => $size]);
} }
/** /**
* Gets an associative array of all options of this identicon. * Gets an associative array of all options of this identicon.
* *
* @return array * @return array<string, mixed>
*/ */
public function getOptions() public function getOptions(): array
{ {
$options = array(); $options = [];
if ($this->valueSet) { if ($this->valueSet) {
$options['value'] = $this->getValue(); $options['value'] = $this->getValue();
@ -142,10 +123,10 @@ class Identicon
* Sets options in this identicon by specifying an associative array of * Sets options in this identicon by specifying an associative array of
* option values. * option values.
* *
* @param array $options Options to set. * @param array<string, mixed> $options Options to set.
* @return self * @return self
*/ */
public function setOptions(array $options) public function setOptions(array $options): self
{ {
foreach ($options as $key => $value) { foreach ($options as $key => $value) {
$this->__set($key, $value); $this->__set($key, $value);
@ -153,7 +134,7 @@ class Identicon
return $this; return $this;
} }
public function __get($name) public function __get(string $name)
{ {
switch (strtolower($name)) { switch (strtolower($name)) {
case 'size': case 'size':
@ -174,7 +155,7 @@ class Identicon
} }
} }
public function __set($name, $value) public function __set(string $name, $value)
{ {
switch (strtolower($name)) { switch (strtolower($name)) {
case 'size': case 'size':
@ -204,7 +185,7 @@ class Identicon
/** /**
* Gets the size of the icon in pixels. * Gets the size of the icon in pixels.
*/ */
public function getSize() public function getSize(): int
{ {
return $this->size; return $this->size;
} }
@ -214,7 +195,7 @@ class Identicon
* *
* @param int|float|double $size The width and height of the icon. * @param int|float|double $size The width and height of the icon.
*/ */
public function setSize($size) public function setSize($size): void
{ {
if (!is_numeric($size) || $size < 1) { if (!is_numeric($size) || $size < 1) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
@ -228,13 +209,12 @@ class Identicon
/** /**
* Gets the size of the icon in pixels. * Gets the size of the icon in pixels.
*/ */
public function getEnableImageMagick() public function getEnableImageMagick(): bool
{ {
// Enable ImageMagick on PHP < 7. On PHP 7 the performance increase // Performance of using Imagick on PHP 7 and later is generally worse than using
// is not as obvious as on PHP 5. Since the ImageMagick renderer has a // the internal renderer. Because of this, default to false.
// lot of quirks, we don't want to use it unless really needed.
if ($this->enableImageMagick === null) { if ($this->enableImageMagick === null) {
return PHP_MAJOR_VERSION < 7 && extension_loaded('imagick'); return false;
} }
return $this->enableImageMagick; return $this->enableImageMagick;
@ -245,13 +225,8 @@ class Identicon
* *
* @param bool $enable true to enable ImageMagick. * @param bool $enable true to enable ImageMagick.
*/ */
public function setEnableImageMagick($enable) public function setEnableImageMagick(bool $enable): void
{ {
if (!is_bool($enable)) {
throw new \InvalidArgumentException(
"enableImageMagick can only assume boolean values. Specified value: $enable.");
}
// Verify that the Imagick extension is installed // Verify that the Imagick extension is installed
if ($enable && !extension_loaded('imagick')) { if ($enable && !extension_loaded('imagick')) {
throw new \Exception( throw new \Exception(
@ -267,7 +242,7 @@ class Identicon
* *
* @return \Jdenticon\Rendering\IconGenerator * @return \Jdenticon\Rendering\IconGenerator
*/ */
public function getIconGenerator() public function getIconGenerator(): IconGenerator
{ {
return $this->iconGenerator; return $this->iconGenerator;
} }
@ -279,7 +254,7 @@ class Identicon
* that will render the shapes of the identicon. * that will render the shapes of the identicon.
* @return \Jdenticon\Identicon * @return \Jdenticon\Identicon
*/ */
public function setIconGenerator(IconGenerator $iconGenerator) public function setIconGenerator(IconGenerator $iconGenerator): self
{ {
if ($iconGenerator === null) { if ($iconGenerator === null) {
$iconGenerator = IconGenerator::getDefaultGenerator(); $iconGenerator = IconGenerator::getDefaultGenerator();
@ -293,7 +268,7 @@ class Identicon
* *
* @return \Jdenticon\IdenticonStyle * @return \Jdenticon\IdenticonStyle
*/ */
public function getStyle() public function getStyle(): IdenticonStyle
{ {
return $this->style; return $this->style;
} }
@ -301,11 +276,11 @@ class Identicon
/** /**
* Gets or sets the style of the icon. * Gets or sets the style of the icon.
* *
* @param array|\Jdenticon\IdenticonStyle $style The new style of the icon. * @param array<string, mixed>|\Jdenticon\IdenticonStyle $style The new style of the icon.
* NULL will revert the identicon to use the default style. * NULL will revert the identicon to use the default style.
* @return self * @return self
*/ */
public function setStyle($style) public function setStyle($style): self
{ {
if ($style == null) { if ($style == null) {
$this->style = new IdenticonStyle(); $this->style = new IdenticonStyle();
@ -327,7 +302,7 @@ class Identicon
* Gets a binary string containing the hash that is used as base for this * Gets a binary string containing the hash that is used as base for this
* icon. * icon.
*/ */
public function getHash() public function getHash(): string
{ {
return $this->hash; return $this->hash;
} }
@ -338,13 +313,8 @@ class Identicon
* *
* @param string $hash Binary string containing the hash. * @param string $hash Binary string containing the hash.
*/ */
public function setHash($hash) public function setHash(string $hash): self
{ {
if (!is_string($hash)) {
throw new \InvalidArgumentException(
'An invalid $hash was passed to Identicon. ' .
'A binary string was expected.');
}
if (strlen($hash) < 6) { if (strlen($hash) < 6) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
'An invalid $hash was passed to Identicon. ' . 'An invalid $hash was passed to Identicon. ' .
@ -372,7 +342,7 @@ class Identicon
* *
* @param mixed $value Value that will be hashed. * @param mixed $value Value that will be hashed.
*/ */
public function setValue($value) public function setValue($value): self
{ {
$this->hash = sha1("$value"); $this->hash = sha1("$value");
$this->value = $value; $this->value = $value;
@ -385,7 +355,7 @@ class Identicon
* *
* @return \Jdenticon\Rendering\Rectangle * @return \Jdenticon\Rendering\Rectangle
*/ */
public function getIconBounds() public function getIconBounds(): Rectangle
{ {
// Round padding to nearest integer // Round padding to nearest integer
$padding = (int)($this->style->getPadding() * $this->size + 0.5); $padding = (int)($this->style->getPadding() * $this->size + 0.5);
@ -396,7 +366,7 @@ class Identicon
$this->size - $padding * 2); $this->size - $padding * 2);
} }
private function getRenderer($imageFormat) private function getRenderer(string $imageFormat): RendererInterface
{ {
switch (strtolower($imageFormat)) { switch (strtolower($imageFormat)) {
case 'svg': case 'svg':
@ -424,10 +394,7 @@ class Identicon
* is omitted, the rectangle is calculated from the current icon * is omitted, the rectangle is calculated from the current icon
* size and padding. * size and padding.
*/ */
public function draw( public function draw(RendererInterface $renderer, ?Rectangle $rect = null): void {
\Jdenticon\Rendering\RendererInterface $renderer,
?\Jdenticon\Rendering\Rectangle $rect = null)
{
if ($rect === null) { if ($rect === null) {
$rect = $this->getIconBounds(); $rect = $this->getIconBounds();
} }
@ -445,7 +412,7 @@ class Identicon
* @param string $imageFormat The image format of the output. * @param string $imageFormat The image format of the output.
* Supported values are 'png' and 'svg'. * Supported values are 'png' and 'svg'.
*/ */
public function displayImage($imageFormat = 'png') public function displayImage(string $imageFormat = 'png'): void
{ {
$renderer = $this->getRenderer($imageFormat); $renderer = $this->getRenderer($imageFormat);
$this->draw($renderer, $this->getIconBounds()); $this->draw($renderer, $this->getIconBounds());
@ -462,7 +429,7 @@ class Identicon
* Supported values are 'png' and 'svg'. * Supported values are 'png' and 'svg'.
* @return string * @return string
*/ */
public function getImageData($imageFormat = 'png') public function getImageData(string $imageFormat = 'png'): string
{ {
$renderer = $this->getRenderer($imageFormat); $renderer = $this->getRenderer($imageFormat);
$this->draw($renderer, $this->getIconBounds()); $this->draw($renderer, $this->getIconBounds());
@ -479,7 +446,7 @@ class Identicon
* Supported values are 'png' and 'svg'. * Supported values are 'png' and 'svg'.
* @return string * @return string
*/ */
public function getImageDataUri($imageFormat = 'png') public function getImageDataUri(string $imageFormat = 'png'): string
{ {
$renderer = $this->getRenderer($imageFormat); $renderer = $this->getRenderer($imageFormat);
$this->draw($renderer, $this->getIconBounds()); $this->draw($renderer, $this->getIconBounds());
@ -488,5 +455,3 @@ class Identicon
return "data:$mimeType;base64,$base64"; return "data:$mimeType;base64,$base64";
} }
} }

View file

@ -3,7 +3,8 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* Copyright (c) 2024 Peter Putzer
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -18,40 +19,13 @@ use Jdenticon\Color;
*/ */
class IdenticonStyle class IdenticonStyle
{ {
/** private Color $backgroundColor;
* @var \Jdenticon\Color private float $padding;
*/ private float $colorSaturation;
private $backgroundColor; private float $grayscaleSaturation;
private array $colorLightness;
/** private array $grayscaleLightness;
* @var float private ?array $hues = null;
*/
private $padding;
/**
* @var float
*/
private $colorSaturation;
/**
* @var float
*/
private $grayscaleSaturation;
/**
* @var array(float)
*/
private $colorLightness;
/**
* @var array(float)
*/
private $grayscaleLightness;
/**
* @var array(integer)
*/
private $hues;
public function __construct(?array $options = null) public function __construct(?array $options = null)
{ {
@ -72,9 +46,9 @@ class IdenticonStyle
* *
* @return array * @return array
*/ */
public function getOptions() public function getOptions(): array
{ {
$options = array(); $options = [];
$options['backgroundColor'] = $this->getBackgroundColor()->__toString(); $options['backgroundColor'] = $this->getBackgroundColor()->__toString();
$options['padding'] = $this->getPadding(); $options['padding'] = $this->getPadding();
@ -97,7 +71,7 @@ class IdenticonStyle
* @param array $options Options to set. * @param array $options Options to set.
* @return self * @return self
*/ */
public function setOptions(array $options) public function setOptions(array $options): self
{ {
foreach ($options as $key => $value) { foreach ($options as $key => $value) {
$this->__set($key, $value); $this->__set($key, $value);
@ -105,7 +79,7 @@ class IdenticonStyle
return $this; return $this;
} }
public function __get($name) public function __get(string $name)
{ {
switch (strtolower($name)) { switch (strtolower($name)) {
case 'backgroundcolor': case 'backgroundcolor':
@ -128,7 +102,7 @@ class IdenticonStyle
} }
} }
public function __set($name, $value) public function __set(string $name, $value): void
{ {
switch (strtolower($name)) { switch (strtolower($name)) {
case 'backgroundcolor': case 'backgroundcolor':
@ -161,17 +135,17 @@ class IdenticonStyle
/** /**
* Normalizes a hue to the first turn [0, 360). * Normalizes a hue to the first turn [0, 360).
* *
* @param mixed $hue * @param int|float $hue
* @return integer * @return integer
*/ */
private static function normalizeHue($hue) private static function normalizeHue($hue): int
{ {
if (!is_numeric($hue)) { if (!is_numeric($hue)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
"'$hue' is not a valid hue."); "'$hue' is not a valid hue.");
} }
$hue = $hue % 360; $hue = (int)$hue % 360;
if ($hue < 0) { if ($hue < 0) {
$hue += 360; $hue += 360;
} }
@ -182,9 +156,9 @@ class IdenticonStyle
/** /**
* Gets an array of allowed hues, or null if there are no restrictions. * Gets an array of allowed hues, or null if there are no restrictions.
* *
* @return array(int)|null * @return array<int>|null
*/ */
public function getHues() public function getHues(): ?array
{ {
return $this->hues; return $this->hues;
} }
@ -192,14 +166,14 @@ class IdenticonStyle
/** /**
* Sets the allowed hues of generated icons. * Sets the allowed hues of generated icons.
* *
* @param array(integer)|integer|null $value A hue specified in degrees, * @param array<integer>|integer|null $value A hue specified in degrees,
* or an array of hues specified in degrees. If set to null, the hue * or an array of hues specified in degrees. If set to null, the hue
* list is cleared. * list is cleared.
* @return self * @return self
*/ */
public function setHues($value) public function setHues($value): self
{ {
$hues = array(); $hues = [];
if ($value !== null) { if ($value !== null) {
if (is_array($value)) { if (is_array($value)) {
@ -220,7 +194,7 @@ class IdenticonStyle
* *
* @return float * @return float
*/ */
public function getPadding() public function getPadding(): float
{ {
return $this->padding; return $this->padding;
} }
@ -231,9 +205,9 @@ class IdenticonStyle
* @param float $value New padding in the range [0.0, 0.4]. * @param float $value New padding in the range [0.0, 0.4].
* @return self * @return self
*/ */
public function setPadding($value) public function setPadding(float $value): self
{ {
if (!is_numeric($value) || $value < 0 || $value > 0.4) { if ($value < 0 || $value > 0.4) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
"Padding '$value' out of range. ". "Padding '$value' out of range. ".
"Values in the range [0.0, 0.4] are allowed."); "Values in the range [0.0, 0.4] are allowed.");
@ -247,7 +221,7 @@ class IdenticonStyle
* *
* @return \Jdenticon\Color * @return \Jdenticon\Color
*/ */
public function getBackgroundColor() public function getBackgroundColor(): Color
{ {
return $this->backgroundColor; return $this->backgroundColor;
} }
@ -258,7 +232,7 @@ class IdenticonStyle
* @param \Jdenticon\Color|string $value New background color. * @param \Jdenticon\Color|string $value New background color.
* @return \Jdenticon\IdenticonStyle * @return \Jdenticon\IdenticonStyle
*/ */
public function setBackgroundColor($value) public function setBackgroundColor($value): self
{ {
if ($value instanceof Color) { if ($value instanceof Color) {
$this->backgroundColor = $value; $this->backgroundColor = $value;
@ -274,7 +248,7 @@ class IdenticonStyle
* *
* @return float Saturation in the range [0.0, 1.0]. * @return float Saturation in the range [0.0, 1.0].
*/ */
public function getGrayscaleSaturation() public function getGrayscaleSaturation(): float
{ {
return $this->grayscaleSaturation; return $this->grayscaleSaturation;
} }
@ -285,11 +259,9 @@ class IdenticonStyle
* @param $value float Saturation in the range [0.0, 1.0]. * @param $value float Saturation in the range [0.0, 1.0].
* @return self * @return self
*/ */
public function setGrayscaleSaturation($value) public function setGrayscaleSaturation(float $value): self
{ {
if (!is_numeric($value) || if ($value < 0 || $value > 1) {
$value < 0 || $value > 1
) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
"The grayscale saturation was invalid. ". "The grayscale saturation was invalid. ".
"Only values in the range [0.0, 1.0] are allowed."); "Only values in the range [0.0, 1.0] are allowed.");
@ -303,7 +275,7 @@ class IdenticonStyle
* *
* @return float Saturation in the range [0.0, 1.0]. * @return float Saturation in the range [0.0, 1.0].
*/ */
public function getColorSaturation() public function getColorSaturation(): float
{ {
return $this->colorSaturation; return $this->colorSaturation;
} }
@ -314,11 +286,9 @@ class IdenticonStyle
* @param $value float Saturation in the range [0.0, 1.0]. * @param $value float Saturation in the range [0.0, 1.0].
* @return self * @return self
*/ */
public function setColorSaturation($value) public function setColorSaturation(float $value): self
{ {
if (!is_numeric($value) || if ($value < 0 || $value > 1) {
$value < 0 || $value > 1
) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(
"The color saturation was invalid. ". "The color saturation was invalid. ".
"Only values in the range [0.0, 1.0] are allowed."); "Only values in the range [0.0, 1.0] are allowed.");
@ -330,9 +300,9 @@ class IdenticonStyle
/** /**
* Gets the value of the ColorLightness property. * Gets the value of the ColorLightness property.
* *
* @return array(float, float) * @return array<float>
*/ */
public function getColorLightness() public function getColorLightness(): array
{ {
return $this->colorLightness; return $this->colorLightness;
} }
@ -340,13 +310,12 @@ class IdenticonStyle
/** /**
* Sets the value of the ColorLightness property. * Sets the value of the ColorLightness property.
* *
* @param $value array(float, float) Lightness range. * @param array<float> $value Lightness range.
* @return self * @return self
*/ */
public function setColorLightness($value) public function setColorLightness(array $value): self
{ {
if (!is_array($value) || if (!array_key_exists(0, $value) ||
!array_key_exists(0, $value) ||
!array_key_exists(1, $value) || !array_key_exists(1, $value) ||
!is_numeric($value[0]) || !is_numeric($value[0]) ||
!is_numeric($value[1]) || !is_numeric($value[1]) ||
@ -358,16 +327,16 @@ class IdenticonStyle
"Please check the documentation."); "Please check the documentation.");
} }
$this->colorLightness = array((float)$value[0], (float)$value[1]); $this->colorLightness = [(float)$value[0], (float)$value[1]];
return $this; return $this;
} }
/** /**
* Gets the value of the GrayscaleLightness property. * Gets the value of the GrayscaleLightness property.
* *
* @return array(float, float) * @return array<float>
*/ */
public function getGrayscaleLightness() public function getGrayscaleLightness(): array
{ {
return $this->grayscaleLightness; return $this->grayscaleLightness;
} }
@ -375,13 +344,12 @@ class IdenticonStyle
/** /**
* Sets the value of the GrayscaleLightness property. * Sets the value of the GrayscaleLightness property.
* *
* @param $value array(float, float) Lightness range. * @param array<float> $value Lightness range.
* @return self * @return self
*/ */
public function setGrayscaleLightness($value) public function setGrayscaleLightness(array $value): self
{ {
if (!is_array($value) || if (!array_key_exists(0, $value) ||
!array_key_exists(0, $value) ||
!array_key_exists(1, $value) || !array_key_exists(1, $value) ||
!is_numeric($value[0]) || !is_numeric($value[0]) ||
!is_numeric($value[1]) || !is_numeric($value[1]) ||
@ -392,7 +360,7 @@ class IdenticonStyle
"The value passed to setGrayscaleLightness was invalid. ". "The value passed to setGrayscaleLightness was invalid. ".
"Please check the documentation."); "Please check the documentation.");
} }
$this->grayscaleLightness = array((float)$value[0], (float)$value[1]); $this->grayscaleLightness = [(float)$value[0], (float)$value[1]];
return $this; return $this;
} }
@ -403,7 +371,7 @@ class IdenticonStyle
* *
* @return \Jdenticon\Color * @return \Jdenticon\Color
*/ */
public static function getDefaultBackgroundColor() public static function getDefaultBackgroundColor(): Color
{ {
return Color::fromRgb(255, 255, 255, 255); return Color::fromRgb(255, 255, 255, 255);
} }
@ -413,7 +381,7 @@ class IdenticonStyle
* *
* @return float * @return float
*/ */
public static function getDefaultPadding() public static function getDefaultPadding(): float
{ {
return 0.08; return 0.08;
} }
@ -423,7 +391,7 @@ class IdenticonStyle
* *
* @return float * @return float
*/ */
public static function getDefaultColorSaturation() public static function getDefaultColorSaturation(): float
{ {
return 0.5; return 0.5;
} }
@ -433,7 +401,7 @@ class IdenticonStyle
* *
* @return float * @return float
*/ */
public static function getDefaultGrayscaleSaturation() public static function getDefaultGrayscaleSaturation(): float
{ {
return 0; return 0;
} }
@ -441,20 +409,20 @@ class IdenticonStyle
/** /**
* Gets the default value of the ColorLightness property. Resolves to [0.4, 0.8]. * Gets the default value of the ColorLightness property. Resolves to [0.4, 0.8].
* *
* @return array * @return array<float>
*/ */
public static function getDefaultColorLightness() public static function getDefaultColorLightness(): array
{ {
return array(0.4, 0.8); return [0.4, 0.8];
} }
/** /**
* Gets the default value of the GrayscaleLightness property. Resolves to [0.3, 0.9]. * Gets the default value of the GrayscaleLightness property. Resolves to [0.3, 0.9].
* *
* @return array * @return array<float>
*/ */
public static function getDefaultGrayscaleLightness() public static function getDefaultGrayscaleLightness(): array
{ {
return array(0.3, 0.9); return [0.3, 0.9];
} }
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -11,6 +11,9 @@
namespace Jdenticon\Rendering; namespace Jdenticon\Rendering;
use Jdenticon\Color;
use Jdenticon\Rendering\Transform;
/** /**
* Base class for rendering shapes in an identicon. Implement this class to e.g. * Base class for rendering shapes in an identicon. Implement this class to e.g.
* support a new file format that is not natively supported by Jdenticon. To * support a new file format that is not natively supported by Jdenticon. To
@ -19,8 +22,8 @@ namespace Jdenticon\Rendering;
*/ */
abstract class AbstractRenderer implements RendererInterface abstract class AbstractRenderer implements RendererInterface
{ {
private $transform; private Transform $transform;
protected $backgroundColor; protected ?Color $backgroundColor = null;
public function __construct() public function __construct()
{ {
@ -31,10 +34,10 @@ abstract class AbstractRenderer implements RendererInterface
* Sets the current transform that will be applied on all coordinates before * Sets the current transform that will be applied on all coordinates before
* being rendered to the target image. * being rendered to the target image.
* *
* @param \Jdenticon\Rendering\Transform $transform The transform to set. * @param \Jdenticon\Rendering\Transform|null $transform The transform to set.
* If NULL is specified any existing transform is removed. * If NULL is specified any existing transform is removed.
*/ */
public function setTransform(\Jdenticon\Rendering\Transform $transform) public function setTransform(?Transform $transform): void
{ {
$this->transform = $transform === null ? $this->transform = $transform === null ?
Transform::getEmpty() : $transform; Transform::getEmpty() : $transform;
@ -46,7 +49,7 @@ abstract class AbstractRenderer implements RendererInterface
* *
* @return \Jdenticon\Rendering\Transform * @return \Jdenticon\Rendering\Transform
*/ */
public function getTransform() public function getTransform(): Transform
{ {
return $this->transform; return $this->transform;
} }
@ -54,29 +57,26 @@ abstract class AbstractRenderer implements RendererInterface
/** /**
* Adds a polygon without translating its coordinates. * Adds a polygon without translating its coordinates.
* *
* @param array $points An array of the points that the polygon consists of. * @param array<\Jdenticon\Rendering\Point> $points An array of the points that the polygon consists of.
*/ */
abstract protected function addPolygonNoTransform($points); abstract protected function addPolygonNoTransform(array $points): void;
/** /**
* Adds a circle without translating its coordinates. * Adds a circle without translating its coordinates.
* *
* @param float $x The x-coordinate of the bounding rectangle * @param float $x The x-coordinate of the bounding rectangle upper-left corner.
* upper-left corner. * @param float $y The y-coordinate of the bounding rectangle upper-left corner.
* @param float $y The y-coordinate of the bounding rectangle
* upper-left corner.
* @param float $size The size of the bounding rectangle. * @param float $size The size of the bounding rectangle.
* @param bool $counterClockwise If true the circle will be drawn * @param bool $counterClockwise If true the circle will be drawn counter clockwise.
* counter clockwise.
*/ */
abstract protected function addCircleNoTransform($x, $y, $size, $counterClockwise); abstract protected function addCircleNoTransform(float $x, float $y, float $size, bool $counterClockwise): void;
/** /**
* Sets the background color of the image. * Sets the background color of the image.
* *
* @param \Jdenticon\Color $color The image background color. * @param \Jdenticon\Color $color The image background color.
*/ */
public function setBackgroundColor(\Jdenticon\Color $color) public function setBackgroundColor(Color $color): void
{ {
$this->backgroundColor = $color; $this->backgroundColor = $color;
} }
@ -86,14 +86,20 @@ abstract class AbstractRenderer implements RendererInterface
* *
* @return \Jdenticon\Color * @return \Jdenticon\Color
*/ */
public function getBackgroundColor() public function getBackgroundColor(): Color
{ {
if ($this->backgroundColor === null) {
$this->backgroundColor = Color::fromRgb(0, 0, 0, 0);
}
return $this->backgroundColor; return $this->backgroundColor;
} }
private function addPolygonCore(array $points, $invert) /**
* @param array<Point> $points
*/
private function addPolygonCore(array $points, bool $invert): void
{ {
$transformedPoints = array(); $transformedPoints = [];
foreach ($points as $point) { foreach ($points as $point) {
$transformedPoints[] = $transformedPoints[] =
$this->transform->transformPoint($point->x, $point->y); $this->transform->transformPoint($point->x, $point->y);
@ -103,8 +109,6 @@ abstract class AbstractRenderer implements RendererInterface
$transformedPoints = array_reverse($transformedPoints); $transformedPoints = array_reverse($transformedPoints);
} }
//var_dump($transformedPoints);
$this->addPolygonNoTransform($transformedPoints); $this->addPolygonNoTransform($transformedPoints);
} }
@ -118,28 +122,25 @@ abstract class AbstractRenderer implements RendererInterface
* @param bool $invert If true the area of the rectangle will be removed * @param bool $invert If true the area of the rectangle will be removed
* from the filled area. * from the filled area.
*/ */
public function addRectangle($x, $y, $width, $height, $invert = false) public function addRectangle(float $x, float $y, float $width, float $height, bool $invert = false): void
{ {
$this->addPolygonCore(array( $this->addPolygonCore([
new Point($x, $y), new Point($x, $y),
new Point($x + $width, $y), new Point($x + $width, $y),
new Point($x + $width, $y + $height), new Point($x + $width, $y + $height),
new Point($x, $y + $height), new Point($x, $y + $height),
), $invert); ], $invert);
} }
/** /**
* Adds a circle to the image. * Adds a circle to the image.
* *
* @param float $x The x-coordinate of the bounding rectangle * @param float $x The x-coordinate of the bounding rectangle upper-left corner.
* upper-left corner. * @param float $y The y-coordinate of the bounding rectangle upper-left corner.
* @param float $y The y-coordinate of the bounding rectangle
* upper-left corner.
* @param float $size The size of the bounding rectangle. * @param float $size The size of the bounding rectangle.
* @param bool $invert If true the area of the circle will be removed * @param bool $invert If true the area of the circle will be removed from the filled area.
* from the filled area.
*/ */
public function addCircle($x, $y, $size, $invert = false) public function addCircle(float $x, float $y, float $size, bool $invert = false): void
{ {
$northWest = $this->transform->transformPoint($x, $y, $size, $size); $northWest = $this->transform->transformPoint($x, $y, $size, $size);
$this->addCircleNoTransform($northWest->x, $northWest->y, $size, $invert); $this->addCircleNoTransform($northWest->x, $northWest->y, $size, $invert);
@ -148,11 +149,10 @@ abstract class AbstractRenderer implements RendererInterface
/** /**
* Adds a polygon to the image. * Adds a polygon to the image.
* *
* @param array $points Array of points that the polygon consists of. * @param array<Point> $points Array of points that the polygon consists of.
* @param bool $invert If true the area of the polygon will be removed * @param bool $invert If true the area of the polygon will be removed from the filled area.
* from the filled area.
*/ */
public function addPolygon($points, $invert = false) public function addPolygon(array $points, bool $invert = false): void
{ {
$this->addPolygonCore($points, $invert); $this->addPolygonCore($points, $invert);
} }
@ -160,25 +160,22 @@ abstract class AbstractRenderer implements RendererInterface
/** /**
* Adds a triangle to the image. * Adds a triangle to the image.
* *
* @param float $x The x-coordinate of the bounding rectangle * @param float $x The x-coordinate of the bounding rectangle upper-left corner.
* upper-left corner. * @param float $y The y-coordinate of the bounding rectangle upper-left corner.
* @param float $y The y-coordinate of the bounding rectangle
* upper-left corner.
* @param float $width The width of the bounding rectangle. * @param float $width The width of the bounding rectangle.
* @param float $height The height of the bounding rectangle. * @param float $height The height of the bounding rectangle.
* @param float $direction The direction of the 90 degree corner of the * @param int $direction The direction of the 90-degree corner of the triangle.
* triangle. * Value of {@link \Jdenticon\Rendering\TriageDirection}
* @param bool $invert If true the area of the triangle will be removed * @param bool $invert If true the area of the triangle will be removed from the filled area.
* from the filled area.
*/ */
public function addTriangle($x, $y, $width, $height, $direction, $invert = false) public function addTriangle(float $x, float $y, float $width, float $height, int $direction, bool $invert = false): void
{ {
$points = array( $points = [
new Point($x + $width, $y), new Point($x + $width, $y),
new Point($x + $width, $y + $height), new Point($x + $width, $y + $height),
new Point($x, $y + $height), new Point($x, $y + $height),
new Point($x, $y) new Point($x, $y)
); ];
array_splice($points, $direction, 1); array_splice($points, $direction, 1);
@ -188,22 +185,19 @@ abstract class AbstractRenderer implements RendererInterface
/** /**
* Adds a rhombus to the image. * Adds a rhombus to the image.
* *
* @param float $x The x-coordinate of the bounding rectangle * @param float $x The x-coordinate of the bounding rectangle upper-left corner.
* upper-left corner. * @param float $y The y-coordinate of the bounding rectangle upper-left corner.
* @param float $y The y-coordinate of the bounding rectangle
* upper-left corner.
* @param float $width The width of the bounding rectangle. * @param float $width The width of the bounding rectangle.
* @param float $height The height of the bounding rectangle. * @param float $height The height of the bounding rectangle.
* @param bool $invert If true the area of the rhombus will be removed * @param bool $invert If true the area of the rhombus will be removed from the filled area.
* from the filled area.
*/ */
public function addRhombus($x, $y, $width, $height, $invert = false) public function addRhombus(float $x, float $y, float $width, float $height, bool $invert = false): void
{ {
$this->addPolygonCore(array( $this->addPolygonCore([
new Point($x + $width / 2, $y), new Point($x + $width / 2, $y),
new Point($x + $width, $y + $height / 2), new Point($x + $width, $y + $height / 2),
new Point($x + $width / 2, $y + $height), new Point($x + $width / 2, $y + $height),
new Point($x, $y + $height / 2), new Point($x, $y + $height / 2),
), $invert); ], $invert);
} }
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -12,17 +12,18 @@
namespace Jdenticon\Rendering; namespace Jdenticon\Rendering;
use Jdenticon\Color; use Jdenticon\Color;
use Jdenticon\IdenticonStyle;
/** /**
* Specifies the colors to be used in an identicon. * Specifies the colors to be used in an identicon.
*/ */
class ColorTheme class ColorTheme
{ {
private $darkGray; private Color $darkGray;
private $midColor; private Color $midColor;
private $lightGray; private Color $lightGray;
private $lightColor; private Color $lightColor;
private $darkColor; private Color $darkColor;
/** /**
* Creates a new ColorTheme. * Creates a new ColorTheme.
@ -31,7 +32,7 @@ class ColorTheme
* @param \Jdenticon\IdenticonStyle $style The style that specifies the * @param \Jdenticon\IdenticonStyle $style The style that specifies the
* lightness and saturation of the icon. * lightness and saturation of the icon.
*/ */
public function __construct($hue, \Jdenticon\IdenticonStyle $style) public function __construct(float $hue, IdenticonStyle $style)
{ {
$grayscaleLightness = $style->getGrayscaleLightness(); $grayscaleLightness = $style->getGrayscaleLightness();
$colorLightness = $style->getColorLightness(); $colorLightness = $style->getColorLightness();
@ -60,9 +61,9 @@ class ColorTheme
* Gets a color from this color theme by index. * Gets a color from this color theme by index.
* *
* @param int $index Color index in the range [0, getCount()). * @param int $index Color index in the range [0, getCount()).
* @return Jdenticon\Color * @return \Jdenticon\Color|null
*/ */
public function getByIndex($index) public function getByIndex(int $index): ?Color
{ {
if ($index === 0) return $this->darkGray; if ($index === 0) return $this->darkGray;
if ($index === 1) return $this->midColor; if ($index === 1) return $this->midColor;
@ -77,7 +78,7 @@ class ColorTheme
* *
* @return int * @return int
*/ */
public function getCount() public function getCount(): int
{ {
return 5; return 5;
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -11,6 +11,7 @@
namespace Jdenticon\Rendering; namespace Jdenticon\Rendering;
use Jdenticon\IdenticonStyle;
use Jdenticon\Shapes\Shape; use Jdenticon\Shapes\Shape;
use Jdenticon\Shapes\ShapeCategory; use Jdenticon\Shapes\ShapeCategory;
use Jdenticon\Shapes\ShapeDefinitions; use Jdenticon\Shapes\ShapeDefinitions;
@ -24,19 +25,20 @@ use Jdenticon\Shapes\ShapeDefinitions;
*/ */
class IconGenerator class IconGenerator
{ {
private $defaultShapes; /** @var array<ShapeCategory> */
private static $instance; private array $defaultShapes;
private static ?IconGenerator $instance = null;
protected function __construct() protected function __construct()
{ {
$this->defaultShapes = array( $this->defaultShapes = [
// Sides // Sides
new ShapeCategory( new ShapeCategory(
/*$colorIndex=*/ 8, /*$colorIndex=*/ 8,
/*$shapes=*/ ShapeDefinitions::getOuterShapes(), /*$shapes=*/ ShapeDefinitions::getOuterShapes(),
/*$shapeIndex=*/ 2, /*$shapeIndex=*/ 2,
/*$rotationIndex=*/ 3, /*$rotationIndex=*/ 3,
/*$positions=*/ array(1,0, 2,0, 2,3, 1,3, 0,1, 3,1, 3,2, 0,2) /*$positions=*/ [1,0, 2,0, 2,3, 1,3, 0,1, 3,1, 3,2, 0,2]
), ),
// Corners // Corners
@ -45,7 +47,7 @@ class IconGenerator
/*$shapes=*/ ShapeDefinitions::getOuterShapes(), /*$shapes=*/ ShapeDefinitions::getOuterShapes(),
/*$shapeIndex=*/ 4, /*$shapeIndex=*/ 4,
/*$rotationIndex=*/ 5, /*$rotationIndex=*/ 5,
/*$positions=*/ array(0,0, 3,0, 3,3, 0,3) /*$positions=*/ [0,0, 3,0, 3,3, 0,3]
), ),
// Center // Center
@ -54,12 +56,12 @@ class IconGenerator
/*$shapes=*/ ShapeDefinitions::getCenterShapes(), /*$shapes=*/ ShapeDefinitions::getCenterShapes(),
/*$shapeIndex=*/ 1, /*$shapeIndex=*/ 1,
/*$rotationIndex=*/ null, /*$rotationIndex=*/ null,
/*$positions=*/ array(1,1, 2,1, 2,2, 1,2) /*$positions=*/ [1,1, 2,1, 2,2, 1,2]
) )
); ];
} }
public static function getDefaultGenerator() public static function getDefaultGenerator(): IconGenerator
{ {
if (self::$instance === null) { if (self::$instance === null) {
self::$instance = new IconGenerator(); self::$instance = new IconGenerator();
@ -73,7 +75,7 @@ class IconGenerator
* *
* @return int * @return int
*/ */
public function getCellCount() public function getCellCount(): int
{ {
return 4; return 4;
} }
@ -83,7 +85,7 @@ class IconGenerator
* *
* @return float Hue in the range [0, 1]. * @return float Hue in the range [0, 1].
*/ */
protected static function getHue($hash) protected static function getHue(string $hash): float
{ {
$value = hexdec(substr($hash, -7)); $value = hexdec(substr($hash, -7));
return $value / 0xfffffff; return $value / 0xfffffff;
@ -97,7 +99,7 @@ class IconGenerator
*/ */
private static function isDuplicate( private static function isDuplicate(
array $source, $newValue, array $source, $newValue,
array $duplicateValues) array $duplicateValues): bool
{ {
if (in_array($newValue, $duplicateValues, true)) { if (in_array($newValue, $duplicateValues, true)) {
foreach ($duplicateValues as $value) { foreach ($duplicateValues as $value) {
@ -116,7 +118,7 @@ class IconGenerator
* @param int $index The zero-based index of the octet to be returned. * @param int $index The zero-based index of the octet to be returned.
* @return int * @return int
*/ */
protected static function getOctet($hash, $index) protected static function getOctet(string $hash, int $index): int
{ {
return hexdec($hash[$index]); return hexdec($hash[$index]);
} }
@ -125,9 +127,9 @@ class IconGenerator
* Gets an array of the shape categories to be rendered in icons generated * Gets an array of the shape categories to be rendered in icons generated
* by this IconGenerator. * by this IconGenerator.
* *
* @return array * @return array<ShapeCategory>
*/ */
protected function getCategories() protected function getCategories(): array
{ {
return $this->defaultShapes; return $this->defaultShapes;
} }
@ -139,13 +141,13 @@ class IconGenerator
* @param \Jdenticon\Rendering\ColorTheme $colorTheme A color theme * @param \Jdenticon\Rendering\ColorTheme $colorTheme A color theme
* specifying the colors to be used in the icon. * specifying the colors to be used in the icon.
* @param string $hash The hash for which the shapes will be returned. * @param string $hash The hash for which the shapes will be returned.
* @return array(Jdenticon\Shapes\Shape) * @return array<\Jdenticon\Shapes\Shape>
*/ */
protected function getShapes($colorTheme, $hash) protected function getShapes(ColorTheme $colorTheme, string $hash): array
{ {
$usedColorThemeIndexes = array(); $usedColorThemeIndexes = [];
$categories = self::getCategories(); $categories = self::getCategories();
$shapes = array(); $shapes = [];
$colorCount = $colorTheme->getCount(); $colorCount = $colorTheme->getCount();
foreach ($categories as $category) { foreach ($categories as $category) {
@ -154,10 +156,10 @@ class IconGenerator
if (self::isDuplicate( if (self::isDuplicate(
// Disallow dark gray and dark color combo // Disallow dark gray and dark color combo
$usedColorThemeIndexes, $colorThemeIndex, array(0, 4)) || $usedColorThemeIndexes, $colorThemeIndex, [0, 4]) ||
self::isDuplicate( self::isDuplicate(
// Disallow light gray and light color combo // Disallow light gray and light color combo
$usedColorThemeIndexes, $colorThemeIndex, array(2, 3)) $usedColorThemeIndexes, $colorThemeIndex, [2, 3])
) { ) {
$colorThemeIndex = 1; $colorThemeIndex = 1;
} }
@ -190,7 +192,7 @@ class IconGenerator
* @param \Jdenticon\Rendering\Rectangle $rect The rectangle to be * @param \Jdenticon\Rendering\Rectangle $rect The rectangle to be
* normalized. * normalized.
*/ */
protected function normalizeRectangle(\Jdenticon\Rendering\Rectangle $rect) protected function normalizeRectangle(Rectangle $rect): Rectangle
{ {
$size = (int)min($rect->width, $rect->height); $size = (int)min($rect->width, $rect->height);
@ -216,12 +218,12 @@ class IconGenerator
* @param string $hash The hash to be used as basis for the generated icon. * @param string $hash The hash to be used as basis for the generated icon.
*/ */
protected function renderBackground( protected function renderBackground(
\Jdenticon\Rendering\RendererInterface $renderer, RendererInterface $renderer,
\Jdenticon\Rendering\Rectangle $rect, Rectangle $rect,
\Jdenticon\IdenticonStyle $style, IdenticonStyle $style,
\Jdenticon\Rendering\ColorTheme $colorTheme, ColorTheme $colorTheme,
$hash) string $hash
{ ): void {
$renderer->setBackgroundColor($style->getBackgroundColor()); $renderer->setBackgroundColor($style->getBackgroundColor());
} }
@ -237,12 +239,12 @@ class IconGenerator
* @param string $hash The hash to be used as basis for the generated icon. * @param string $hash The hash to be used as basis for the generated icon.
*/ */
protected function renderForeground( protected function renderForeground(
\Jdenticon\Rendering\RendererInterface $renderer, RendererInterface $renderer,
\Jdenticon\Rendering\Rectangle $rect, Rectangle $rect,
\Jdenticon\IdenticonStyle $style, IdenticonStyle $style,
\Jdenticon\Rendering\ColorTheme $colorTheme, ColorTheme $colorTheme,
$hash) string $hash
{ ): void {
// Ensure rect is quadratic and a multiple of the cell count // Ensure rect is quadratic and a multiple of the cell count
$normalizedRect = $this->normalizeRectangle($rect); $normalizedRect = $this->normalizeRectangle($rect);
$cellSize = $normalizedRect->width / $this->getCellCount(); $cellSize = $normalizedRect->width / $this->getCellCount();
@ -259,7 +261,7 @@ class IconGenerator
$normalizedRect->y + $shape->positions[$i + 1] * $cellSize, $normalizedRect->y + $shape->positions[$i + 1] * $cellSize,
$cellSize, $rotation++ % 4)); $cellSize, $rotation++ % 4));
$shape->definition->__invoke($renderer, $cellSize, $i / 2); call_user_func($shape->definition, $renderer, $cellSize, $i / 2);
} }
$renderer->endShape(); $renderer->endShape();
@ -276,11 +278,11 @@ class IconGenerator
* @param string $hash The hash to be used as basis for the generated icon. * @param string $hash The hash to be used as basis for the generated icon.
*/ */
public function generate( public function generate(
\Jdenticon\Rendering\RendererInterface $renderer, RendererInterface $renderer,
\Jdenticon\Rendering\Rectangle $rect, Rectangle $rect,
\Jdenticon\IdenticonStyle $style, IdenticonStyle $style,
$hash) string $hash
{ ): void {
$hue = self::getHue($hash); $hue = self::getHue($hash);
$colorTheme = new ColorTheme($hue, $style); $colorTheme = new ColorTheme($hue, $style);

View file

@ -1,6 +1,8 @@
<?php <?php
namespace Jdenticon\Rendering; namespace Jdenticon\Rendering;
use Jdenticon\Color;
class ImagickRendererLine class ImagickRendererLine
{ {
/** /**
@ -10,8 +12,9 @@ class ImagickRendererLine
* @param float $y0 Vector start y coordinate. * @param float $y0 Vector start y coordinate.
* @param float $x1 Vector end x coordinate. * @param float $x1 Vector end x coordinate.
* @param float $y1 Vector end y coordinate. * @param float $y1 Vector end y coordinate.
* @return self
*/ */
public static function fromVector($x0, $y0, $x1, $y1) public static function fromVector(float $x0, float $y0, float $x1, float $y1): self
{ {
$line = new ImagickRendererLine(); $line = new ImagickRendererLine();
@ -29,7 +32,7 @@ class ImagickRendererLine
* *
* @param float $distance The number of pixels to move the line. * @param float $distance The number of pixels to move the line.
*/ */
public function moveRight($distance) public function moveRight(float $distance): void
{ {
// Ortogonal direction vector // Ortogonal direction vector
$rx = -$this->ry; $rx = -$this->ry;
@ -44,12 +47,12 @@ class ImagickRendererLine
/** /**
* Computes the point at which two lines intersect. * Computes the point at which two lines intersect.
* *
* @param ImagickRendererLine $l1
* @param ImagickRendererLine $l2
* @return Point|null * @return Point|null
*/ */
public static function intersection( public static function intersection(ImagickRendererLine $l1, ImagickRendererLine $l2): ?Point
ImagickRendererLine $l1, {
ImagickRendererLine $l2
) {
$rs = $l1->rx * $l2->ry - $l1->ry * $l2->rx; $rs = $l1->rx * $l2->ry - $l1->ry * $l2->rx;
if ($rs == 0) { if ($rs == 0) {
@ -66,25 +69,21 @@ class ImagickRendererLine
/** /**
* X coordiate of a point on the line. * X coordiate of a point on the line.
* @var float
*/ */
public $Px; public float $Px;
/** /**
* Y coordiate of a point on the line. * Y coordiate of a point on the line.
* @var float
*/ */
public $Py; public float $Py;
/** /**
* X component of the direction vector. * X component of the direction vector.
* @var float
*/ */
public $rx; public float $rx;
/** /**
* Y component of the direction vector. * Y component of the direction vector.
* @var float
*/ */
public $ry; public float $ry;
} }
/** /**
@ -108,17 +107,17 @@ class ImagickRendererLine
*/ */
class ImagickRenderer extends AbstractRenderer class ImagickRenderer extends AbstractRenderer
{ {
private $draw; private \ImagickDraw $draw;
private $polygon; private array $polygon;
private $width; private int $width;
private $height; private int $height;
/** /**
* This constant is added to all coordinates to avoid white pixels * This constant is added to all coordinates to avoid white pixels
* that sometimes appear near edge intersections when a polygon including * that sometimes appear near edge intersections when a polygon including
* its 1/2 invisible border is perfectly aligned to the pixel grid. * its 1/2 invisible border is perfectly aligned to the pixel grid.
*/ */
const PREVENT_WHITE_PIXELS_OFFSET = -0.00013; private const PREVENT_WHITE_PIXELS_OFFSET = -0.00013;
/** /**
* Creates an instance of the class ImagickRenderer. * Creates an instance of the class ImagickRenderer.
@ -126,7 +125,7 @@ class ImagickRenderer extends AbstractRenderer
* @param int $width The width of the icon in pixels. * @param int $width The width of the icon in pixels.
* @param int $height The height of the icon in pixels. * @param int $height The height of the icon in pixels.
*/ */
public function __construct($width, $height) public function __construct(int $width, int $height)
{ {
parent::__construct(); parent::__construct();
$this->draw = new \ImagickDraw(); $this->draw = new \ImagickDraw();
@ -141,7 +140,7 @@ class ImagickRenderer extends AbstractRenderer
* *
* @return string * @return string
*/ */
public function getMimeType() public function getMimeType(): string
{ {
return 'image/png'; return 'image/png';
} }
@ -149,15 +148,12 @@ class ImagickRenderer extends AbstractRenderer
/** /**
* Adds a circle without translating its coordinates. * Adds a circle without translating its coordinates.
* *
* @param float $x The x-coordinate of the bounding rectangle * @param float $x The x-coordinate of the bounding rectangle upper-left corner.
* upper-left corner. * @param float $y The y-coordinate of the bounding rectangle upper-left corner.
* @param float $y The y-coordinate of the bounding rectangle
* upper-left corner.
* @param float $size The size of the bounding rectangle. * @param float $size The size of the bounding rectangle.
* @param bool $counterClockwise If true the circle will be drawn * @param bool $counterClockwise If true the circle will be drawn counter clockwise.
* counter clockwise.
*/ */
protected function addCircleNoTransform($x, $y, $size, $counterClockwise) protected function addCircleNoTransform(float $x, float $y, float $size, bool $counterClockwise): void
{ {
if ($counterClockwise) { if ($counterClockwise) {
$x -= $size + 0.5; $x -= $size + 0.5;
@ -179,9 +175,9 @@ class ImagickRenderer extends AbstractRenderer
/** /**
* Adds a polygon without translating its coordinates. * Adds a polygon without translating its coordinates.
* *
* @param array $points An array of the points that the polygon consists of. * @param array<Point> $points An array of the points that the polygon consists of.
*/ */
protected function addPolygonNoTransform($points) protected function addPolygonNoTransform(array $points): void
{ {
$firstPoint = $points[0]; $firstPoint = $points[0];
$lastPoint = end($points); $lastPoint = end($points);
@ -209,7 +205,7 @@ class ImagickRenderer extends AbstractRenderer
// ImageMagick draws all polygons 1 pixel too large. To prevent this, // ImageMagick draws all polygons 1 pixel too large. To prevent this,
// shrink polygons by 1 pixel. // shrink polygons by 1 pixel.
$lines = array(); $lines = [];
$previousPoint = null; $previousPoint = null;
// Transform all edges to lines. // Transform all edges to lines.
@ -259,7 +255,7 @@ class ImagickRenderer extends AbstractRenderer
* *
* @param \Jdenticon\Color $color The color of the shape. * @param \Jdenticon\Color $color The color of the shape.
*/ */
public function beginShape(\Jdenticon\Color $color) public function beginShape(Color $color): void
{ {
$this->draw->setFillColor($color->__toString()); $this->draw->setFillColor($color->__toString());
$this->draw->pathStart(); $this->draw->pathStart();
@ -268,7 +264,7 @@ class ImagickRenderer extends AbstractRenderer
/** /**
* Ends the currently drawn shape. * Ends the currently drawn shape.
*/ */
public function endShape() public function endShape(): void
{ {
$this->draw->pathFinish(); $this->draw->pathFinish();
} }
@ -278,7 +274,7 @@ class ImagickRenderer extends AbstractRenderer
* *
* @return string * @return string
*/ */
public function getData() public function getData(): string
{ {
$imagick = new \Imagick(); $imagick = new \Imagick();
$imagick->newImage($this->width, $this->height, $this->backgroundColor->__toString()); $imagick->newImage($this->width, $this->height, $this->backgroundColor->__toString());

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -12,14 +12,16 @@
namespace Jdenticon\Rendering; namespace Jdenticon\Rendering;
use Jdenticon\Canvas\Canvas; use Jdenticon\Canvas\Canvas;
use Jdenticon\Canvas\CanvasContext;
use Jdenticon\Color;
/** /**
* Renders icons as PNG using the internal vector rasterizer. * Renders icons as PNG using the internal vector rasterizer.
*/ */
class InternalPngRenderer extends AbstractRenderer class InternalPngRenderer extends AbstractRenderer
{ {
private $canvas; private Canvas $canvas;
private $ctx; private CanvasContext $ctx;
/** /**
* Creates an instance of the class ImagickRenderer. * Creates an instance of the class ImagickRenderer.
@ -27,7 +29,7 @@ class InternalPngRenderer extends AbstractRenderer
* @param int $width The width of the icon in pixels. * @param int $width The width of the icon in pixels.
* @param int $height The height of the icon in pixels. * @param int $height The height of the icon in pixels.
*/ */
public function __construct($width, $height) public function __construct(int $width, int $height)
{ {
parent::__construct(); parent::__construct();
$this->canvas = new Canvas($width, $height); $this->canvas = new Canvas($width, $height);
@ -39,7 +41,7 @@ class InternalPngRenderer extends AbstractRenderer
* *
* @return string * @return string
*/ */
public function getMimeType() public function getMimeType(): string
{ {
return 'image/png'; return 'image/png';
} }
@ -55,7 +57,7 @@ class InternalPngRenderer extends AbstractRenderer
* @param bool $counterClockwise If true the circle will be drawn * @param bool $counterClockwise If true the circle will be drawn
* counter clockwise. * counter clockwise.
*/ */
protected function addCircleNoTransform($x, $y, $size, $counterClockwise) protected function addCircleNoTransform(float $x, float $y, float $size, bool $counterClockwise): void
{ {
$radius = $size / 2; $radius = $size / 2;
$this->ctx->moveTo($x + $size, $y + $radius); $this->ctx->moveTo($x + $size, $y + $radius);
@ -69,9 +71,9 @@ class InternalPngRenderer extends AbstractRenderer
/** /**
* Adds a polygon without translating its coordinates. * Adds a polygon without translating its coordinates.
* *
* @param array $points An array of the points that the polygon consists of. * @param array<Point> $points An array of the points that the polygon consists of.
*/ */
protected function addPolygonNoTransform($points) protected function addPolygonNoTransform(array $points): void
{ {
$pointCount = count($points); $pointCount = count($points);
$this->ctx->moveTo($points[0]->x, $points[0]->y); $this->ctx->moveTo($points[0]->x, $points[0]->y);
@ -86,7 +88,7 @@ class InternalPngRenderer extends AbstractRenderer
* *
* @param \Jdenticon\Color $color The background color. * @param \Jdenticon\Color $color The background color.
*/ */
public function setBackgroundColor(\Jdenticon\Color $color) public function setBackgroundColor(Color $color): void
{ {
parent::setBackgroundColor($color); parent::setBackgroundColor($color);
$this->canvas->backColor = $this->backgroundColor->toRgba(); $this->canvas->backColor = $this->backgroundColor->toRgba();
@ -97,7 +99,7 @@ class InternalPngRenderer extends AbstractRenderer
* *
* @param \Jdenticon\Color $color The color of the shape. * @param \Jdenticon\Color $color The color of the shape.
*/ */
public function beginShape(\Jdenticon\Color $color) public function beginShape(Color $color): void
{ {
$this->ctx->fillStyle = $color->toRgba(); $this->ctx->fillStyle = $color->toRgba();
$this->ctx->beginPath(); $this->ctx->beginPath();
@ -106,7 +108,7 @@ class InternalPngRenderer extends AbstractRenderer
/** /**
* Ends the currently drawn shape. * Ends the currently drawn shape.
*/ */
public function endShape() public function endShape(): void
{ {
$this->ctx->fill(); $this->ctx->fill();
} }
@ -116,8 +118,8 @@ class InternalPngRenderer extends AbstractRenderer
* *
* @return string * @return string
*/ */
public function getData() public function getData(): string
{ {
return $this->canvas->toPng(array('Software' => 'Jdenticon')); return $this->canvas->toPng(['Software' => 'Jdenticon']);
} }
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -22,7 +22,7 @@ class Point
* @param float $x X coordinate. * @param float $x X coordinate.
* @param float $y Y coordinate. * @param float $y Y coordinate.
*/ */
public function __construct($x, $y) public function __construct(float $x, float $y)
{ {
$this->x = $x; $this->x = $x;
$this->y = $y; $this->y = $y;
@ -30,25 +30,19 @@ class Point
/** /**
* The X coordinate of this point. * The X coordinate of this point.
*
* @var float
*/ */
public $x; public float $x;
/** /**
* The Y coordinate of this point. * The Y coordinate of this point.
*
* @var float
*/ */
public $y; public float $y;
/** /**
* Gets a string representation of the point. * Gets a string representation of the point.
*
* @return string
*/ */
public function __toString() public function __toString(): string
{ {
return $this->x + ", " + $this->y; return $this->x . ", " . $this->y;
} }
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -18,29 +18,23 @@ class Rectangle
{ {
/** /**
* The X coordinate of the left side of the rectangle. * The X coordinate of the left side of the rectangle.
*
* @var float
*/ */
public $x; public float $x;
/** /**
* The Y coordinate of the top side of the rectangle. * The Y coordinate of the top side of the rectangle.
*
* @var float
*/ */
public $y; public float $y;
/** /**
* The width of the rectangle. * The width of the rectangle.
* @var float
*/ */
public $width; public float $width;
/** /**
* The height of the rectangle. * The height of the rectangle.
* @var float
*/ */
public $height; public float $height;
/** /**
* Creates a new Rectangle. * Creates a new Rectangle.
@ -50,7 +44,7 @@ class Rectangle
* @param float $width The width of the rectangle. * @param float $width The width of the rectangle.
* @param float $height The height of the rectangle. * @param float $height The height of the rectangle.
*/ */
public function __construct($x, $y, $width, $height) public function __construct(float $x, float $y, float $width, float $height)
{ {
$this->x = $x; $this->x = $x;
$this->y = $y; $this->y = $y;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -25,7 +25,7 @@ interface RendererInterface
* @param \Jdenticon\Rendering\Transform $transform The transform to set. * @param \Jdenticon\Rendering\Transform $transform The transform to set.
* If NULL is specified any existing transform is removed. * If NULL is specified any existing transform is removed.
*/ */
public function setTransform(\Jdenticon\Rendering\Transform $transform); public function setTransform(Transform $transform): void;
/** /**
* Gets the current transform that will be applied on all coordinates before * Gets the current transform that will be applied on all coordinates before
@ -33,40 +33,40 @@ interface RendererInterface
* *
* @return \Jdenticon\Rendering\Transform * @return \Jdenticon\Rendering\Transform
*/ */
public function getTransform(); public function getTransform(): Transform;
/** /**
* Sets the background color of the image. * Sets the background color of the image.
* *
* @param \Jdenticon\Color $color The image background color. * @param \Jdenticon\Color $color The image background color.
*/ */
public function setBackgroundColor(Color $color); public function setBackgroundColor(Color $color): void;
/** /**
* Gets the background color of the image. * Gets the background color of the image.
* *
* @return \Jdenticon\Color * @return \Jdenticon\Color
*/ */
public function getBackgroundColor(); public function getBackgroundColor(): Color;
/** /**
* Gets the MIME type of the renderer output. * Gets the MIME type of the renderer output.
* *
* @return string * @return string
*/ */
public function getMimeType(); public function getMimeType(): string;
/** /**
* Begins a new shape. The shape should be ended with a call to endShape. * Begins a new shape. The shape should be ended with a call to endShape.
* *
* @param \Jdenticon\Color $color The color of the shape. * @param \Jdenticon\Color $color The color of the shape.
*/ */
public function beginShape(Color $color); public function beginShape(Color $color): void;
/** /**
* Ends the currently drawn shape. * Ends the currently drawn shape.
*/ */
public function endShape(); public function endShape(): void;
/** /**
* Adds a rectangle to the image. * Adds a rectangle to the image.
@ -78,7 +78,7 @@ interface RendererInterface
* @param bool $invert If true the area of the rectangle will be removed * @param bool $invert If true the area of the rectangle will be removed
* from the filled area. * from the filled area.
*/ */
public function addRectangle($x, $y, $width, $height, $invert = false); public function addRectangle(float $x, float $y, float $width, float $height, bool $invert = false): void;
/** /**
* Adds a circle to the image. * Adds a circle to the image.
@ -91,16 +91,16 @@ interface RendererInterface
* @param bool $invert If true the area of the circle will be removed from * @param bool $invert If true the area of the circle will be removed from
* the filled area. * the filled area.
*/ */
public function addCircle($x, $y, $size, $invert = false); public function addCircle(float $x, float $y, float $size, bool $invert = false): void;
/** /**
* Adds a polygon to the image. * Adds a polygon to the image.
* *
* @param array $points Array of points that the polygon consists of. * @param array<Point> $points Array of points that the polygon consists of.
* @param bool $invert If true the area of the polygon will be removed from * @param bool $invert If true the area of the polygon will be removed from
* the filled area. * the filled area.
*/ */
public function addPolygon($points, $invert = false); public function addPolygon(array $points, bool $invert = false): void;
/** /**
* Adds a triangle to the image. * Adds a triangle to the image.
@ -111,12 +111,12 @@ interface RendererInterface
* upper-left corner. * upper-left corner.
* @param float $width The width of the bounding rectangle. * @param float $width The width of the bounding rectangle.
* @param float $height The height of the bounding rectangle. * @param float $height The height of the bounding rectangle.
* @param float $direction The direction of the 90 degree corner of * @param int $direction The direction of the 90 degree corner of
* the triangle. * the triangle. Value of {@link \Jdenticon\Rendering\TriageDirection}
* @param bool $invert If true the area of the triangle will be removed * @param bool $invert If true the area of the triangle will be removed
* from the filled area. * from the filled area.
*/ */
public function addTriangle($x, $y, $width, $height, $direction, $invert = false); public function addTriangle(float $x, float $y, float $width, float $height, int $direction, bool $invert = false): void;
/** /**
* Adds a rhombus to the image. * Adds a rhombus to the image.
@ -130,12 +130,12 @@ interface RendererInterface
* @param bool $invert If true the area of the rhombus will be removed * @param bool $invert If true the area of the rhombus will be removed
* from the filled area. * from the filled area.
*/ */
public function addRhombus($x, $y, $width, $height, $invert = false); public function addRhombus(float $x, float $y, float $width, float $height, bool $invert = false): void;
/** /**
* Gets the output from the renderer. * Gets the output from the renderer.
* *
* @return string * @return string
*/ */
public function getData(); public function getData(): string;
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -16,7 +16,7 @@ namespace Jdenticon\Rendering;
*/ */
class SvgPath class SvgPath
{ {
private $dataString; private string $dataString;
public function __construct() public function __construct()
{ {
@ -33,7 +33,7 @@ class SvgPath
* clockwise. This affects the rendering since the evenodd filling rule * clockwise. This affects the rendering since the evenodd filling rule
* is used by Jdenticon. * is used by Jdenticon.
*/ */
public function addCircle($x, $y, $size, $counterClockwise) public function addCircle(float $x, float $y, float $size, bool $counterClockwise): void
{ {
$sweepFlag = $counterClockwise ? '0' : '1'; $sweepFlag = $counterClockwise ? '0' : '1';
$radiusAsString = number_format($size / 2, 2, '.', ''); $radiusAsString = number_format($size / 2, 2, '.', '');
@ -50,10 +50,10 @@ class SvgPath
/** /**
* Adds a polygon to the SVG. * Adds a polygon to the SVG.
* *
* @param array(\Jdenticon\Rendering\Point) $points The corners of the * @param array<\Jdenticon\Rendering\Point> $points The corners of the
* polygon. * polygon.
*/ */
public function addPolygon($points) public function addPolygon(array $points): void
{ {
$pointCount = count($points); $pointCount = count($points);
@ -75,7 +75,7 @@ class SvgPath
* *
* @return string * @return string
*/ */
public function __toString() public function __toString(): string
{ {
return $this->dataString; return $this->dataString;
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -11,15 +11,18 @@
namespace Jdenticon\Rendering; namespace Jdenticon\Rendering;
use Jdenticon\Color;
/** /**
* Renders icons as SVG paths. * Renders icons as SVG paths.
*/ */
class SvgRenderer extends AbstractRenderer class SvgRenderer extends AbstractRenderer
{ {
private $pathsByColor = array(); /** @var array<string, SvgPath> */
private $path; private array $pathsByColor = [];
private $width; private ?SvgPath $path = null;
private $height; private int $width;
private int $height;
/** /**
* Creates a new SvgRenderer. * Creates a new SvgRenderer.
@ -27,7 +30,7 @@ class SvgRenderer extends AbstractRenderer
* @param int $width The width of the icon in pixels. * @param int $width The width of the icon in pixels.
* @param int $height The height of the icon in pixels. * @param int $height The height of the icon in pixels.
*/ */
public function __construct($width, $height) public function __construct(int $width, int $height)
{ {
$this->width = $width; $this->width = $width;
$this->height = $height; $this->height = $height;
@ -38,7 +41,7 @@ class SvgRenderer extends AbstractRenderer
* *
* @return string * @return string
*/ */
public function getMimeType() public function getMimeType(): string
{ {
return 'image/svg+xml'; return 'image/svg+xml';
} }
@ -54,7 +57,7 @@ class SvgRenderer extends AbstractRenderer
* @param bool $counterClockwise If true the circle will be drawn * @param bool $counterClockwise If true the circle will be drawn
* counter clockwise. * counter clockwise.
*/ */
protected function addCircleNoTransform($x, $y, $size, $counterClockwise) protected function addCircleNoTransform(float $x, float $y, float $size, bool $counterClockwise): void
{ {
$this->path->addCircle($x, $y, $size, $counterClockwise); $this->path->addCircle($x, $y, $size, $counterClockwise);
} }
@ -62,9 +65,9 @@ class SvgRenderer extends AbstractRenderer
/** /**
* Adds a polygon without translating its coordinates. * Adds a polygon without translating its coordinates.
* *
* @param array $points An array of the points that the polygon consists of. * @param array<Point> $points An array of the points that the polygon consists of.
*/ */
protected function addPolygonNoTransform($points) protected function addPolygonNoTransform(array $points): void
{ {
$this->path->addPolygon($points); $this->path->addPolygon($points);
} }
@ -74,7 +77,7 @@ class SvgRenderer extends AbstractRenderer
* *
* @param \Jdenticon\Color $color The color of the shape. * @param \Jdenticon\Color $color The color of the shape.
*/ */
public function beginShape(\Jdenticon\Color $color) public function beginShape(Color $color): void
{ {
$colorString = $color->toHexString(6); $colorString = $color->toHexString(6);
@ -89,7 +92,7 @@ class SvgRenderer extends AbstractRenderer
/** /**
* Ends the currently drawn shape. * Ends the currently drawn shape.
*/ */
public function endShape() public function endShape(): void
{ {
} }
@ -98,8 +101,9 @@ class SvgRenderer extends AbstractRenderer
* *
* @param bool $fragment If true an SVG string without the root svg element * @param bool $fragment If true an SVG string without the root svg element
* will be rendered. * will be rendered.
* @return string
*/ */
public function getData($fragment = false) public function getData(bool $fragment = false): string
{ {
$svg = ''; $svg = '';
$widthAsString = number_format($this->width, 0, '.', ''); $widthAsString = number_format($this->width, 0, '.', '');

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -16,10 +16,10 @@ namespace Jdenticon\Rendering;
*/ */
class Transform class Transform
{ {
private $x; private float $x;
private $y; private float $y;
private $size; private float $size;
private $rotation; private int $rotation;
/** /**
* Creates a new Transform. * Creates a new Transform.
@ -29,10 +29,10 @@ class Transform
* @param float $y The y-coordinate of the upper left corner of the * @param float $y The y-coordinate of the upper left corner of the
* transformed rectangle. * transformed rectangle.
* @param float $size The size of the transformed rectangle. * @param float $size The size of the transformed rectangle.
* @param integer $rotation Rotation specified as * @param int $rotation Rotation specified as
* 0 = 0 rad, 1 = 0.5π rad, 2 = π rad, 3 = 1.5π rad. * 0 = 0 rad, 1 = 0.5π rad, 2 = π rad, 3 = 1.5π rad.
*/ */
public function __construct($x, $y, $size, $rotation) public function __construct(float $x, float $y, float $size, int $rotation)
{ {
$this->x = $x; $this->x = $x;
$this->y = $y; $this->y = $y;
@ -45,7 +45,7 @@ class Transform
* *
* @return \Jdenticon\Rendering\Transform * @return \Jdenticon\Rendering\Transform
*/ */
public static function getEmpty() public static function getEmpty(): Transform
{ {
return new Transform(0, 0, 0, 0); return new Transform(0, 0, 0, 0);
} }
@ -64,7 +64,7 @@ class Transform
* corner of the transformed rectangle. * corner of the transformed rectangle.
* @return \Jdenticon\Rendering\Point * @return \Jdenticon\Rendering\Point
*/ */
public function transformPoint($x, $y, $width = 0, $height = 0) public function transformPoint(float $x, float $y, float $width = 0, float $height = 0): Point
{ {
$right = $this->x + $this->size; $right = $this->x + $this->size;
$bottom = $this->y + $this->size; $bottom = $this->y + $this->size;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -19,17 +19,17 @@ class TriangleDirection
/** /**
* The 90 degree angle is pointing to South West. * The 90 degree angle is pointing to South West.
*/ */
const SOUTH_WEST = 0; public const SOUTH_WEST = 0;
/** /**
* The 90 degree angle is pointing to North West. * The 90 degree angle is pointing to North West.
*/ */
const NORTH_WEST = 1; public const NORTH_WEST = 1;
/** /**
* The 90 degree angle is pointing to North East. * The 90 degree angle is pointing to North East.
*/ */
const NORTH_EAST = 2; public const NORTH_EAST = 2;
/** /**
* The 90 degree angle is pointing to South East. * The 90 degree angle is pointing to South East.
*/ */
const SOUTH_EAST = 3; public const SOUTH_EAST = 3;
} }

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -11,6 +11,8 @@
namespace Jdenticon\Shapes; namespace Jdenticon\Shapes;
use Jdenticon\Color;
/** /**
* Represents a shape to be rendered in an icon. These instances are * Represents a shape to be rendered in an icon. These instances are
* hash specific. * hash specific.
@ -20,39 +22,35 @@ class Shape
/** /**
* The shape definition to be used to render the shape. * The shape definition to be used to render the shape.
* *
* @var function( * @var callable(
* \Jdenticon\Rendering\RendererInterface $renderer, * \Jdenticon\Rendering\RendererInterface $renderer,
* \Jdenticon\Shapes\ShapePosition $cell, * int $cell,
* int $index) * int $index): void
*/ */
public $definition; public $definition;
/** /**
* The fill color of the shape. * The fill color of the shape.
*
* @var Jdenticon\Color
*/ */
public $color; public Color $color;
/** /**
* The positions in which the shape will be rendered. * The positions in which the shape will be rendered. Interleaved x and y cell coordinates.
* *
* @var array(\Jdenticon\Shapes\ShapePosition) * @var array<int>
*/ */
public $positions; public array $positions;
/** /**
* The rotation index of the icon in the first position. * The rotation index of the icon in the first position.
*
* @var int
*/ */
public $startRotationIndex; public int $startRotationIndex;
public function __construct( public function __construct(
$definition, callable $definition,
\Jdenticon\Color $color, Color $color,
array $positions, array $positions,
$startRotationIndex) int $startRotationIndex)
{ {
$this->definition = $definition; $this->definition = $definition;
$this->color = $color; $this->color = $color;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -20,46 +20,44 @@ class ShapeCategory
/** /**
* The index of the hash octet determining the color of shapes in this * The index of the hash octet determining the color of shapes in this
* category. * category.
*
* @var int
*/ */
public $colorIndex; public int $colorIndex;
/** /**
* A list of possible shape definitions in this category. * A list of possible shape definitions in this category.
* *
* @var array(function( * @var array<callable(
* \Jdenticon\Rendering\RendererInterface $renderer, * \Jdenticon\Rendering\RendererInterface $renderer,
* \Jdenticon\Shapes\ShapePosition $cell, * int $cell,
* int $index)) * int $index): void>
*/ */
public $shapes; public array $shapes;
/** /**
* The index of the hash octet determining which of the shape definitions * The index of the hash octet determining which of the shape definitions
* that will be used for a particular hash. * that will be used for a particular hash.
*
* @var int
*/ */
public $shapeIndex; public int $shapeIndex;
/** /**
* The index of the hash octet determining the rotation index of the shape * The index of the hash octet determining the rotation index of the shape
* in the first position. * in the first position.
*
* @var int
*/ */
public $rotationIndex; public ?int $rotationIndex;
/** /**
* The positions in which the shapes of this category will be rendered. * The positions in which the shapes of this category will be rendered.
* *
* @var array(int) * @var array<int>
*/ */
public $positions; public array $positions;
public function __construct( public function __construct(
$colorIndex, array $shapes, $shapeIndex, $rotationIndex, array $positions) int $colorIndex,
array $shapes,
int $shapeIndex,
?int $rotationIndex,
array $positions)
{ {
$this->colorIndex = $colorIndex; $this->colorIndex = $colorIndex;
$this->shapes = $shapes; $this->shapes = $shapes;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for PHP. * This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/ * https://github.com/dmester/jdenticon-php/
* *
* Copyright (c) 2018 Daniel Mester Pirttijärvi * Copyright (c) 2025 Daniel Mester Pirttijärvi
* *
* For full license information, please see the LICENSE file that was * For full license information, please see the LICENSE file that was
* distributed with this source code. * distributed with this source code.
@ -12,6 +12,7 @@
namespace Jdenticon\Shapes; namespace Jdenticon\Shapes;
use Jdenticon\Rendering\Point; use Jdenticon\Rendering\Point;
use Jdenticon\Rendering\RendererInterface;
use Jdenticon\Rendering\TriangleDirection; use Jdenticon\Rendering\TriangleDirection;
/** /**
@ -19,16 +20,16 @@ use Jdenticon\Rendering\TriangleDirection;
*/ */
class ShapeDefinitions class ShapeDefinitions
{ {
private static $outerShapes; private static ?array $outerShapes = null;
private static $centerShapes; private static ?array $centerShapes = null;
/** /**
* Gets an array of all possible outer shapes. Do not modify the returned * Gets an array of all possible outer shapes. Do not modify the returned
* array. * array.
* *
* @return array(\Jdenticon\Rendering\Shape) * @return array<callable>
*/ */
public static function getOuterShapes() public static function getOuterShapes(): array
{ {
if (self::$outerShapes === null) { if (self::$outerShapes === null) {
self::$outerShapes = self::createOuterShapes(); self::$outerShapes = self::createOuterShapes();
@ -40,9 +41,9 @@ class ShapeDefinitions
* Gets an array of all possible center shapes. Do not modify the returned * Gets an array of all possible center shapes. Do not modify the returned
* array. * array.
* *
* @return array(\Jdenticon\Rendering\Shape) * @return array<callable>
*/ */
public static function getCenterShapes() public static function getCenterShapes(): array
{ {
if (self::$centerShapes === null) { if (self::$centerShapes === null) {
self::$centerShapes = self::createCenterShapes(); self::$centerShapes = self::createCenterShapes();
@ -50,58 +51,60 @@ class ShapeDefinitions
return self::$centerShapes; return self::$centerShapes;
} }
private static function createOuterShapes() /**
{ * Creates an array of outer shape definitions.
return array( *
function ($renderer, $cell, $index) * @return array<callable>
*/
private static function createOuterShapes(): array
{ {
return [
function (RendererInterface $renderer, int $cell, int $index): void {
$renderer->addTriangle(0, 0, $cell, $cell, 0); $renderer->addTriangle(0, 0, $cell, $cell, 0);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$renderer->addTriangle(0, $cell / 2, $cell, $cell / 2, 0); $renderer->addTriangle(0, $cell / 2, $cell, $cell / 2, 0);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$renderer->addRhombus(0, 0, $cell, $cell); $renderer->addRhombus(0, 0, $cell, $cell);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$m = $cell / 6; $m = $cell / 6;
$renderer->addCircle($m, $m, $cell - 2 * $m); $renderer->addCircle($m, $m, $cell - 2 * $m);
} }
); ];
} }
private static function createCenterShapes() /**
{ * Creates an array of center shape definitions.
return array( *
function ($renderer, $cell, $index) * @return array<callable>
*/
private static function createCenterShapes(): array
{ {
return [
function (RendererInterface $renderer, int $cell, int $index): void {
$k = $cell * 0.42; $k = $cell * 0.42;
$renderer->addPolygon(array( $renderer->addPolygon([
new Point(0, 0), new Point(0, 0),
new Point($cell, 0), new Point($cell, 0),
new Point($cell, $cell - $k * 2), new Point($cell, $cell - $k * 2),
new Point($cell - $k, $cell), new Point($cell - $k, $cell),
new Point(0, $cell) new Point(0, $cell)
)); ]);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$w = (int)($cell * 0.5); $w = (int)($cell * 0.5);
$h = (int)($cell * 0.8); $h = (int)($cell * 0.8);
$renderer->addTriangle( $renderer->addTriangle(
$cell - $w, 0, $w, $h, $cell - $w, 0, $w, $h,
TriangleDirection::NORTH_EAST); TriangleDirection::NORTH_EAST);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$s = (int)($cell / 3); $s = (int)($cell / 3);
$renderer->addRectangle($s, $s, $cell - $s, $cell - $s); $renderer->addRectangle($s, $s, $cell - $s, $cell - $s);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$tmp = $cell * 0.1; $tmp = $cell * 0.1;
if ($tmp > 1) { if ($tmp > 1) {
@ -129,14 +132,12 @@ class ShapeDefinitions
$outer, $outer, $outer, $outer,
$cell - $inner - $outer, $cell - $inner - $outer); $cell - $inner - $outer, $cell - $inner - $outer);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$m = (int)($cell * 0.15); $m = (int)($cell * 0.15);
$s = (int)($cell * 0.5); $s = (int)($cell * 0.5);
$renderer->addCircle($cell - $s - $m, $cell - $s - $m, $s); $renderer->addCircle($cell - $s - $m, $cell - $s - $m, $s);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$inner = $cell * 0.1; $inner = $cell * 0.1;
$outer = $inner * 4; $outer = $inner * 4;
@ -146,42 +147,38 @@ class ShapeDefinitions
} }
$renderer->addRectangle(0, 0, $cell, $cell); $renderer->addRectangle(0, 0, $cell, $cell);
$renderer->addPolygon(array( $renderer->addPolygon([
new Point($outer, $outer), new Point($outer, $outer),
new Point($cell - $inner, $outer), new Point($cell - $inner, $outer),
new Point($outer + ($cell - $outer - $inner) / 2, new Point($outer + ($cell - $outer - $inner) / 2,
$cell - $inner) $cell - $inner)
), true); ], true);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{ $renderer->addPolygon([
$renderer->addPolygon(array(
new Point(0, 0), new Point(0, 0),
new Point($cell, 0), new Point($cell, 0),
new Point($cell, $cell * 0.7), new Point($cell, $cell * 0.7),
new Point($cell * 0.4, $cell * 0.4), new Point($cell * 0.4, $cell * 0.4),
new Point($cell * 0.7, $cell), new Point($cell * 0.7, $cell),
new Point(0, $cell) new Point(0, $cell)
)); ]);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$renderer->addTriangle( $renderer->addTriangle(
$cell / 2, $cell / 2, $cell / 2, $cell / 2, $cell / 2, $cell / 2, $cell / 2, $cell / 2,
TriangleDirection::SOUTH_EAST); TriangleDirection::SOUTH_EAST);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{ $renderer->addPolygon([
$renderer->addPolygon(array(
new Point(0, 0), new Point(0, 0),
new Point($cell, 0), new Point($cell, 0),
new Point($cell, $cell / 2), new Point($cell, $cell / 2),
new Point($cell / 2, $cell), new Point($cell / 2, $cell),
new Point(0, $cell) new Point(0, $cell)
)); ]);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$tmp = $cell * 0.14; $tmp = $cell * 0.14;
if ($cell < 8) { if ($cell < 8) {
@ -207,8 +204,7 @@ class ShapeDefinitions
$outer, $outer, $outer, $outer,
$cell - $outer - $inner, $cell - $outer - $inner, true); $cell - $outer - $inner, $cell - $outer - $inner, true);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$inner = $cell * 0.12; $inner = $cell * 0.12;
$outer = $inner * 3; $outer = $inner * 3;
@ -216,21 +212,18 @@ class ShapeDefinitions
$renderer->addCircle($outer, $outer, $cell - $inner - $outer, $renderer->addCircle($outer, $outer, $cell - $inner - $outer,
true); true);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$renderer->addTriangle( $renderer->addTriangle(
$cell / 2, $cell / 2, $cell / 2, $cell / 2, $cell / 2, $cell / 2, $cell / 2, $cell / 2,
TriangleDirection::SOUTH_EAST); TriangleDirection::SOUTH_EAST);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$m = $cell * 0.25; $m = $cell * 0.25;
$renderer->addRectangle(0, 0, $cell, $cell); $renderer->addRectangle(0, 0, $cell, $cell);
$renderer->addRhombus($m, $m, $cell - $m, $cell - $m, true); $renderer->addRhombus($m, $m, $cell - $m, $cell - $m, true);
}, },
function ($renderer, $cell, $index) function (RendererInterface $renderer, int $cell, int $index): void {
{
$m = $cell * 0.4; $m = $cell * 0.4;
$s = $cell * 1.2; $s = $cell * 1.2;
@ -238,6 +231,6 @@ class ShapeDefinitions
$renderer->addCircle($m, $m, $s); $renderer->addCircle($m, $m, $s);
} }
} }
); ];
} }
} }

View file

@ -1,45 +0,0 @@
<?php
/**
* This file is part of Jdenticon for PHP.
* https://github.com/dmester/jdenticon-php/
*
* Copyright (c) 2018 Daniel Mester Pirttijärvi
*
* For full license information, please see the LICENSE file that was
* distributed with this source code.
*/
namespace Jdenticon\Shapes;
/**
* Specifies in which cell a shape will be rendered.
*/
class ShapePosition
{
/**
* The horizontal cell index measured left to right.
*
* @var int
*/
public $x;
/**
* The vertical cell index measured from the top.
*
* @var int
*/
public $y;
/**
* Creates a new ShapePosition instance.
*
* @param int $x The x-coordinate of the containing cell.
* @param int $y The y-coordinate of the containing cell.
*/
public function __construct($x, $y)
{
$this->x = $x;
$this->y = $y;
}
}