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 }}"
strategy:
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]
# uncomment this to start testing on development release
# include:

View file

@ -3,6 +3,8 @@
## 2.0.0 (not yet released)
* CHANGED: Remove page template (#265)
* 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: Unable to paste attachments from clipboard (#1589)

View file

@ -24,8 +24,8 @@
"docs" : "https://privatebin.info/codedoc/"
},
"require" : {
"php": "^7.3 || ^8.0",
"jdenticon/jdenticon": "1.0.2",
"php": "^7.4 || ^8.0",
"jdenticon/jdenticon": "2.0.0",
"mlocati/ip-lib": "1.20.0",
"symfony/polyfill-php80": "1.31.0",
"yzalis/identicon": "2.0.0"
@ -48,7 +48,7 @@
"preferred-install": "dist",
"sort-packages": true,
"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",
"This file is @generated automatically"
],
"content-hash": "6c7e6dea19e8bfd5641b220cb68c4b65",
"content-hash": "cc778a671eac2ba1ec70bf9398b2e1bf",
"packages": [
{
"name": "jdenticon/jdenticon",
"version": "1.0.2",
"version": "2.0.0",
"source": {
"type": "git",
"url": "https://github.com/dmester/jdenticon-php.git",
"reference": "cabb7a44c413c318392a341c5d3ca30fcdd57a6f"
"reference": "fb39a98a0a54982a130b7e7b06305d4fd8c9717e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/dmester/jdenticon-php/zipball/cabb7a44c413c318392a341c5d3ca30fcdd57a6f",
"reference": "cabb7a44c413c318392a341c5d3ca30fcdd57a6f",
"url": "https://api.github.com/repos/dmester/jdenticon-php/zipball/fb39a98a0a54982a130b7e7b06305d4fd8c9717e",
"reference": "fb39a98a0a54982a130b7e7b06305d4fd8c9717e",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
"php": ">=7.4.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
"phpunit/phpunit": "^9"
},
"type": "library",
"autoload": {
@ -53,7 +53,7 @@
"issues": "https://github.com/dmester/jdenticon-php/issues",
"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",
@ -337,16 +337,16 @@
},
{
"name": "myclabs/deep-copy",
"version": "1.13.0",
"version": "1.13.3",
"source": {
"type": "git",
"url": "https://github.com/myclabs/DeepCopy.git",
"reference": "024473a478be9df5fdaca2c793f2232fe788e414"
"reference": "faed855a7b5f4d4637717c2b3863e277116beb36"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/024473a478be9df5fdaca2c793f2232fe788e414",
"reference": "024473a478be9df5fdaca2c793f2232fe788e414",
"url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/faed855a7b5f4d4637717c2b3863e277116beb36",
"reference": "faed855a7b5f4d4637717c2b3863e277116beb36",
"shasum": ""
},
"require": {
@ -385,7 +385,7 @@
],
"support": {
"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": [
{
@ -393,29 +393,31 @@
"type": "tidelift"
}
],
"time": "2025-02-12T12:17:51+00:00"
"time": "2025-07-05T12:25:42+00:00"
},
{
"name": "nikic/php-parser",
"version": "v4.19.4",
"version": "v5.5.0",
"source": {
"type": "git",
"url": "https://github.com/nikic/PHP-Parser.git",
"reference": "715f4d25e225bc47b293a8b997fe6ce99bf987d2"
"reference": "ae59794362fe85e051a58ad36b289443f57be7a9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/715f4d25e225bc47b293a8b997fe6ce99bf987d2",
"reference": "715f4d25e225bc47b293a8b997fe6ce99bf987d2",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/ae59794362fe85e051a58ad36b289443f57be7a9",
"reference": "ae59794362fe85e051a58ad36b289443f57be7a9",
"shasum": ""
},
"require": {
"ext-ctype": "*",
"ext-json": "*",
"ext-tokenizer": "*",
"php": ">=7.1"
"php": ">=7.4"
},
"require-dev": {
"ircmaxell/php-yacc": "^0.0.7",
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0"
"phpunit/phpunit": "^9.0"
},
"bin": [
"bin/php-parse"
@ -423,7 +425,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.9-dev"
"dev-master": "5.0-dev"
}
},
"autoload": {
@ -447,9 +449,9 @@
],
"support": {
"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",
@ -890,16 +892,16 @@
},
{
"name": "phpunit/phpunit",
"version": "9.6.22",
"version": "9.6.23",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "f80235cb4d3caa59ae09be3adf1ded27521d1a9c"
"reference": "43d2cb18d0675c38bd44982a5d1d88f6d53d8d95"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f80235cb4d3caa59ae09be3adf1ded27521d1a9c",
"reference": "f80235cb4d3caa59ae09be3adf1ded27521d1a9c",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/43d2cb18d0675c38bd44982a5d1d88f6d53d8d95",
"reference": "43d2cb18d0675c38bd44982a5d1d88f6d53d8d95",
"shasum": ""
},
"require": {
@ -910,7 +912,7 @@
"ext-mbstring": "*",
"ext-xml": "*",
"ext-xmlwriter": "*",
"myclabs/deep-copy": "^1.12.1",
"myclabs/deep-copy": "^1.13.1",
"phar-io/manifest": "^2.0.4",
"phar-io/version": "^3.2.1",
"php": ">=7.3",
@ -973,7 +975,7 @@
"support": {
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
"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": [
{
@ -984,12 +986,20 @@
"url": "https://github.com/sebastianbergmann",
"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",
"type": "tidelift"
}
],
"time": "2024-12-05T13:48:26+00:00"
"time": "2025-05-02T06:40:34+00:00"
},
{
"name": "sebastian/cli-parser",
@ -2011,11 +2021,11 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": "^7.3 || ^8.0"
"php": "^7.4 || ^8.0"
},
"platform-dev": [],
"platform-overrides": {
"php": "7.3"
"php": "7.4"
},
"plugin-api-version": "2.3.0"
}

View file

@ -21,7 +21,7 @@ for more information.
### 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)
- zlib extension
- 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/legacy.js' => 'sha512-UxW/TOZKon83n6dk/09GsYKIyeO5LeBHokxyIq+r7KFS5KMBeIB/EM7NrkVYIezwZBaovnyNtY2d9tKFicRlXg==',
'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/rawinflate-0.3.js' => 'sha512-g8uelGgJW9A/Z1tB6Izxab++oj5kdD7B4qC7DHwZkB6DGMXKyzx7v5mvap2HXueI2IIn08YlRYM56jwWdm2ucQ==',
'js/showdown-2.1.0.js' => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',

View file

@ -34,7 +34,7 @@ class Controller
*
* @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

View file

@ -65,7 +65,6 @@ return array(
'Jdenticon\\Shapes\\Shape' => $vendorDir . '/jdenticon/jdenticon/src/Shapes/Shape.php',
'Jdenticon\\Shapes\\ShapeCategory' => $vendorDir . '/jdenticon/jdenticon/src/Shapes/ShapeCategory.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',
'PrivateBin\\Configuration' => $baseDir . '/lib/Configuration.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\\ShapeCategory' => __DIR__ . '/..' . '/jdenticon/jdenticon/src/Shapes/ShapeCategory.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',
'PrivateBin\\Configuration' => __DIR__ . '/../..' . '/lib/Configuration.php',
'PrivateBin\\Controller' => __DIR__ . '/../..' . '/lib/Controller.php',

View file

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

View file

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

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -11,7 +11,6 @@
namespace Jdenticon\Canvas;
use Jdenticon\Canvas\Rasterization\Edge;
use Jdenticon\Canvas\Rasterization\EdgeTable;
use Jdenticon\Canvas\Rasterization\Rasterizer;
use Jdenticon\Canvas\Png\PngPalette;
@ -21,15 +20,15 @@ use Jdenticon\Canvas\ColorUtils;
class Canvas
{
private $edges;
private EdgeTable $edges;
/**
* Creates a new canvas with the specified dimensions given in pixels.
*
* @param integer $width Canvas width in pixels.
* @param integer $height Canvas height in pixels.
* @param int $width Canvas width 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->height = $height;
@ -38,17 +37,13 @@ class Canvas
/**
* The width of the canvas in pixels.
*
* @var integer
*/
public $width = 0;
public int $width = 0;
/**
* The height of the canvas in pixels.
*
* @var integer
*/
public $height = 0;
public int $height = 0;
/**
* Specifies the background color. Allowed values are:
@ -57,16 +52,16 @@ class Canvas
* - strings on the format #RRGGBB
* - strings on the format #RRGGBBAA
*
* @var integer|string
* @var int|string
*/
public $backColor = 0x00000000;
/**
* 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);
}
@ -74,13 +69,13 @@ class Canvas
/**
* 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.
* @returns string
* @return string
*/
public function toPng($keywords = array())
public function toPng(array $keywords = []): string
{
$colorRanges = array();
$colorRanges = [];
Rasterizer::rasterize(
$colorRanges, $this->edges,

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -12,26 +12,27 @@
namespace Jdenticon\Canvas;
use Jdenticon\Canvas\ColorUtils;
use Jdenticon\Canvas\CanvasState;
use Jdenticon\Canvas\Rasterization\EdgeTable;
use Jdenticon\Canvas\Rasterization\Edge;
use Jdenticon\Canvas\Matrix;
class CanvasContext
{
private $savedStates = array();
private $edges;
private $transform;
private $paths;
private $canvas;
/** @var array<string, array> */
private array $savedStates = [];
private EdgeTable $edges;
private Matrix $transform;
/** @var array<float> */
private array $paths;
private Canvas $canvas;
/**
* Creates a new canvas with the specified dimensions given in pixels.
*
* @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->canvas = $canvas;
@ -46,25 +47,25 @@ class CanvasContext
* - strings on the format #RRGGBB
* - strings on the format #RRGGBBAA
*
* @var integer|string
* @var int|string
*/
public $fillStyle = 0x000000ff;
/**
* 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,
'fillStyle' => $this->fillStyle
));
]);
}
/**
* Restores the last saved state of the CanvasContext.
*/
public function restore()
public function restore(): void
{
$state = array_pop($this->savedStates);
if ($state != NULL) {
@ -76,7 +77,7 @@ class CanvasContext
/**
* 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);
}
@ -84,36 +85,16 @@ class CanvasContext
/**
* 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);
}
/**
* 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);
}
@ -123,7 +104,7 @@ class CanvasContext
* @param float $x Distance to move in the horizontal 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);
}
@ -134,7 +115,7 @@ class CanvasContext
* @param float $x Scale in the horizontal 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);
}
@ -142,10 +123,9 @@ class CanvasContext
/**
* Applies a rotation transformation to the canvas around its current origo.
*
* @param float $angle Angle in radians measured clockwise from the
* positive x axis.
* @param float $angle Angle in radians measured clockwise from the positive x axis.
*/
public function rotate($angle)
public function rotate(float $angle): void
{
$this->transform = $this->transform->rotate($angle);
}
@ -153,16 +133,16 @@ class CanvasContext
/**
* 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
* point of the previous one.
*/
public function closePath()
public function closePath(): void
{
$pathsCount = count($this->paths);
if ($pathsCount > 0) {
@ -179,7 +159,7 @@ class CanvasContext
}
// 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 $y Y coordinate.
*/
public function moveTo($x, $y)
public function moveTo(float $x, float $y): void
{
$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 $y Target Y coordinate.
* @public
*/
public function lineTo($x, $y)
public function lineTo(float $x, float $y): void
{
$pathsCount = count($this->paths);
if ($pathsCount == 0) {
$this->paths[] = array();
$this->paths[] = [];
$pathsCount++;
}
@ -227,10 +206,10 @@ class CanvasContext
* measured clockwise from the positive x axis.
* @param float $endAngle The angle in radians at which the arc end,
* 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.
*/
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;
@ -269,8 +248,6 @@ class CanvasContext
}
}
$dx;
$dy;
$sectors = ($endAngle - $startAngle) / $sectorAngle;
$angle = $startAngle;
@ -296,7 +273,7 @@ class CanvasContext
* @param float $width Width 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;
@ -330,23 +307,23 @@ class CanvasContext
* @param float $width Width 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);
$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();
$points = array(
$points = [
$this->transform->multiplyPoint($x, $y),
$this->transform->multiplyPoint($x + $width, $y),
$this->transform->multiplyPoint($x + $width, $y + $height),
$this->transform->multiplyPoint($x, $y + $height),
$this->transform->multiplyPoint($x, $y)
);
];
$pointsCount = count($points);
for ($i = 1; $i < $pointsCount; $i++) {
@ -367,7 +344,7 @@ class CanvasContext
* which areas are covered by the current path. Valid values are
* "evenodd" and "nonzero". Default is "nonzero".
*/
public function fill($windingRule = "nonzero")
public function fill(string $windingRule = "nonzero"): void
{
$polygonId = $this->edges->getNextPolygonId();
$fillColor = ColorUtils::parse($this->fillStyle);

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -15,9 +15,9 @@ class ColorUtils
{
/**
* 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
@ -25,15 +25,15 @@ class ColorUtils
*
* @var float
*/
const FORCE_TRANSPARENT = INF;
public const FORCE_TRANSPARENT = INF;
/**
* Creates a color on the format 0xRRGGBBAA from the specified
* 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;
}
@ -41,10 +41,10 @@ class ColorUtils
/**
* Gets the alpha component of a color.
*
* @param integer $color 32-bit color value on the format 0xRRGGBBAA.
* @return integer Alpha in the range [0, 255].
* @param int $color 32-bit color value on the format 0xRRGGBBAA.
* @return int Alpha in the range [0, 255].
*/
public static function alpha($color)
public static function alpha(int $color): int
{
return $color & 0xff;
}
@ -52,10 +52,10 @@ class ColorUtils
/**
* Gets the red component of a color.
*
* @param integer $color 32-bit color value on the format 0xRRGGBBAA.
* @return integer Red component in the range [0, 255].
* @param int $color 32-bit color value on the format 0xRRGGBBAA.
* @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;
}
@ -63,10 +63,10 @@ class ColorUtils
/**
* Gets the green component of a color.
*
* @param integer $color 32-bit color value on the format 0xRRGGBBAA.
* @return integer Green component in the range [0, 255].
* @param int $color 32-bit color value on the format 0xRRGGBBAA.
* @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;
}
@ -74,10 +74,10 @@ class ColorUtils
/**
* Gets the blue component of a color.
*
* @param integer $color 32-bit color value on the format 0xRRGGBBAA.
* @return integer Blue component in the range [0, 255].
* @param int $color 32-bit color value on the format 0xRRGGBBAA.
* @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;
}
@ -85,10 +85,10 @@ class ColorUtils
/**
* Formats a color as a string.
*
* @param integer $color Color to format.
* @param int $color Color to format.
* @return string
*/
public static function format($color)
public static function format(int $color): string
{
return bin2hex(pack('N', $color));
}
@ -97,13 +97,13 @@ class ColorUtils
* Computes a mix of the two specified colors, with the proportion given
* by the specified weight.
*
* @param integer $color1 First color to mix.
* @param integer $color2 Second color to mix.
* @param int $color1 First color to mix.
* @param int $color2 Second color to mix.
* @param float $weight Weight in the range [0,1].
* 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) {
$weight = 0;
@ -142,10 +142,10 @@ class ColorUtils
/**
* Parses a value to a 32-bit color on the format 0xRRGGBBAA.
*
* @param integer|string $color The value to parse.
* @return integer
* @param int|string $color The value to parse.
* @return int
*/
public static function parse($color)
public static function parse($color): int
{
if (gettype($color) == "integer") {
return $color & 0xffffffff;
@ -187,11 +187,11 @@ class ColorUtils
/**
* Blends this color with another color using the over blending operation.
*
* @param integer $fore The foreground color.
* @param integer $back The background color.
* @return integer
* @param int $fore The foreground color.
* @param int $back The background color.
* @return int
*/
public static function over($fore, $back)
public static function over(int $fore, int $back): int
{
$foreA = ($fore & 0xff);
$backA = ($back & 0xff);

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -15,17 +15,17 @@ use Jdenticon\Canvas\Point;
class Matrix
{
private $a;
private $b;
private $c;
private $d;
private $e;
private $f;
private float $a;
private float $b;
private float $c;
private float $d;
private float $e;
private float $f;
/**
* 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->b = $b;
@ -40,7 +40,7 @@ class Matrix
*
* @return boolean
*/
public function hasSkewing()
public function hasSkewing(): bool
{
return $this->b || $this->c;
}
@ -50,7 +50,7 @@ class Matrix
*
* @return boolean
*/
public function hasTranslation()
public function hasTranslation(): bool
{
return $this->e || $this->f;
}
@ -60,7 +60,7 @@ class Matrix
*
* @return boolean
*/
public function hasScaling()
public function hasScaling(): bool
{
return $this->a != 1 || $this->d != 1;
}
@ -71,7 +71,7 @@ class 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(
$this->a * $a + $this->c * $b,
@ -91,7 +91,7 @@ class Matrix
* @param float $y Y coordinate.
* @return \Jdenticon\Canvas\Point
*/
public function multiplyPoint($x, $y)
public function multiplyPoint(float $x, float $y): Point
{
return new Point(
$this->a * $x + $this->c * $y + $this->e,
@ -106,7 +106,7 @@ class Matrix
* @param float $angle Rotation angle in radians.
* @return \Jdenticon\Canvas\Matrix
*/
public function rotate($angle)
public function rotate(float $angle): Matrix
{
$sin = sin($angle);
$cos = cos($angle);
@ -121,7 +121,7 @@ class Matrix
* @param float $y Vertical move distance.
* @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);
}
@ -134,7 +134,7 @@ class Matrix
* @param float $y Vertical scale.
* @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);
}

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -13,15 +13,15 @@ namespace Jdenticon\Canvas\Png;
class PngBuffer
{
private $buffer = '';
private $chunkPreviousBuffer = '';
private string $buffer = '';
private string $chunkPreviousBuffer = '';
/**
* Writes a string to the buffer.
*
* @param string $str String to write.
*/
public function writeString($str)
public function writeString(string $str): void
{
$this->buffer .= $str;
}
@ -31,7 +31,7 @@ class PngBuffer
*
* @param integer $value Value to write.
*/
public function writeUInt32BE($value)
public function writeUInt32BE(int $value): void
{
$this->buffer .= pack('N', $value);
}
@ -41,7 +41,7 @@ class PngBuffer
*
* @param integer $value Value to write.
*/
public function writeUInt8($value)
public function writeUInt8(int $value): void
{
$this->buffer .= pack('C', $value);
}
@ -52,7 +52,7 @@ class PngBuffer
* @param string $type Name of the chunk. Must contain exactly 4
* ASCII characters.
*/
public function startChunk($type)
public function startChunk(string $type): void
{
$this->chunkPreviousBuffer = $this->buffer;
$this->buffer = $type;
@ -61,7 +61,7 @@ class PngBuffer
/**
* Closes the current PNG chunk.
*/
public function endChunk()
public function endChunk(): void
{
// Compute Crc32 for type + data
$data = $this->buffer;
@ -85,7 +85,7 @@ class PngBuffer
*
* @return string
*/
public function getBuffer()
public function getBuffer(): string
{
return $this->buffer;
}

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -13,7 +13,6 @@ namespace Jdenticon\Canvas\Png;
use Jdenticon\Canvas\Png\PngPalette;
use Jdenticon\Canvas\Png\PngBuffer;
use Jdenticon\Canvas\ColorUtils;
class PngEncoder
{
@ -23,7 +22,7 @@ class PngEncoder
const GRAYSCALE_WITH_ALPHA = 4;
const TRUE_COLOR_WITH_ALPHA = 6;
private $buffer;
private PngBuffer $buffer;
public function __construct()
{
@ -36,10 +35,10 @@ class PngEncoder
*
* @param int $width Image width 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.
*/
public function writeImageHeader($width, $height, $colorType)
public function writeImageHeader(int $width, int $height, int $colorType): void
{
$this->buffer->startChunk("IHDR");
$this->buffer->writeUInt32BE($width);
@ -57,7 +56,7 @@ class PngEncoder
*
* @param int $gamma Gamma value.
*/
public function writeImageGamma($gamma = 45455)
public function writeImageGamma(int $gamma = 45455): void
{
$this->buffer->startChunk("gAMA");
$this->buffer->writeUInt32BE($gamma);
@ -68,12 +67,11 @@ class PngEncoder
* Writes an IDAT chunk of truecolor encoded image data.
*
* @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 $height Image height in pixels.
*/
public function writeTrueColorWithAlpha(
array & $colorRanges, $width, $height)
public function writeTrueColorWithAlpha(array &$colorRanges, int $width, int $height): void
{
$this->buffer->startChunk("IDAT");
@ -111,16 +109,13 @@ class PngEncoder
* Writes an IDAT chunk of indexed image data.
*
* @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
* indexed colors.
* @param int $width Image width in pixels.
* @param int $height Image height in pixels.
*/
public function writeIndexed(
array & $colorRanges,
PngPalette $palette,
$width, $height)
public function writeIndexed(array &$colorRanges, PngPalette $palette, int $width, int $height): void
{
$this->buffer->startChunk("IDAT");
@ -162,7 +157,7 @@ class PngEncoder
* @param \Jdenticon\Canvas\Png\PngPalette $palette Palette containing the
* indexed colors.
*/
public function writePalette(PngPalette $palette)
public function writePalette(PngPalette $palette): void
{
if ($palette && $palette->isValid) {
$this->buffer->startChunk("PLTE");
@ -184,7 +179,7 @@ class PngEncoder
* @param \Jdenticon\Canvas\Png\PngPalette $palette Palette containing the
* indexed colors.
*/
public function writeTransparency(PngPalette $palette)
public function writeTransparency(PngPalette $palette): void
{
if ($palette && $palette->isValid && $palette->hasAlphaChannel) {
$this->buffer->startChunk("tRNS");
@ -208,7 +203,7 @@ class PngEncoder
* {@link https://www.w3.org/TR/2003/REC-PNG-20031110/#11keywords}
* @param string $value Value.
*/
public function writeTextualData($key, $value)
public function writeTextualData(string $key, string $value): void
{
$this->buffer->startChunk("tEXt");
$this->buffer->writeString($key);
@ -220,7 +215,7 @@ class PngEncoder
/**
* Writes an IEND chunk to the png data stream.
*/
public function writeImageEnd()
public function writeImageEnd(): void
{
$this->buffer->startChunk("IEND");
$this->buffer->endChunk();
@ -231,7 +226,7 @@ class PngEncoder
*
* @return string
*/
public function getBuffer()
public function getBuffer(): string
{
return $this->buffer->getBuffer();
}

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -11,8 +11,6 @@
namespace Jdenticon\Canvas\Png;
use Jdenticon\Canvas\ColorUtils;
/**
* Contains the colors of a PNG color palette.
*/
@ -21,13 +19,13 @@ class PngPalette
/**
* Creates a PNG color palette for the specified bitmap data.
*
* @param array(integer) $colorRanges Array of interleaved values on the
* format array(count0, color0, count1, color1, ...).
* @param array<int> $colorRanges Array of interleaved values on the
* format [count0, color0, count1, color1, ...]
*/
function __construct(& $colorRanges)
function __construct(array &$colorRanges)
{
$lookup = array();
$colors = array();
$lookup = [];
$colors = [];
$hasAlphaChannel = false;
$colorsCount = 0;
@ -63,30 +61,22 @@ class PngPalette
/**
* 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
* colors.
*
* @var boolean
*/
public $hasAlphaChannel;
public bool $hasAlphaChannel;
/**
* Array of colors in the palette.
*
* @var array
*/
public $colors;
public array $colors;
/**
* 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.
* 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
* distributed with this source code.
@ -15,17 +15,13 @@ class Point
{
/**
* X coordinate.
*
* @var float
*/
public $x;
public float $x;
/**
* Y coordinate.
*
* @var float
*/
public $y;
public float $y;
/**
* Creates a new 2D point.
@ -33,7 +29,7 @@ class Point
* @param float $x X coordinate.
* @param float $y Y coordinate.
*/
public function __construct($x, $y)
public function __construct(float $x, float $y)
{
$this->x = $x;
$this->y = $y;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -13,16 +13,22 @@ namespace Jdenticon\Canvas\Rasterization;
class Edge
{
public $polygonId;
public $x0;
public $x1;
public $y0;
public $y1;
public $color;
public $windingRule;
public int $polygonId;
public float $x0;
public float $x1;
public float $y0;
public float $y1;
public int $color;
public string $windingRule;
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->x0 = $x0;
@ -33,7 +39,7 @@ class Edge
$this->windingRule = $windingRule;
}
public function intersection($y)
public function intersection(float $y): float
{
$dx =
($this->x1 - $this->x0) * ($this->y0 - $y) /

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -13,11 +13,11 @@ namespace Jdenticon\Canvas\Rasterization;
class EdgeIntersection
{
public $fromX;
public $width;
public $edge;
public int $fromX;
public int $width;
public Edge $edge;
public function __construct($fromX, $width, $edge)
public function __construct(int $fromX, int $width, Edge $edge)
{
$this->fromX = $fromX;
$this->width = $width;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -13,10 +13,10 @@ namespace Jdenticon\Canvas\Rasterization;
class EdgeSuperSampleIntersection
{
public $x;
public $edge;
public float $x;
public Edge $edge;
public function __construct($x, $edge)
public function __construct(float $x, Edge $edge)
{
$this->x = $x;
$this->edge = $edge;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -11,20 +11,25 @@
namespace Jdenticon\Canvas\Rasterization;
use Jdenticon\Canvas\Rasterization\Edge;
class EdgeTable
{
private $scanlines;
private $nextPolygonId;
private $width;
private $height;
/**
* @var array<array<EdgeIntersection>>
*/
private array $scanlines;
private int $nextPolygonId;
private int $width;
private int $height;
/**
* Keeps a list of edges per scanline.
*
* @param integer $width Clipping width.
* @param integer $height Clipping height.
* @param int $width Clipping width.
* @param int $height Clipping height.
*/
public function __construct($width, $height)
public function __construct(int $width, int $height)
{
$this->width = $width;
$this->height = $height;
@ -34,9 +39,9 @@ class EdgeTable
/**
* 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;
}
@ -45,18 +50,19 @@ class EdgeTable
*
* @return int
*/
public function getNextPolygonId()
public function getNextPolygonId(): int
{
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.
*
* @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;
}
@ -66,7 +72,7 @@ class EdgeTable
*
* @param \Jdenticon\Canvas\Rasterization\Edge $edge
*/
public function add(\Jdenticon\Canvas\Rasterization\Edge $edge)
public function add(Edge $edge): void
{
$minY = 0;
$maxY = 0;
@ -121,7 +127,7 @@ class EdgeTable
if ($fromX < $this->width) {
if (!isset($this->scanlines[$y])) {
$this->scanlines[$y] = array();
$this->scanlines[$y] = [];
}
$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) {
return -1;
@ -148,11 +154,10 @@ class EdgeTable
/**
* Sorts the edges of each scanline in ascending x coordinates.
*/
public function sort()
public function sort(): void
{
foreach ($this->scanlines as $i => &$scanline) {
usort($scanline, array(
'Jdenticon\\Canvas\\Rasterization\\EdgeTable', 'edge_cmp'));
usort($scanline, [self::class, 'edge_cmp']);
}
}
}

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -13,14 +13,21 @@ namespace Jdenticon\Canvas\Rasterization;
class Layer
{
public $polygonId;
public $color;
public $winding;
public $windingRule;
public int $polygonId;
public int $color;
public int $winding;
public string $windingRule;
public ?Layer $nextLayer = null;
public $nextLayer;
public function __construct($polygonId, $color, $winding, $windingRule)
/**
* Creates a new layer.
*
* @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->color = $color;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -21,13 +21,12 @@ use Jdenticon\Canvas\Rasterization\Edge;
*/
class LayerManager
{
public $topLayer;
public ?Layer $topLayer = null;
/**
* The current visible color.
* @var integer
*/
public $color;
public int $color;
public function __construct()
{
@ -40,7 +39,7 @@ class LayerManager
* @param \Jdenticon\Canvas\Rasterization\LayerManager $other The
* LayerManager to copy all layers to.
*/
public function copyTo(LayerManager $other)
public function copyTo(LayerManager $other): void
{
$other->color = $this->color;
@ -72,7 +71,7 @@ class LayerManager
*
* @param \Jdenticon\Canvas\Rasterization\Edge edge
*/
public function add(Edge $edge)
public function add(Edge $edge): void
{
$dwinding = $edge->y0 < $edge->y1 ? 1 : -1;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* 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
* 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();
$superSampleBuffer = new SuperSampleBuffer(
$width, self::SAMPLES_PER_PIXEL_X);
$layers = array();
$layers = [];
$color = 0;
// Keeps track of how many of the subpixellayers that are used for
@ -278,7 +281,7 @@ class Rasterizer
return $colorData;
}
private static function intersection_cmp($a, $b)
private static function intersection_cmp(EdgeSuperSampleIntersection $a, EdgeSuperSampleIntersection $b): int
{
if ($a->x < $b->x) {
return -1;
@ -296,14 +299,14 @@ class Rasterizer
* y coordinate. For each intersecting edge the intersecting x coordinate is
* returned.
*
* @param array $edges Array of edges in the current scanline.
* @param int $y Y coordinate of the current scanline.
* @return array Array containing EdgeSuperSampleIntersection. Objects
* are sorted ascending by x coordinate.
* @param array<Edge> $edges Array of edges in the current scanline.
* @param float $y Y coordinate of the current scanline.
* @return array<EdgeSuperSampleIntersection> Intersections 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) {
if ($edge->y0 < $y && $edge->y1 >= $y ||
@ -317,9 +320,7 @@ class Rasterizer
}
}
usort($intersections, array(
'Jdenticon\\Canvas\\Rasterization\\Rasterizer',
'intersection_cmp'));
usort($intersections, [self::class, 'intersection_cmp']);
return $intersections;
}
@ -327,12 +328,13 @@ class Rasterizer
/**
* Determines what ranges of a scanline that needs to be supersampled.
*
* @param array $scanline Array of edges in the current scanline.
* @return array Array of SuperSampleRange.
* @param array<EdgeIntersection> $scanline Array of edges in the current scanline.
* @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;
$scanlineCount = count($scanline);

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -21,25 +21,25 @@ class SuperSampleBuffer
const IDX_G = 3;
const IDX_B = 4;
private $samples;
private $samplesPerPixel;
private array $samples;
private int $samplesPerPixel;
private $pixelOffset;
private $subPixelOffset;
private int $pixelOffset;
private int $subPixelOffset;
private $width;
private $used;
private int $width;
private int $used;
/**
* Creates a color buffer keeping an average color out of several
* color samples per pixel.
*
* @param integer $width Width of the buffer in pixels.
* @param integer $samplesPerPixel Number of samples to keep per pixel.
* @param int $width Width of the buffer in pixels.
* @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->pixelOffset = 0;
@ -52,7 +52,7 @@ class SuperSampleBuffer
/**
* Rewinds the cursor to the beginning of the buffer.
*/
public function rewind()
public function rewind(): void
{
$this->pixelOffset = 0;
$this->subPixelOffset = 0;
@ -61,7 +61,7 @@ class SuperSampleBuffer
/**
* Clears the samples in this buffer.
*/
public function clear()
public function clear(): void
{
$this->pixelOffset = 0;
$this->subPixelOffset = 0;
@ -71,11 +71,11 @@ class SuperSampleBuffer
/**
* 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.
* @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++) {
$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.
*
* @param integer $index The index of the pixel.
* @return integer
* @param int $index The index of the pixel.
* @return int
*/
public function colorAt($index)
public function colorAt(int $index): int
{
$sampleCount = $this->samples[$index * 5 + self::IDX_COUNT];
$alphaSum = $this->samples[$index * 5 + self::IDX_A];
@ -119,10 +119,14 @@ class SuperSampleBuffer
/**
* 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.
* @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) {
$this->used = $this->pixelOffset;
@ -147,11 +151,11 @@ class SuperSampleBuffer
/**
* Adds a color to the buffer up until the specified x index.
*
* @param integer $color Color to write.
* @param float $untilX Samples of the color will be added the buffer until
* @param int $color Color to write.
* @param float $untilX Samples of the color will be added to the buffer until
* the cursor reaches this coordinate.
*/
public function add($color, $untilX)
public function add(int $color, float $untilX): void
{
$samplesLeft =
(int)($untilX * $this->samplesPerPixel) -

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -13,15 +13,16 @@ namespace Jdenticon\Canvas\Rasterization;
class SuperSampleRange
{
public $fromX;
public $toXExcl;
public $edges;
public $width;
public int $fromX;
public int $toXExcl;
/** @var array<Edge> */
public array $edges;
public int $width;
public function __construct($fromX, $toXExcl)
public function __construct(int $fromX, int $toXExcl)
{
$this->fromX = $fromX;
$this->toXExcl = $toXExcl;
$this->edges = array();
$this->edges = [];
}
}

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -16,32 +16,28 @@ namespace Jdenticon;
*/
class Color
{
private static $lightnessCompensations = array(
0.55, 0.5, 0.5, 0.46, 0.6, 0.55, 0.55);
/** @var array<float> */
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].
* @var int
*/
public $r;
public int $r;
/**
* 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].
* @var int
*/
public $b;
public int $b;
/**
* 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
// to create Color value.
@ -52,12 +48,13 @@ class Color
/**
* 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 $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 $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->r = $red;
@ -70,12 +67,13 @@ class Color
/**
* Creates a Color instance from HSL color parameters.
*
* @param float $hue Hue 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 $hue Hue 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 $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 > 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.
*
* @param float $hue Hue 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 $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 > 1) $hue = 1;
@ -135,7 +134,7 @@ class Color
}
// 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) {
$h = $h + 6;
@ -161,7 +160,7 @@ class Color
*
* @return int
*/
public function toRgba()
public function toRgba(): int
{
return
($this->r << 24) |
@ -175,7 +174,7 @@ class Color
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return '#' . bin2hex(pack('N', $this->toRgba()));
}
@ -183,9 +182,10 @@ class Color
/**
* Gets a hexadecimal representation of this color on the format #rrggbbaa.
*
* @param int $length Length of the hex string (6 or 8).
* @return string
*/
public function toHexString($length = 8)
public function toHexString(int $length = 8): string
{
if ($length === 8) {
return $this->__toString();
@ -197,10 +197,11 @@ class Color
* Tries to parse a value as a Color.
*
* @param mixed $value Value to parse.
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
* @return \Jdenticon\Color
*/
public static function parse($value) {
public static function parse($value): Color
{
if ($value instanceof Color) {
return $value;
}
@ -252,7 +253,7 @@ class Color
*
* @return boolean
*/
private static function parsePercent($input, &$result)
private static function parsePercent(string $input, ?float &$result): bool
{
// Detect and remove percent sign
if (preg_match('/^\\s*(\\d*(?:\\.\\d*)?)%\\s*$/', $input, $matches)) {
@ -274,7 +275,7 @@ class Color
*
* @return boolean
*/
private static function parseAlpha($input, &$result)
private static function parseAlpha(?string $input, ?float &$result): bool
{
if ($input === null ||
$input === ''
@ -304,20 +305,20 @@ class Color
* Parses an RGB component.
*
* @param string $input Input string.
* @param float $result Hue in range [0, 255].
* @param int $result RGB component in range [0, 255].
*
* @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)) {
$result = floatval($matches[1]);
$float = floatval($matches[1]);
if ($matches[2] === '%') {
$result = 255 * $result / 100;
$float = 255 * $float / 100;
}
$result = (int)$result;
$result = (int)$float;
if ($result < 0) $result = 0;
if ($result > 255) $result = 255;
@ -335,7 +336,7 @@ class Color
*
* @return boolean
*/
private static function parseHue($input, &$result)
private static function parseHue(string $input, ?float &$result): bool
{
if (preg_match(
'/^\s*(\d*(?:\.\d*)?)(deg|grad|rad|turn|)\s*$/',
@ -355,7 +356,7 @@ class Color
break;
case "turn":
// Turns: range 0 - 1
$result = $result;
// No change
break;
default:
// Degree: range 0 - 360
@ -378,11 +379,11 @@ class Color
* Parses a hex color string.
*
* @param string $input Input string.
* @param float $result Hue in range [0, 1].
* @param \Jdenticon\Color $result Parsed color.
*
* @return boolean
*/
private static function parseHexColor($input, &$result)
private static function parseHexColor(string $input, ?Color &$result): bool
{
if ($input[0] === '#') {
$input = substr($input, 1);
@ -444,9 +445,9 @@ class Color
*
* @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
switch ($input) {

View file

@ -3,7 +3,8 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -29,37 +30,17 @@ class Identicon
*/
private $value;
/**
* @var boolean
*/
private $valueSet = false;
private bool $valueSet = false;
/**
* Defaults to hash of an empty string.
*
* @var string
*/
private $hash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
private string $hash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
/**
* @var integer
*/
private $size = 100;
/**
* @var Jdenticon\Rendering\IconGenerator
*/
private $iconGenerator;
/**
* @var Jdenticon\IdenticonStyle
*/
private $style;
/**
* @var bool
*/
private $enableImageMagick;
private int $size = 100;
private IconGenerator $iconGenerator;
private ?IdenticonStyle $style = null;
private ?bool $enableImageMagick = null;
/**
* 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
* is quadratic).
*/
public function __construct($options = null)
public function __construct(?array $options = null)
{
$this->iconGenerator = IconGenerator::getDefaultGenerator();
@ -90,9 +71,9 @@ class Identicon
* @param int $size The size of the icon in pixels (the icon is quadratic).
* @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).
* @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.
*
* @return array
* @return array<string, mixed>
*/
public function getOptions()
public function getOptions(): array
{
$options = array();
$options = [];
if ($this->valueSet) {
$options['value'] = $this->getValue();
@ -142,10 +123,10 @@ class Identicon
* Sets options in this identicon by specifying an associative array of
* option values.
*
* @param array $options Options to set.
* @param array<string, mixed> $options Options to set.
* @return self
*/
public function setOptions(array $options)
public function setOptions(array $options): self
{
foreach ($options as $key => $value) {
$this->__set($key, $value);
@ -153,7 +134,7 @@ class Identicon
return $this;
}
public function __get($name)
public function __get(string $name)
{
switch (strtolower($name)) {
case 'size':
@ -174,7 +155,7 @@ class Identicon
}
}
public function __set($name, $value)
public function __set(string $name, $value)
{
switch (strtolower($name)) {
case 'size':
@ -204,7 +185,7 @@ class Identicon
/**
* Gets the size of the icon in pixels.
*/
public function getSize()
public function getSize(): int
{
return $this->size;
}
@ -214,7 +195,7 @@ class Identicon
*
* @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) {
throw new \InvalidArgumentException(
@ -228,13 +209,12 @@ class Identicon
/**
* 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
// is not as obvious as on PHP 5. Since the ImageMagick renderer has a
// lot of quirks, we don't want to use it unless really needed.
// Performance of using Imagick on PHP 7 and later is generally worse than using
// the internal renderer. Because of this, default to false.
if ($this->enableImageMagick === null) {
return PHP_MAJOR_VERSION < 7 && extension_loaded('imagick');
return false;
}
return $this->enableImageMagick;
@ -245,13 +225,8 @@ class Identicon
*
* @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
if ($enable && !extension_loaded('imagick')) {
throw new \Exception(
@ -267,7 +242,7 @@ class Identicon
*
* @return \Jdenticon\Rendering\IconGenerator
*/
public function getIconGenerator()
public function getIconGenerator(): IconGenerator
{
return $this->iconGenerator;
}
@ -279,7 +254,7 @@ class Identicon
* that will render the shapes of the identicon.
* @return \Jdenticon\Identicon
*/
public function setIconGenerator(IconGenerator $iconGenerator)
public function setIconGenerator(IconGenerator $iconGenerator): self
{
if ($iconGenerator === null) {
$iconGenerator = IconGenerator::getDefaultGenerator();
@ -293,7 +268,7 @@ class Identicon
*
* @return \Jdenticon\IdenticonStyle
*/
public function getStyle()
public function getStyle(): IdenticonStyle
{
return $this->style;
}
@ -301,11 +276,11 @@ class Identicon
/**
* 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.
* @return self
*/
public function setStyle($style)
public function setStyle($style): self
{
if ($style == null) {
$this->style = new IdenticonStyle();
@ -327,7 +302,7 @@ class Identicon
* Gets a binary string containing the hash that is used as base for this
* icon.
*/
public function getHash()
public function getHash(): string
{
return $this->hash;
}
@ -338,13 +313,8 @@ class Identicon
*
* @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) {
throw new \InvalidArgumentException(
'An invalid $hash was passed to Identicon. ' .
@ -372,7 +342,7 @@ class Identicon
*
* @param mixed $value Value that will be hashed.
*/
public function setValue($value)
public function setValue($value): self
{
$this->hash = sha1("$value");
$this->value = $value;
@ -385,7 +355,7 @@ class Identicon
*
* @return \Jdenticon\Rendering\Rectangle
*/
public function getIconBounds()
public function getIconBounds(): Rectangle
{
// Round padding to nearest integer
$padding = (int)($this->style->getPadding() * $this->size + 0.5);
@ -396,7 +366,7 @@ class Identicon
$this->size - $padding * 2);
}
private function getRenderer($imageFormat)
private function getRenderer(string $imageFormat): RendererInterface
{
switch (strtolower($imageFormat)) {
case 'svg':
@ -424,10 +394,7 @@ class Identicon
* is omitted, the rectangle is calculated from the current icon
* size and padding.
*/
public function draw(
\Jdenticon\Rendering\RendererInterface $renderer,
?\Jdenticon\Rendering\Rectangle $rect = null)
{
public function draw(RendererInterface $renderer, ?Rectangle $rect = null): void {
if ($rect === null) {
$rect = $this->getIconBounds();
}
@ -445,7 +412,7 @@ class Identicon
* @param string $imageFormat The image format of the output.
* Supported values are 'png' and 'svg'.
*/
public function displayImage($imageFormat = 'png')
public function displayImage(string $imageFormat = 'png'): void
{
$renderer = $this->getRenderer($imageFormat);
$this->draw($renderer, $this->getIconBounds());
@ -462,7 +429,7 @@ class Identicon
* Supported values are 'png' and 'svg'.
* @return string
*/
public function getImageData($imageFormat = 'png')
public function getImageData(string $imageFormat = 'png'): string
{
$renderer = $this->getRenderer($imageFormat);
$this->draw($renderer, $this->getIconBounds());
@ -479,7 +446,7 @@ class Identicon
* Supported values are 'png' and 'svg'.
* @return string
*/
public function getImageDataUri($imageFormat = 'png')
public function getImageDataUri(string $imageFormat = 'png'): string
{
$renderer = $this->getRenderer($imageFormat);
$this->draw($renderer, $this->getIconBounds());
@ -488,5 +455,3 @@ class Identicon
return "data:$mimeType;base64,$base64";
}
}

View file

@ -3,7 +3,8 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -18,40 +19,13 @@ use Jdenticon\Color;
*/
class IdenticonStyle
{
/**
* @var \Jdenticon\Color
*/
private $backgroundColor;
/**
* @var float
*/
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;
private Color $backgroundColor;
private float $padding;
private float $colorSaturation;
private float $grayscaleSaturation;
private array $colorLightness;
private array $grayscaleLightness;
private ?array $hues = null;
public function __construct(?array $options = null)
{
@ -72,9 +46,9 @@ class IdenticonStyle
*
* @return array
*/
public function getOptions()
public function getOptions(): array
{
$options = array();
$options = [];
$options['backgroundColor'] = $this->getBackgroundColor()->__toString();
$options['padding'] = $this->getPadding();
@ -97,7 +71,7 @@ class IdenticonStyle
* @param array $options Options to set.
* @return self
*/
public function setOptions(array $options)
public function setOptions(array $options): self
{
foreach ($options as $key => $value) {
$this->__set($key, $value);
@ -105,7 +79,7 @@ class IdenticonStyle
return $this;
}
public function __get($name)
public function __get(string $name)
{
switch (strtolower($name)) {
case 'backgroundcolor':
@ -128,7 +102,7 @@ class IdenticonStyle
}
}
public function __set($name, $value)
public function __set(string $name, $value): void
{
switch (strtolower($name)) {
case 'backgroundcolor':
@ -161,17 +135,17 @@ class IdenticonStyle
/**
* Normalizes a hue to the first turn [0, 360).
*
* @param mixed $hue
* @param int|float $hue
* @return integer
*/
private static function normalizeHue($hue)
private static function normalizeHue($hue): int
{
if (!is_numeric($hue)) {
throw new \InvalidArgumentException(
"'$hue' is not a valid hue.");
}
$hue = $hue % 360;
$hue = (int)$hue % 360;
if ($hue < 0) {
$hue += 360;
}
@ -182,9 +156,9 @@ class IdenticonStyle
/**
* 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;
}
@ -192,14 +166,14 @@ class IdenticonStyle
/**
* 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
* list is cleared.
* @return self
*/
public function setHues($value)
public function setHues($value): self
{
$hues = array();
$hues = [];
if ($value !== null) {
if (is_array($value)) {
@ -220,7 +194,7 @@ class IdenticonStyle
*
* @return float
*/
public function getPadding()
public function getPadding(): float
{
return $this->padding;
}
@ -231,9 +205,9 @@ class IdenticonStyle
* @param float $value New padding in the range [0.0, 0.4].
* @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(
"Padding '$value' out of range. ".
"Values in the range [0.0, 0.4] are allowed.");
@ -247,7 +221,7 @@ class IdenticonStyle
*
* @return \Jdenticon\Color
*/
public function getBackgroundColor()
public function getBackgroundColor(): Color
{
return $this->backgroundColor;
}
@ -258,7 +232,7 @@ class IdenticonStyle
* @param \Jdenticon\Color|string $value New background color.
* @return \Jdenticon\IdenticonStyle
*/
public function setBackgroundColor($value)
public function setBackgroundColor($value): self
{
if ($value instanceof Color) {
$this->backgroundColor = $value;
@ -274,7 +248,7 @@ class IdenticonStyle
*
* @return float Saturation in the range [0.0, 1.0].
*/
public function getGrayscaleSaturation()
public function getGrayscaleSaturation(): float
{
return $this->grayscaleSaturation;
}
@ -285,11 +259,9 @@ class IdenticonStyle
* @param $value float Saturation in the range [0.0, 1.0].
* @return self
*/
public function setGrayscaleSaturation($value)
public function setGrayscaleSaturation(float $value): self
{
if (!is_numeric($value) ||
$value < 0 || $value > 1
) {
if ($value < 0 || $value > 1) {
throw new \InvalidArgumentException(
"The grayscale saturation was invalid. ".
"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].
*/
public function getColorSaturation()
public function getColorSaturation(): float
{
return $this->colorSaturation;
}
@ -314,11 +286,9 @@ class IdenticonStyle
* @param $value float Saturation in the range [0.0, 1.0].
* @return self
*/
public function setColorSaturation($value)
public function setColorSaturation(float $value): self
{
if (!is_numeric($value) ||
$value < 0 || $value > 1
) {
if ($value < 0 || $value > 1) {
throw new \InvalidArgumentException(
"The color saturation was invalid. ".
"Only values in the range [0.0, 1.0] are allowed.");
@ -330,9 +300,9 @@ class IdenticonStyle
/**
* Gets the value of the ColorLightness property.
*
* @return array(float, float)
* @return array<float>
*/
public function getColorLightness()
public function getColorLightness(): array
{
return $this->colorLightness;
}
@ -340,13 +310,12 @@ class IdenticonStyle
/**
* Sets the value of the ColorLightness property.
*
* @param $value array(float, float) Lightness range.
* @param array<float> $value Lightness range.
* @return self
*/
public function setColorLightness($value)
public function setColorLightness(array $value): self
{
if (!is_array($value) ||
!array_key_exists(0, $value) ||
if (!array_key_exists(0, $value) ||
!array_key_exists(1, $value) ||
!is_numeric($value[0]) ||
!is_numeric($value[1]) ||
@ -358,16 +327,16 @@ class IdenticonStyle
"Please check the documentation.");
}
$this->colorLightness = array((float)$value[0], (float)$value[1]);
$this->colorLightness = [(float)$value[0], (float)$value[1]];
return $this;
}
/**
* Gets the value of the GrayscaleLightness property.
*
* @return array(float, float)
* @return array<float>
*/
public function getGrayscaleLightness()
public function getGrayscaleLightness(): array
{
return $this->grayscaleLightness;
}
@ -375,13 +344,12 @@ class IdenticonStyle
/**
* Sets the value of the GrayscaleLightness property.
*
* @param $value array(float, float) Lightness range.
* @param array<float> $value Lightness range.
* @return self
*/
public function setGrayscaleLightness($value)
public function setGrayscaleLightness(array $value): self
{
if (!is_array($value) ||
!array_key_exists(0, $value) ||
if (!array_key_exists(0, $value) ||
!array_key_exists(1, $value) ||
!is_numeric($value[0]) ||
!is_numeric($value[1]) ||
@ -392,7 +360,7 @@ class IdenticonStyle
"The value passed to setGrayscaleLightness was invalid. ".
"Please check the documentation.");
}
$this->grayscaleLightness = array((float)$value[0], (float)$value[1]);
$this->grayscaleLightness = [(float)$value[0], (float)$value[1]];
return $this;
}
@ -403,7 +371,7 @@ class IdenticonStyle
*
* @return \Jdenticon\Color
*/
public static function getDefaultBackgroundColor()
public static function getDefaultBackgroundColor(): Color
{
return Color::fromRgb(255, 255, 255, 255);
}
@ -413,7 +381,7 @@ class IdenticonStyle
*
* @return float
*/
public static function getDefaultPadding()
public static function getDefaultPadding(): float
{
return 0.08;
}
@ -423,7 +391,7 @@ class IdenticonStyle
*
* @return float
*/
public static function getDefaultColorSaturation()
public static function getDefaultColorSaturation(): float
{
return 0.5;
}
@ -433,7 +401,7 @@ class IdenticonStyle
*
* @return float
*/
public static function getDefaultGrayscaleSaturation()
public static function getDefaultGrayscaleSaturation(): float
{
return 0;
}
@ -441,20 +409,20 @@ class IdenticonStyle
/**
* 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].
*
* @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.
* 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
* distributed with this source code.
@ -11,6 +11,9 @@
namespace Jdenticon\Rendering;
use Jdenticon\Color;
use Jdenticon\Rendering\Transform;
/**
* 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
@ -19,8 +22,8 @@ namespace Jdenticon\Rendering;
*/
abstract class AbstractRenderer implements RendererInterface
{
private $transform;
protected $backgroundColor;
private Transform $transform;
protected ?Color $backgroundColor = null;
public function __construct()
{
@ -31,10 +34,10 @@ abstract class AbstractRenderer implements RendererInterface
* Sets the current transform that will be applied on all coordinates before
* 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.
*/
public function setTransform(\Jdenticon\Rendering\Transform $transform)
public function setTransform(?Transform $transform): void
{
$this->transform = $transform === null ?
Transform::getEmpty() : $transform;
@ -46,7 +49,7 @@ abstract class AbstractRenderer implements RendererInterface
*
* @return \Jdenticon\Rendering\Transform
*/
public function getTransform()
public function getTransform(): Transform
{
return $this->transform;
}
@ -54,29 +57,26 @@ abstract class AbstractRenderer implements RendererInterface
/**
* 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.
*
* @param float $x The x-coordinate of the bounding rectangle
* upper-left corner.
* @param float $y The y-coordinate of the bounding rectangle
* upper-left corner.
* @param float $x The x-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 bool $counterClockwise If true the circle will be drawn
* counter clockwise.
* @param bool $counterClockwise If true the circle will be drawn 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.
*
* @param \Jdenticon\Color $color The image background color.
*/
public function setBackgroundColor(\Jdenticon\Color $color)
public function setBackgroundColor(Color $color): void
{
$this->backgroundColor = $color;
}
@ -86,14 +86,20 @@ abstract class AbstractRenderer implements RendererInterface
*
* @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;
}
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) {
$transformedPoints[] =
$this->transform->transformPoint($point->x, $point->y);
@ -103,8 +109,6 @@ abstract class AbstractRenderer implements RendererInterface
$transformedPoints = array_reverse($transformedPoints);
}
//var_dump($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
* 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 + $width, $y),
new Point($x + $width, $y + $height),
new Point($x, $y + $height),
), $invert);
], $invert);
}
/**
* Adds a circle to the image.
*
* @param float $x The x-coordinate of the bounding rectangle
* upper-left corner.
* @param float $y The y-coordinate of the bounding rectangle
* upper-left corner.
* @param float $x The x-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 bool $invert If true the area of the circle will be removed
* from the filled area.
* @param bool $invert If true the area of the circle will be removed 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);
$this->addCircleNoTransform($northWest->x, $northWest->y, $size, $invert);
@ -148,11 +149,10 @@ abstract class AbstractRenderer implements RendererInterface
/**
* Adds a polygon to the image.
*
* @param array $points Array of points that the polygon consists of.
* @param bool $invert If true the area of the polygon will be removed
* from the filled area.
* @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 the filled area.
*/
public function addPolygon($points, $invert = false)
public function addPolygon(array $points, bool $invert = false): void
{
$this->addPolygonCore($points, $invert);
}
@ -160,25 +160,22 @@ abstract class AbstractRenderer implements RendererInterface
/**
* Adds a triangle to the image.
*
* @param float $x The x-coordinate of the bounding rectangle
* upper-left corner.
* @param float $y The y-coordinate of the bounding rectangle
* upper-left corner.
* @param float $x The x-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 $height The height of the bounding rectangle.
* @param float $direction The direction of the 90 degree corner of the
* triangle.
* @param bool $invert If true the area of the triangle will be removed
* from the filled area.
* @param int $direction The direction of the 90-degree corner of the triangle.
* Value of {@link \Jdenticon\Rendering\TriageDirection}
* @param bool $invert If true the area of the triangle will be removed 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 + $height),
new Point($x, $y + $height),
new Point($x, $y)
);
];
array_splice($points, $direction, 1);
@ -188,22 +185,19 @@ abstract class AbstractRenderer implements RendererInterface
/**
* Adds a rhombus to the image.
*
* @param float $x The x-coordinate of the bounding rectangle
* upper-left corner.
* @param float $y The y-coordinate of the bounding rectangle
* upper-left corner.
* @param float $x The x-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 $height The height of the bounding rectangle.
* @param bool $invert If true the area of the rhombus will be removed
* from the filled area.
* @param bool $invert If true the area of the rhombus will be removed 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, $y + $height / 2),
new Point($x + $width / 2, $y + $height),
new Point($x, $y + $height / 2),
), $invert);
], $invert);
}
}

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -12,17 +12,18 @@
namespace Jdenticon\Rendering;
use Jdenticon\Color;
use Jdenticon\IdenticonStyle;
/**
* Specifies the colors to be used in an identicon.
*/
class ColorTheme
{
private $darkGray;
private $midColor;
private $lightGray;
private $lightColor;
private $darkColor;
private Color $darkGray;
private Color $midColor;
private Color $lightGray;
private Color $lightColor;
private Color $darkColor;
/**
* Creates a new ColorTheme.
@ -31,7 +32,7 @@ class ColorTheme
* @param \Jdenticon\IdenticonStyle $style The style that specifies the
* lightness and saturation of the icon.
*/
public function __construct($hue, \Jdenticon\IdenticonStyle $style)
public function __construct(float $hue, IdenticonStyle $style)
{
$grayscaleLightness = $style->getGrayscaleLightness();
$colorLightness = $style->getColorLightness();
@ -60,9 +61,9 @@ class ColorTheme
* Gets a color from this color theme by index.
*
* @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 === 1) return $this->midColor;
@ -77,7 +78,7 @@ class ColorTheme
*
* @return int
*/
public function getCount()
public function getCount(): int
{
return 5;
}

View file

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

View file

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

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -12,14 +12,16 @@
namespace Jdenticon\Rendering;
use Jdenticon\Canvas\Canvas;
use Jdenticon\Canvas\CanvasContext;
use Jdenticon\Color;
/**
* Renders icons as PNG using the internal vector rasterizer.
*/
class InternalPngRenderer extends AbstractRenderer
{
private $canvas;
private $ctx;
private Canvas $canvas;
private CanvasContext $ctx;
/**
* 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 $height The height of the icon in pixels.
*/
public function __construct($width, $height)
public function __construct(int $width, int $height)
{
parent::__construct();
$this->canvas = new Canvas($width, $height);
@ -39,7 +41,7 @@ class InternalPngRenderer extends AbstractRenderer
*
* @return string
*/
public function getMimeType()
public function getMimeType(): string
{
return 'image/png';
}
@ -55,7 +57,7 @@ class InternalPngRenderer extends AbstractRenderer
* @param bool $counterClockwise If true the circle will be drawn
* counter clockwise.
*/
protected function addCircleNoTransform($x, $y, $size, $counterClockwise)
protected function addCircleNoTransform(float $x, float $y, float $size, bool $counterClockwise): void
{
$radius = $size / 2;
$this->ctx->moveTo($x + $size, $y + $radius);
@ -69,9 +71,9 @@ class InternalPngRenderer extends AbstractRenderer
/**
* 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);
$this->ctx->moveTo($points[0]->x, $points[0]->y);
@ -86,7 +88,7 @@ class InternalPngRenderer extends AbstractRenderer
*
* @param \Jdenticon\Color $color The background color.
*/
public function setBackgroundColor(\Jdenticon\Color $color)
public function setBackgroundColor(Color $color): void
{
parent::setBackgroundColor($color);
$this->canvas->backColor = $this->backgroundColor->toRgba();
@ -97,7 +99,7 @@ class InternalPngRenderer extends AbstractRenderer
*
* @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->beginPath();
@ -106,7 +108,7 @@ class InternalPngRenderer extends AbstractRenderer
/**
* Ends the currently drawn shape.
*/
public function endShape()
public function endShape(): void
{
$this->ctx->fill();
}
@ -116,8 +118,8 @@ class InternalPngRenderer extends AbstractRenderer
*
* @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.
* 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
* distributed with this source code.
@ -22,7 +22,7 @@ class Point
* @param float $x X coordinate.
* @param float $y Y coordinate.
*/
public function __construct($x, $y)
public function __construct(float $x, float $y)
{
$this->x = $x;
$this->y = $y;
@ -30,25 +30,19 @@ class Point
/**
* The X coordinate of this point.
*
* @var float
*/
public $x;
public float $x;
/**
* The Y coordinate of this point.
*
* @var float
*/
public $y;
public float $y;
/**
* 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.
* 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
* distributed with this source code.
@ -18,29 +18,23 @@ class 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.
*
* @var float
*/
public $y;
public float $y;
/**
* The width of the rectangle.
* @var float
*/
public $width;
public float $width;
/**
* The height of the rectangle.
* @var float
*/
public $height;
public float $height;
/**
* Creates a new Rectangle.
@ -50,7 +44,7 @@ class Rectangle
* @param float $width The width 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->y = $y;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -25,7 +25,7 @@ interface RendererInterface
* @param \Jdenticon\Rendering\Transform $transform The transform to set.
* 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
@ -33,40 +33,40 @@ interface RendererInterface
*
* @return \Jdenticon\Rendering\Transform
*/
public function getTransform();
public function getTransform(): Transform;
/**
* Sets the background color of the image.
*
* @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.
*
* @return \Jdenticon\Color
*/
public function getBackgroundColor();
public function getBackgroundColor(): Color;
/**
* Gets the MIME type of the renderer output.
*
* @return string
*/
public function getMimeType();
public function getMimeType(): string;
/**
* Begins a new shape. The shape should be ended with a call to endShape.
*
* @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.
*/
public function endShape();
public function endShape(): void;
/**
* 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
* 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.
@ -91,16 +91,16 @@ interface RendererInterface
* @param bool $invert If true the area of the circle will be removed 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;
/**
* 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
* the filled area.
*/
public function addPolygon($points, $invert = false);
public function addPolygon(array $points, bool $invert = false): void;
/**
* Adds a triangle to the image.
@ -111,12 +111,12 @@ interface RendererInterface
* upper-left corner.
* @param float $width The width 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 triangle.
* @param int $direction The direction of the 90 degree corner of
* the triangle. Value of {@link \Jdenticon\Rendering\TriageDirection}
* @param bool $invert If true the area of the triangle will be removed
* 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.
@ -130,12 +130,12 @@ interface RendererInterface
* @param bool $invert If true the area of the rhombus will be removed
* 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.
*
* @return string
*/
public function getData();
public function getData(): string;
}

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -16,7 +16,7 @@ namespace Jdenticon\Rendering;
*/
class SvgPath
{
private $dataString;
private string $dataString;
public function __construct()
{
@ -33,7 +33,7 @@ class SvgPath
* clockwise. This affects the rendering since the evenodd filling rule
* 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';
$radiusAsString = number_format($size / 2, 2, '.', '');
@ -50,10 +50,10 @@ class SvgPath
/**
* 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.
*/
public function addPolygon($points)
public function addPolygon(array $points): void
{
$pointCount = count($points);
@ -75,7 +75,7 @@ class SvgPath
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->dataString;
}

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -11,15 +11,18 @@
namespace Jdenticon\Rendering;
use Jdenticon\Color;
/**
* Renders icons as SVG paths.
*/
class SvgRenderer extends AbstractRenderer
{
private $pathsByColor = array();
private $path;
private $width;
private $height;
/** @var array<string, SvgPath> */
private array $pathsByColor = [];
private ?SvgPath $path = null;
private int $width;
private int $height;
/**
* Creates a new SvgRenderer.
@ -27,7 +30,7 @@ class SvgRenderer extends AbstractRenderer
* @param int $width The width 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->height = $height;
@ -38,7 +41,7 @@ class SvgRenderer extends AbstractRenderer
*
* @return string
*/
public function getMimeType()
public function getMimeType(): string
{
return 'image/svg+xml';
}
@ -54,7 +57,7 @@ class SvgRenderer extends AbstractRenderer
* @param bool $counterClockwise If true the circle will be drawn
* 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);
}
@ -62,9 +65,9 @@ class SvgRenderer extends AbstractRenderer
/**
* 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);
}
@ -74,7 +77,7 @@ class SvgRenderer extends AbstractRenderer
*
* @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);
@ -89,7 +92,7 @@ class SvgRenderer extends AbstractRenderer
/**
* 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
* will be rendered.
* @return string
*/
public function getData($fragment = false)
public function getData(bool $fragment = false): string
{
$svg = '';
$widthAsString = number_format($this->width, 0, '.', '');

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -16,10 +16,10 @@ namespace Jdenticon\Rendering;
*/
class Transform
{
private $x;
private $y;
private $size;
private $rotation;
private float $x;
private float $y;
private float $size;
private int $rotation;
/**
* Creates a new Transform.
@ -29,10 +29,10 @@ class Transform
* @param float $y The y-coordinate of the upper left corner 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.
*/
public function __construct($x, $y, $size, $rotation)
public function __construct(float $x, float $y, float $size, int $rotation)
{
$this->x = $x;
$this->y = $y;
@ -45,7 +45,7 @@ class Transform
*
* @return \Jdenticon\Rendering\Transform
*/
public static function getEmpty()
public static function getEmpty(): Transform
{
return new Transform(0, 0, 0, 0);
}
@ -64,7 +64,7 @@ class Transform
* corner of the transformed rectangle.
* @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;
$bottom = $this->y + $this->size;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -19,17 +19,17 @@ class TriangleDirection
/**
* 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.
*/
const NORTH_WEST = 1;
public const NORTH_WEST = 1;
/**
* 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.
*/
const SOUTH_EAST = 3;
public const SOUTH_EAST = 3;
}

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -11,6 +11,8 @@
namespace Jdenticon\Shapes;
use Jdenticon\Color;
/**
* Represents a shape to be rendered in an icon. These instances are
* hash specific.
@ -20,39 +22,35 @@ class Shape
/**
* The shape definition to be used to render the shape.
*
* @var function(
* @var callable(
* \Jdenticon\Rendering\RendererInterface $renderer,
* \Jdenticon\Shapes\ShapePosition $cell,
* int $index)
* int $cell,
* int $index): void
*/
public $definition;
/**
* 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.
*
* @var int
*/
public $startRotationIndex;
public int $startRotationIndex;
public function __construct(
$definition,
\Jdenticon\Color $color,
callable $definition,
Color $color,
array $positions,
$startRotationIndex)
int $startRotationIndex)
{
$this->definition = $definition;
$this->color = $color;

View file

@ -3,7 +3,7 @@
* This file is part of Jdenticon for 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
* distributed with this source code.
@ -20,46 +20,44 @@ class ShapeCategory
/**
* The index of the hash octet determining the color of shapes in this
* category.
*
* @var int
*/
public $colorIndex;
public int $colorIndex;
/**
* A list of possible shape definitions in this category.
*
* @var array(function(
* @var array<callable(
* \Jdenticon\Rendering\RendererInterface $renderer,
* \Jdenticon\Shapes\ShapePosition $cell,
* int $index))
* int $cell,
* int $index): void>
*/
public $shapes;
public array $shapes;
/**
* The index of the hash octet determining which of the shape definitions
* 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
* in the first position.
*
* @var int
*/
public $rotationIndex;
public ?int $rotationIndex;
/**
* 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(
$colorIndex, array $shapes, $shapeIndex, $rotationIndex, array $positions)
int $colorIndex,
array $shapes,
int $shapeIndex,
?int $rotationIndex,
array $positions)
{
$this->colorIndex = $colorIndex;
$this->shapes = $shapes;

View file

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