|
@@ -2,10 +2,11 @@
|
|
|
/**
|
|
/**
|
|
|
* This file is part of Jdenticon for PHP.
|
|
* This file is part of Jdenticon for PHP.
|
|
|
* https://github.com/dmester/jdenticon-php/
|
|
* https://github.com/dmester/jdenticon-php/
|
|
|
- *
|
|
|
|
|
- * Copyright (c) 2018 Daniel Mester Pirttijärvi
|
|
|
|
|
- *
|
|
|
|
|
- * For full license information, please see the LICENSE file that was
|
|
|
|
|
|
|
+ *
|
|
|
|
|
+ * 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.
|
|
* distributed with this source code.
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
@@ -28,51 +29,31 @@ class Identicon
|
|
|
* @var mixed
|
|
* @var mixed
|
|
|
*/
|
|
*/
|
|
|
private $value;
|
|
private $value;
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * @var boolean
|
|
|
|
|
- */
|
|
|
|
|
- private $valueSet = false;
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private bool $valueSet = false;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Defaults to hash of an empty string.
|
|
* Defaults to hash of an empty string.
|
|
|
- *
|
|
|
|
|
- * @var string
|
|
|
|
|
- */
|
|
|
|
|
- private $hash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * @var integer
|
|
|
|
|
- */
|
|
|
|
|
- private $size = 100;
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * @var Jdenticon\Rendering\IconGenerator
|
|
|
|
|
- */
|
|
|
|
|
- private $iconGenerator;
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * @var Jdenticon\IdenticonStyle
|
|
|
|
|
*/
|
|
*/
|
|
|
- private $style;
|
|
|
|
|
|
|
+ private string $hash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * @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.
|
|
* Creates an Identicon instance with the specified hash.
|
|
|
*
|
|
*
|
|
|
- * @param string $hash A binary string containing the hash that will be used
|
|
|
|
|
|
|
+ * @param string $hash A binary string containing the hash that will be used
|
|
|
* as base for this icon. The hash must contain at least 6 bytes.
|
|
* as base for this icon. The hash must contain at least 6 bytes.
|
|
|
- * @param int|float|double $size The size of the icon in pixels (the icon
|
|
|
|
|
|
|
+ * @param int|float|double $size The size of the icon in pixels (the icon
|
|
|
* is quadratic).
|
|
* is quadratic).
|
|
|
*/
|
|
*/
|
|
|
- public function __construct($options = null)
|
|
|
|
|
|
|
+ public function __construct(?array $options = null)
|
|
|
{
|
|
{
|
|
|
$this->iconGenerator = IconGenerator::getDefaultGenerator();
|
|
$this->iconGenerator = IconGenerator::getDefaultGenerator();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if ($options !== null) {
|
|
if ($options !== null) {
|
|
|
$this->setOptions($options);
|
|
$this->setOptions($options);
|
|
|
}
|
|
}
|
|
@@ -81,79 +62,79 @@ class Identicon
|
|
|
$this->style = new IdenticonStyle();
|
|
$this->style = new IdenticonStyle();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Creates an Identicon instance from a specified hash.
|
|
* Creates an Identicon instance from a specified hash.
|
|
|
*
|
|
*
|
|
|
- * @param string $hash A binary string containing the hash that will be used
|
|
|
|
|
|
|
+ * @param string $hash A binary string containing the hash that will be used
|
|
|
* as base for this icon. The hash must contain at least 6 bytes.
|
|
* as base for this icon. The hash must contain at least 6 bytes.
|
|
|
* @param int $size The size of the icon in pixels (the icon is quadratic).
|
|
* @param int $size The size of the icon in pixels (the icon is quadratic).
|
|
|
* @return \Jdenticon\Identicon
|
|
* @return \Jdenticon\Identicon
|
|
|
*/
|
|
*/
|
|
|
- public static function fromHash($hash, $size)
|
|
|
|
|
|
|
+ public static function fromHash(string $hash, int $size): self
|
|
|
{
|
|
{
|
|
|
- return new Identicon(array('hash' => $hash, 'size' => $size));
|
|
|
|
|
|
|
+ return new Identicon(['hash' => $hash, 'size' => $size]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Creates an Identicon instance from a specified value.
|
|
* Creates an Identicon instance from a specified value.
|
|
|
*
|
|
*
|
|
|
- * @param mixed $value The value that will be used as base for this icon.
|
|
|
|
|
- * The value will be converted to a UTF8 encoded string and then hashed
|
|
|
|
|
|
|
+ * @param mixed $value The value that will be used as base for this icon.
|
|
|
|
|
+ * The value will be converted to a UTF8 encoded string and then hashed
|
|
|
* using SHA1.
|
|
* using SHA1.
|
|
|
* @param int $size The size of the icon in pixels (the icon is quadratic).
|
|
* @param int $size The size of the icon in pixels (the icon is quadratic).
|
|
|
* @return \Jdenticon\Identicon
|
|
* @return \Jdenticon\Identicon
|
|
|
*/
|
|
*/
|
|
|
- public static function fromValue($value, $size)
|
|
|
|
|
|
|
+ public static function fromValue($value, int $size): self
|
|
|
{
|
|
{
|
|
|
- return new Identicon(array('value' => $value, 'size' => $size));
|
|
|
|
|
|
|
+ return new Identicon(['value' => $value, 'size' => $size]);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Gets an associative array of all options of this identicon.
|
|
* Gets an associative array of all options of this identicon.
|
|
|
*
|
|
*
|
|
|
- * @return array
|
|
|
|
|
|
|
+ * @return array<string, mixed>
|
|
|
*/
|
|
*/
|
|
|
- public function getOptions()
|
|
|
|
|
|
|
+ public function getOptions(): array
|
|
|
{
|
|
{
|
|
|
- $options = array();
|
|
|
|
|
-
|
|
|
|
|
|
|
+ $options = [];
|
|
|
|
|
+
|
|
|
if ($this->valueSet) {
|
|
if ($this->valueSet) {
|
|
|
$options['value'] = $this->getValue();
|
|
$options['value'] = $this->getValue();
|
|
|
} elseif ($this->hash !== null) {
|
|
} elseif ($this->hash !== null) {
|
|
|
$options['hash'] = $this->getHash();
|
|
$options['hash'] = $this->getHash();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
$options['size'] = $this->getSize();
|
|
$options['size'] = $this->getSize();
|
|
|
$options['style'] = $this->getStyle()->getOptions();
|
|
$options['style'] = $this->getStyle()->getOptions();
|
|
|
|
|
|
|
|
if ($this->enableImageMagick !== null) {
|
|
if ($this->enableImageMagick !== null) {
|
|
|
$options['enableImageMagick'] = $this->getEnableImageMagick();
|
|
$options['enableImageMagick'] = $this->getEnableImageMagick();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if ($this->iconGenerator !== IconGenerator::getDefaultGenerator()) {
|
|
if ($this->iconGenerator !== IconGenerator::getDefaultGenerator()) {
|
|
|
$options['iconGenerator'] = $this->getIconGenerator();
|
|
$options['iconGenerator'] = $this->getIconGenerator();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return $options;
|
|
return $options;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * Sets options in this identicon by specifying an associative array of
|
|
|
|
|
|
|
+ * Sets options in this identicon by specifying an associative array of
|
|
|
* option values.
|
|
* option values.
|
|
|
*
|
|
*
|
|
|
- * @param array $options Options to set.
|
|
|
|
|
|
|
+ * @param array<string, mixed> $options Options to set.
|
|
|
* @return self
|
|
* @return self
|
|
|
*/
|
|
*/
|
|
|
- public function setOptions(array $options)
|
|
|
|
|
|
|
+ public function setOptions(array $options): self
|
|
|
{
|
|
{
|
|
|
foreach ($options as $key => $value) {
|
|
foreach ($options as $key => $value) {
|
|
|
$this->__set($key, $value);
|
|
$this->__set($key, $value);
|
|
|
}
|
|
}
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- public function __get($name)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public function __get(string $name)
|
|
|
{
|
|
{
|
|
|
switch (strtolower($name)) {
|
|
switch (strtolower($name)) {
|
|
|
case 'size':
|
|
case 'size':
|
|
@@ -173,8 +154,8 @@ class Identicon
|
|
|
"Unknown Identicon option '$name'.");
|
|
"Unknown Identicon option '$name'.");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- public function __set($name, $value)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public function __set(string $name, $value)
|
|
|
{
|
|
{
|
|
|
switch (strtolower($name)) {
|
|
switch (strtolower($name)) {
|
|
|
case 'size':
|
|
case 'size':
|
|
@@ -200,86 +181,80 @@ class Identicon
|
|
|
"Unknown Identicon option '$name'.");
|
|
"Unknown Identicon option '$name'.");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
* Gets the size of the icon in pixels.
|
|
* Gets the size of the icon in pixels.
|
|
|
*/
|
|
*/
|
|
|
- public function getSize()
|
|
|
|
|
|
|
+ public function getSize(): int
|
|
|
{
|
|
{
|
|
|
return $this->size;
|
|
return $this->size;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Sets the size of this icon in pixels.
|
|
* Sets the size of this icon in pixels.
|
|
|
*
|
|
*
|
|
|
* @param int|float|double $size The width and height of the icon.
|
|
* @param int|float|double $size The width and height of the icon.
|
|
|
*/
|
|
*/
|
|
|
- public function setSize($size)
|
|
|
|
|
|
|
+ public function setSize($size): void
|
|
|
{
|
|
{
|
|
|
if (!is_numeric($size) || $size < 1) {
|
|
if (!is_numeric($size) || $size < 1) {
|
|
|
throw new \InvalidArgumentException(
|
|
throw new \InvalidArgumentException(
|
|
|
"An invalid identicon size was specified. ".
|
|
"An invalid identicon size was specified. ".
|
|
|
"A numeric value >= 1 was expected. Specified value: $size.");
|
|
"A numeric value >= 1 was expected. Specified value: $size.");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
$this->size = (int)$size;
|
|
$this->size = (int)$size;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
|
|
+ /**
|
|
|
* Gets the size of the icon in pixels.
|
|
* Gets the size of the icon in pixels.
|
|
|
*/
|
|
*/
|
|
|
- public function getEnableImageMagick()
|
|
|
|
|
|
|
+ public function getEnableImageMagick(): bool
|
|
|
{
|
|
{
|
|
|
- // Enable ImageMagick on PHP < 7. On PHP 7 the performance increase
|
|
|
|
|
- // 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) {
|
|
if ($this->enableImageMagick === null) {
|
|
|
- return PHP_MAJOR_VERSION < 7 && extension_loaded('imagick');
|
|
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return $this->enableImageMagick;
|
|
return $this->enableImageMagick;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Sets whether ImageMagick should be used to generate PNG icons.
|
|
* Sets whether ImageMagick should be used to generate PNG icons.
|
|
|
*
|
|
*
|
|
|
* @param bool $enable true to enable ImageMagick.
|
|
* @param bool $enable true to enable ImageMagick.
|
|
|
*/
|
|
*/
|
|
|
- public function setEnableImageMagick($enable)
|
|
|
|
|
|
|
+ public function setEnableImageMagick(bool $enable): void
|
|
|
{
|
|
{
|
|
|
- if (!is_bool($enable)) {
|
|
|
|
|
- throw new \InvalidArgumentException(
|
|
|
|
|
- "enableImageMagick can only assume boolean values. Specified value: $enable.");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
// Verify that the Imagick extension is installed
|
|
// Verify that the Imagick extension is installed
|
|
|
if ($enable && !extension_loaded('imagick')) {
|
|
if ($enable && !extension_loaded('imagick')) {
|
|
|
throw new \Exception(
|
|
throw new \Exception(
|
|
|
'Failed to enable ImageMagick. '.
|
|
'Failed to enable ImageMagick. '.
|
|
|
'The Imagick PHP extension was not found on this system.');
|
|
'The Imagick PHP extension was not found on this system.');
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
$this->enableImageMagick = $enable;
|
|
$this->enableImageMagick = $enable;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Gets the {@see IconGenerator} used to generate icons.
|
|
* Gets the {@see IconGenerator} used to generate icons.
|
|
|
*
|
|
*
|
|
|
* @return \Jdenticon\Rendering\IconGenerator
|
|
* @return \Jdenticon\Rendering\IconGenerator
|
|
|
*/
|
|
*/
|
|
|
- public function getIconGenerator()
|
|
|
|
|
|
|
+ public function getIconGenerator(): IconGenerator
|
|
|
{
|
|
{
|
|
|
return $this->iconGenerator;
|
|
return $this->iconGenerator;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Sets the {@see IconGenerator} used to generate icons.
|
|
* Sets the {@see IconGenerator} used to generate icons.
|
|
|
*
|
|
*
|
|
|
- * @param \Jdenticon\Rendering\IconGenerator $iconGenerator Icon generator
|
|
|
|
|
|
|
+ * @param \Jdenticon\Rendering\IconGenerator $iconGenerator Icon generator
|
|
|
* that will render the shapes of the identicon.
|
|
* that will render the shapes of the identicon.
|
|
|
* @return \Jdenticon\Identicon
|
|
* @return \Jdenticon\Identicon
|
|
|
*/
|
|
*/
|
|
|
- public function setIconGenerator(IconGenerator $iconGenerator)
|
|
|
|
|
|
|
+ public function setIconGenerator(IconGenerator $iconGenerator): self
|
|
|
{
|
|
{
|
|
|
if ($iconGenerator === null) {
|
|
if ($iconGenerator === null) {
|
|
|
$iconGenerator = IconGenerator::getDefaultGenerator();
|
|
$iconGenerator = IconGenerator::getDefaultGenerator();
|
|
@@ -287,25 +262,25 @@ class Identicon
|
|
|
$this->iconGenerator = $iconGenerator;
|
|
$this->iconGenerator = $iconGenerator;
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Gets or sets the style of the icon.
|
|
* Gets or sets the style of the icon.
|
|
|
*
|
|
*
|
|
|
* @return \Jdenticon\IdenticonStyle
|
|
* @return \Jdenticon\IdenticonStyle
|
|
|
*/
|
|
*/
|
|
|
- public function getStyle()
|
|
|
|
|
|
|
+ public function getStyle(): IdenticonStyle
|
|
|
{
|
|
{
|
|
|
return $this->style;
|
|
return $this->style;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Gets or sets the style of the icon.
|
|
* Gets or sets the style of the icon.
|
|
|
*
|
|
*
|
|
|
- * @param array|\Jdenticon\IdenticonStyle $style The new style of the icon.
|
|
|
|
|
|
|
+ * @param array<string, mixed>|\Jdenticon\IdenticonStyle $style The new style of the icon.
|
|
|
* NULL will revert the identicon to use the default style.
|
|
* NULL will revert the identicon to use the default style.
|
|
|
* @return self
|
|
* @return self
|
|
|
*/
|
|
*/
|
|
|
- public function setStyle($style)
|
|
|
|
|
|
|
+ public function setStyle($style): self
|
|
|
{
|
|
{
|
|
|
if ($style == null) {
|
|
if ($style == null) {
|
|
|
$this->style = new IdenticonStyle();
|
|
$this->style = new IdenticonStyle();
|
|
@@ -319,84 +294,79 @@ class Identicon
|
|
|
"Allowed values are IdenticonStyle instances and associative ".
|
|
"Allowed values are IdenticonStyle instances and associative ".
|
|
|
"arrays containing IdenticonStyle options.");
|
|
"arrays containing IdenticonStyle options.");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Gets a binary string containing the hash that is used as base for this
|
|
|
|
|
|
|
+ * Gets a binary string containing the hash that is used as base for this
|
|
|
* icon.
|
|
* icon.
|
|
|
*/
|
|
*/
|
|
|
- public function getHash()
|
|
|
|
|
|
|
+ public function getHash(): string
|
|
|
{
|
|
{
|
|
|
return $this->hash;
|
|
return $this->hash;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * Sets a binary string containing the hash that is used as base for this
|
|
|
|
|
|
|
+ * Sets a binary string containing the hash that is used as base for this
|
|
|
* icon. The string should contain at least 6 bytes.
|
|
* icon. The string should contain at least 6 bytes.
|
|
|
*
|
|
*
|
|
|
* @param string $hash Binary string containing the hash.
|
|
* @param string $hash Binary string containing the hash.
|
|
|
*/
|
|
*/
|
|
|
- public function setHash($hash)
|
|
|
|
|
|
|
+ public function setHash(string $hash): self
|
|
|
{
|
|
{
|
|
|
- if (!is_string($hash)) {
|
|
|
|
|
- throw new \InvalidArgumentException(
|
|
|
|
|
- 'An invalid $hash was passed to Identicon. ' .
|
|
|
|
|
- 'A binary string was expected.');
|
|
|
|
|
- }
|
|
|
|
|
if (strlen($hash) < 6) {
|
|
if (strlen($hash) < 6) {
|
|
|
throw new \InvalidArgumentException(
|
|
throw new \InvalidArgumentException(
|
|
|
- 'An invalid $hash was passed to Identicon. ' .
|
|
|
|
|
|
|
+ 'An invalid $hash was passed to Identicon. ' .
|
|
|
'The hash was expected to contain at least 6 bytes.');
|
|
'The hash was expected to contain at least 6 bytes.');
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
$this->hash = $hash;
|
|
$this->hash = $hash;
|
|
|
$this->value = null;
|
|
$this->value = null;
|
|
|
$this->valueSet = false;
|
|
$this->valueSet = false;
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * Gets a binary string containing the hash that is used as base for this
|
|
|
|
|
|
|
+ * Gets a binary string containing the hash that is used as base for this
|
|
|
* icon.
|
|
* icon.
|
|
|
*/
|
|
*/
|
|
|
public function getValue()
|
|
public function getValue()
|
|
|
{
|
|
{
|
|
|
return $this->value;
|
|
return $this->value;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Sets a value that will be used as base for this icon. The value will
|
|
* Sets a value that will be used as base for this icon. The value will
|
|
|
* be converted to a string and then hashed using SHA1.
|
|
* be converted to a string and then hashed using SHA1.
|
|
|
*
|
|
*
|
|
|
* @param mixed $value Value that will be hashed.
|
|
* @param mixed $value Value that will be hashed.
|
|
|
*/
|
|
*/
|
|
|
- public function setValue($value)
|
|
|
|
|
|
|
+ public function setValue($value): self
|
|
|
{
|
|
{
|
|
|
$this->hash = sha1("$value");
|
|
$this->hash = sha1("$value");
|
|
|
$this->value = $value;
|
|
$this->value = $value;
|
|
|
$this->valueSet = true;
|
|
$this->valueSet = true;
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Gets the bounds of the icon excluding its padding.
|
|
* Gets the bounds of the icon excluding its padding.
|
|
|
*
|
|
*
|
|
|
* @return \Jdenticon\Rendering\Rectangle
|
|
* @return \Jdenticon\Rendering\Rectangle
|
|
|
*/
|
|
*/
|
|
|
- public function getIconBounds()
|
|
|
|
|
|
|
+ public function getIconBounds(): Rectangle
|
|
|
{
|
|
{
|
|
|
// Round padding to nearest integer
|
|
// Round padding to nearest integer
|
|
|
$padding = (int)($this->style->getPadding() * $this->size + 0.5);
|
|
$padding = (int)($this->style->getPadding() * $this->size + 0.5);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return new Rectangle(
|
|
return new Rectangle(
|
|
|
$padding, $padding,
|
|
$padding, $padding,
|
|
|
$this->size - $padding * 2,
|
|
$this->size - $padding * 2,
|
|
|
$this->size - $padding * 2);
|
|
$this->size - $padding * 2);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- private function getRenderer($imageFormat)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private function getRenderer(string $imageFormat): RendererInterface
|
|
|
{
|
|
{
|
|
|
switch (strtolower($imageFormat)) {
|
|
switch (strtolower($imageFormat)) {
|
|
|
case 'svg':
|
|
case 'svg':
|
|
@@ -408,26 +378,23 @@ class Identicon
|
|
|
new InternalPngRenderer($this->size, $this->size);
|
|
new InternalPngRenderer($this->size, $this->size);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Draws this icon using a specified renderer.
|
|
* Draws this icon using a specified renderer.
|
|
|
*
|
|
*
|
|
|
- * This method is only intended for usage with custom renderers. A custom
|
|
|
|
|
- * renderer could as an example render an Identicon in a file format not
|
|
|
|
|
- * natively supported by Jdenticon. To implement a new file format,
|
|
|
|
|
|
|
+ * This method is only intended for usage with custom renderers. A custom
|
|
|
|
|
+ * renderer could as an example render an Identicon in a file format not
|
|
|
|
|
+ * natively supported by Jdenticon. To implement a new file format,
|
|
|
* implement {@see \Jdenticon\Rendering\RendererInterface}.
|
|
* implement {@see \Jdenticon\Rendering\RendererInterface}.
|
|
|
*
|
|
*
|
|
|
- * @param \Jdenticon\Rendering\RendererInterface $renderer The renderer used
|
|
|
|
|
|
|
+ * @param \Jdenticon\Rendering\RendererInterface $renderer The renderer used
|
|
|
* to render this icon.
|
|
* to render this icon.
|
|
|
- * @param \Jdenticon\Rendering\Rectangle $rect The bounds of the rendered
|
|
|
|
|
|
|
+ * @param \Jdenticon\Rendering\Rectangle $rect The bounds of the rendered
|
|
|
* icon. No padding will be applied to the rectangle. If the parameter
|
|
* icon. No padding will be applied to the rectangle. If the parameter
|
|
|
* is omitted, the rectangle is calculated from the current icon
|
|
* is omitted, the rectangle is calculated from the current icon
|
|
|
* size and padding.
|
|
* size and padding.
|
|
|
*/
|
|
*/
|
|
|
- public function draw(
|
|
|
|
|
- \Jdenticon\Rendering\RendererInterface $renderer,
|
|
|
|
|
- \Jdenticon\Rendering\Rectangle $rect = null)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public function draw(RendererInterface $renderer, ?Rectangle $rect = null): void {
|
|
|
if ($rect === null) {
|
|
if ($rect === null) {
|
|
|
$rect = $this->getIconBounds();
|
|
$rect = $this->getIconBounds();
|
|
|
}
|
|
}
|
|
@@ -438,14 +405,14 @@ class Identicon
|
|
|
/**
|
|
/**
|
|
|
* Renders the icon directly to the page output.
|
|
* Renders the icon directly to the page output.
|
|
|
*
|
|
*
|
|
|
- * The method will set the 'Content-Type' HTTP header. You are recommended
|
|
|
|
|
- * to set an appropriate 'Cache-Control' header before calling this method
|
|
|
|
|
|
|
+ * The method will set the 'Content-Type' HTTP header. You are recommended
|
|
|
|
|
+ * to set an appropriate 'Cache-Control' header before calling this method
|
|
|
* to ensure the icon is cached client side.
|
|
* to ensure the icon is cached client side.
|
|
|
*
|
|
*
|
|
|
- * @param string $imageFormat The image format of the output.
|
|
|
|
|
|
|
+ * @param string $imageFormat The image format of the output.
|
|
|
* Supported values are 'png' and 'svg'.
|
|
* Supported values are 'png' and 'svg'.
|
|
|
*/
|
|
*/
|
|
|
- public function displayImage($imageFormat = 'png')
|
|
|
|
|
|
|
+ public function displayImage(string $imageFormat = 'png'): void
|
|
|
{
|
|
{
|
|
|
$renderer = $this->getRenderer($imageFormat);
|
|
$renderer = $this->getRenderer($imageFormat);
|
|
|
$this->draw($renderer, $this->getIconBounds());
|
|
$this->draw($renderer, $this->getIconBounds());
|
|
@@ -454,32 +421,32 @@ class Identicon
|
|
|
header("Content-Type: $mimeType");
|
|
header("Content-Type: $mimeType");
|
|
|
echo $data;
|
|
echo $data;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Renders the icon to a binary string.
|
|
* Renders the icon to a binary string.
|
|
|
*
|
|
*
|
|
|
- * @param string $imageFormat The image format of the output string.
|
|
|
|
|
|
|
+ * @param string $imageFormat The image format of the output string.
|
|
|
* Supported values are 'png' and 'svg'.
|
|
* Supported values are 'png' and 'svg'.
|
|
|
* @return string
|
|
* @return string
|
|
|
*/
|
|
*/
|
|
|
- public function getImageData($imageFormat = 'png')
|
|
|
|
|
|
|
+ public function getImageData(string $imageFormat = 'png'): string
|
|
|
{
|
|
{
|
|
|
$renderer = $this->getRenderer($imageFormat);
|
|
$renderer = $this->getRenderer($imageFormat);
|
|
|
$this->draw($renderer, $this->getIconBounds());
|
|
$this->draw($renderer, $this->getIconBounds());
|
|
|
return $renderer->getData();
|
|
return $renderer->getData();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * Renders the icon as a data URI. It is recommended to avoid using this
|
|
|
|
|
- * method unless really necessary, since it will effectively disable client
|
|
|
|
|
- * caching of generated icons, and will also cause the same icon to be
|
|
|
|
|
|
|
+ * Renders the icon as a data URI. It is recommended to avoid using this
|
|
|
|
|
+ * method unless really necessary, since it will effectively disable client
|
|
|
|
|
+ * caching of generated icons, and will also cause the same icon to be
|
|
|
* rendered multiple times, when used multiple times on a single page.
|
|
* rendered multiple times, when used multiple times on a single page.
|
|
|
*
|
|
*
|
|
|
- * @param string $imageFormat The image format of the data URI.
|
|
|
|
|
|
|
+ * @param string $imageFormat The image format of the data URI.
|
|
|
* Supported values are 'png' and 'svg'.
|
|
* Supported values are 'png' and 'svg'.
|
|
|
* @return string
|
|
* @return string
|
|
|
*/
|
|
*/
|
|
|
- public function getImageDataUri($imageFormat = 'png')
|
|
|
|
|
|
|
+ public function getImageDataUri(string $imageFormat = 'png'): string
|
|
|
{
|
|
{
|
|
|
$renderer = $this->getRenderer($imageFormat);
|
|
$renderer = $this->getRenderer($imageFormat);
|
|
|
$this->draw($renderer, $this->getIconBounds());
|
|
$this->draw($renderer, $this->getIconBounds());
|
|
@@ -488,5 +455,3 @@ class Identicon
|
|
|
return "data:$mimeType;base64,$base64";
|
|
return "data:$mimeType;base64,$base64";
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-
|
|
|