Identicon.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Identicon;
  3. use Identicon\Generator\GdGenerator;
  4. use Identicon\Generator\GeneratorInterface;
  5. /**
  6. * @author Benjamin Laugueux <benjamin@yzalis.com>
  7. */
  8. class Identicon
  9. {
  10. /**
  11. * @var \Identicon\Generator\GeneratorInterface
  12. */
  13. private $generator;
  14. /**
  15. * Identicon constructor.
  16. *
  17. * @param \Identicon\Generator\GeneratorInterface|null $generator
  18. */
  19. public function __construct($generator = null)
  20. {
  21. if (null === $generator) {
  22. $this->generator = new GdGenerator();
  23. } else {
  24. $this->generator = $generator;
  25. }
  26. }
  27. /**
  28. * Set the image generator.
  29. *
  30. * @param \Identicon\Generator\GeneratorInterface $generator
  31. *
  32. * @return $this
  33. */
  34. public function setGenerator(GeneratorInterface $generator)
  35. {
  36. $this->generator = $generator;
  37. return $this;
  38. }
  39. /**
  40. * Display an Identicon image.
  41. *
  42. * @param string $string
  43. * @param int $size
  44. * @param string $color
  45. * @param string $backgroundColor
  46. */
  47. public function displayImage($string, $size = 64, $color = null, $backgroundColor = null)
  48. {
  49. header('Content-Type: '.$this->generator->getMimeType());
  50. echo $this->getImageData($string, $size, $color, $backgroundColor);
  51. }
  52. /**
  53. * Get an Identicon PNG image data.
  54. *
  55. * @param string $string
  56. * @param int $size
  57. * @param string $color
  58. * @param string $backgroundColor
  59. *
  60. * @return string
  61. */
  62. public function getImageData($string, $size = 64, $color = null, $backgroundColor = null)
  63. {
  64. return $this->generator->getImageBinaryData($string, $size, $color, $backgroundColor);
  65. }
  66. /**
  67. * Get an Identicon PNG image resource.
  68. *
  69. * @param string $string
  70. * @param int $size
  71. * @param string $color
  72. * @param string $backgroundColor
  73. *
  74. * @return string
  75. */
  76. public function getImageResource($string, $size = 64, $color = null, $backgroundColor = null)
  77. {
  78. return $this->generator->getImageResource($string, $size, $color, $backgroundColor);
  79. }
  80. /**
  81. * Get an Identicon PNG image data as base 64 encoded.
  82. *
  83. * @param string $string
  84. * @param int $size
  85. * @param string $color
  86. * @param string $backgroundColor
  87. *
  88. * @return string
  89. */
  90. public function getImageDataUri($string, $size = 64, $color = null, $backgroundColor = null)
  91. {
  92. return sprintf('data:%s;base64,%s', $this->generator->getMimeType(), base64_encode($this->getImageData($string, $size, $color, $backgroundColor)));
  93. }
  94. /**
  95. * Get the color of the Identicon
  96. *
  97. * Returns an array with RGB values of the Identicon's color. Colors may be NULL if no image has been generated
  98. * so far (e.g., when calling the method on a new Identicon()).
  99. *
  100. * @return array
  101. */
  102. public function getColor()
  103. {
  104. $colors = $this->generator->getColor();
  105. return [
  106. "r" => $colors[0],
  107. "g" => $colors[1],
  108. "b" => $colors[2]
  109. ];
  110. }
  111. }