2016-08-10 15:14:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Identicon;
|
|
|
|
|
|
|
|
use Identicon\Generator\GdGenerator;
|
|
|
|
use Identicon\Generator\GeneratorInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Benjamin Laugueux <benjamin@yzalis.com>
|
|
|
|
*/
|
|
|
|
class Identicon
|
|
|
|
{
|
|
|
|
/**
|
2019-06-16 07:10:24 +02:00
|
|
|
* @var \Identicon\Generator\GeneratorInterface
|
2016-08-10 15:14:50 +02:00
|
|
|
*/
|
|
|
|
private $generator;
|
|
|
|
|
2019-06-16 07:10:24 +02:00
|
|
|
/**
|
|
|
|
* Identicon constructor.
|
|
|
|
*
|
|
|
|
* @param \Identicon\Generator\GeneratorInterface|null $generator
|
|
|
|
*/
|
2016-08-10 15:14:50 +02:00
|
|
|
public function __construct($generator = null)
|
|
|
|
{
|
|
|
|
if (null === $generator) {
|
|
|
|
$this->generator = new GdGenerator();
|
|
|
|
} else {
|
|
|
|
$this->generator = $generator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-16 07:10:24 +02:00
|
|
|
* Set the image generator.
|
2016-08-10 15:14:50 +02:00
|
|
|
*
|
2019-06-16 07:10:24 +02:00
|
|
|
* @param \Identicon\Generator\GeneratorInterface $generator
|
2016-08-10 15:14:50 +02:00
|
|
|
*
|
2019-06-16 07:10:24 +02:00
|
|
|
* @return $this
|
2016-08-10 15:14:50 +02:00
|
|
|
*/
|
|
|
|
public function setGenerator(GeneratorInterface $generator)
|
|
|
|
{
|
|
|
|
$this->generator = $generator;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-16 07:10:24 +02:00
|
|
|
* Display an Identicon image.
|
2016-08-10 15:14:50 +02:00
|
|
|
*
|
2020-02-05 19:17:22 +01:00
|
|
|
* @param string $string
|
|
|
|
* @param int $size
|
|
|
|
* @param string|array $color
|
|
|
|
* @param string $backgroundColor
|
2016-08-10 15:14:50 +02:00
|
|
|
*/
|
|
|
|
public function displayImage($string, $size = 64, $color = null, $backgroundColor = null)
|
|
|
|
{
|
2019-06-16 07:10:24 +02:00
|
|
|
header('Content-Type: '.$this->generator->getMimeType());
|
2016-08-10 15:14:50 +02:00
|
|
|
echo $this->getImageData($string, $size, $color, $backgroundColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-16 07:10:24 +02:00
|
|
|
* Get an Identicon PNG image data.
|
2016-08-10 15:14:50 +02:00
|
|
|
*
|
2020-02-05 19:17:22 +01:00
|
|
|
* @param string $string
|
|
|
|
* @param int $size
|
|
|
|
* @param string|array $color
|
|
|
|
* @param string $backgroundColor
|
2016-08-10 15:14:50 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getImageData($string, $size = 64, $color = null, $backgroundColor = null)
|
|
|
|
{
|
|
|
|
return $this->generator->getImageBinaryData($string, $size, $color, $backgroundColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-16 07:10:24 +02:00
|
|
|
* Get an Identicon PNG image resource.
|
2016-08-10 15:14:50 +02:00
|
|
|
*
|
2020-02-05 19:17:22 +01:00
|
|
|
* @param string $string
|
|
|
|
* @param int $size
|
|
|
|
* @param string|array $color
|
|
|
|
* @param string $backgroundColor
|
2016-08-10 15:14:50 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getImageResource($string, $size = 64, $color = null, $backgroundColor = null)
|
|
|
|
{
|
|
|
|
return $this->generator->getImageResource($string, $size, $color, $backgroundColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-16 07:10:24 +02:00
|
|
|
* Get an Identicon PNG image data as base 64 encoded.
|
2016-08-10 15:14:50 +02:00
|
|
|
*
|
2020-02-05 19:17:22 +01:00
|
|
|
* @param string $string
|
|
|
|
* @param int $size
|
|
|
|
* @param string|array $color
|
|
|
|
* @param string $backgroundColor
|
2016-08-10 15:14:50 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getImageDataUri($string, $size = 64, $color = null, $backgroundColor = null)
|
|
|
|
{
|
2019-06-16 07:10:24 +02:00
|
|
|
return sprintf('data:%s;base64,%s', $this->generator->getMimeType(), base64_encode($this->getImageData($string, $size, $color, $backgroundColor)));
|
2016-08-10 15:14:50 +02:00
|
|
|
}
|
2020-02-05 19:17:22 +01:00
|
|
|
|
2019-06-16 07:10:24 +02:00
|
|
|
/**
|
|
|
|
* Get the color of the Identicon
|
|
|
|
*
|
|
|
|
* Returns an array with RGB values of the Identicon's color. Colors may be NULL if no image has been generated
|
|
|
|
* so far (e.g., when calling the method on a new Identicon()).
|
2020-02-05 19:17:22 +01:00
|
|
|
*
|
2019-06-16 07:10:24 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getColor()
|
|
|
|
{
|
|
|
|
$colors = $this->generator->getColor();
|
|
|
|
|
|
|
|
return [
|
|
|
|
"r" => $colors[0],
|
|
|
|
"g" => $colors[1],
|
|
|
|
"b" => $colors[2]
|
|
|
|
];
|
|
|
|
}
|
2016-08-10 15:14:50 +02:00
|
|
|
}
|