Vizhash16x16.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php declare(strict_types=1);
  2. /**
  3. * VizHash_GD
  4. *
  5. * Visual Hash implementation in php4+GD,
  6. * stripped down from version 0.0.5 beta, modified for PrivateBin
  7. *
  8. * @link https://sebsauvage.net/wiki/doku.php?id=php:vizhash_gd
  9. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  10. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  11. */
  12. namespace PrivateBin;
  13. /**
  14. * Vizhash16x16
  15. *
  16. * Example:
  17. * $vz = new Vizhash16x16();
  18. * $data = $vz->generate(sha512('hello'));
  19. * header('Content-type: image/png');
  20. * echo $data;
  21. * exit;
  22. */
  23. class Vizhash16x16
  24. {
  25. /**
  26. * hash values
  27. *
  28. * @access private
  29. * @var array
  30. */
  31. private $VALUES;
  32. /**
  33. * index of current value
  34. *
  35. * @access private
  36. * @var int
  37. */
  38. private $VALUES_INDEX;
  39. /**
  40. * image width
  41. *
  42. * @access private
  43. * @var int
  44. */
  45. private $width;
  46. /**
  47. * image height
  48. *
  49. * @access private
  50. * @var int
  51. */
  52. private $height;
  53. /**
  54. * constructor
  55. *
  56. * @access public
  57. */
  58. public function __construct()
  59. {
  60. $this->width = 16;
  61. $this->height = 16;
  62. }
  63. /**
  64. * Generate a 16x16 png corresponding to $text.
  65. *
  66. * The given text should to be 128 to 150 characters long
  67. *
  68. * @access public
  69. * @param string $text
  70. * @return string PNG data. Or empty string if GD is not available.
  71. */
  72. public function generate($text)
  73. {
  74. if (!function_exists('gd_info')) {
  75. return '';
  76. }
  77. $textlen = strlen($text);
  78. // We convert the hash into an array of integers.
  79. $this->VALUES = [];
  80. for ($i = 0; $i < $textlen; $i = $i + 2) {
  81. array_push($this->VALUES, hexdec(substr($text, $i, 2)));
  82. }
  83. $this->VALUES_INDEX = 0; // to walk the array.
  84. // Then use these integers to drive the creation of an image.
  85. $image = imagecreatetruecolor($this->width, $this->height);
  86. if ($image === false) {
  87. return '';
  88. }
  89. $r = $r0 = $this->getInt();
  90. $g = $g0 = $this->getInt();
  91. $b = $b0 = $this->getInt();
  92. // First, create an image with a specific gradient background.
  93. $op = 'v';
  94. if (($this->getInt() % 2) === 0) {
  95. $op = 'h';
  96. }
  97. $image = $this->degrade($image, $op, [$r0, $g0, $b0], [0, 0, 0]);
  98. for ($i = 0; $i < 7; ++$i) {
  99. $action = $this->getInt();
  100. $color = imagecolorallocate($image, $r, $g, $b);
  101. $r = $r0 = (int) ($r0 + $this->getInt() / 25) % 256;
  102. $g = $g0 = (int) ($g0 + $this->getInt() / 25) % 256;
  103. $b = $b0 = (int) ($b0 + $this->getInt() / 25) % 256;
  104. $this->drawshape($image, $action, $color);
  105. }
  106. $color = imagecolorallocate($image, $this->getInt(), $this->getInt(), $this->getInt());
  107. $this->drawshape($image, $this->getInt(), $color);
  108. ob_start();
  109. imagepng($image);
  110. $imagedata = ob_get_contents();
  111. ob_end_clean();
  112. if (version_compare(PHP_VERSION, '8.5', '<') && defined('imagedestroy')) {
  113. imagedestroy($image);
  114. }
  115. return $imagedata;
  116. }
  117. /**
  118. * Returns a single integer from the $VALUES array (0...255)
  119. *
  120. * @access private
  121. * @return int
  122. */
  123. private function getInt()
  124. {
  125. $v = $this->VALUES[$this->VALUES_INDEX];
  126. ++$this->VALUES_INDEX;
  127. $this->VALUES_INDEX %= count($this->VALUES); // Wrap around the array
  128. return $v;
  129. }
  130. /**
  131. * Returns a single integer from the array (roughly mapped to image width)
  132. *
  133. * @access private
  134. * @return int
  135. */
  136. private function getX()
  137. {
  138. return (int) ($this->width * $this->getInt() / 256);
  139. }
  140. /**
  141. * Returns a single integer from the array (roughly mapped to image height)
  142. *
  143. * @access private
  144. * @return int
  145. */
  146. private function getY()
  147. {
  148. return (int) ($this->height * $this->getInt() / 256);
  149. }
  150. /**
  151. * Gradient function
  152. *
  153. * taken from:
  154. * @link https://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
  155. *
  156. * @access private
  157. * @param resource|\GdImage $img
  158. * @param string $direction
  159. * @param array $color1
  160. * @param array $color2
  161. * @return resource|\GdImage
  162. */
  163. private function degrade($img, $direction, $color1, $color2)
  164. {
  165. if ($direction === 'h') {
  166. $size = imagesx($img);
  167. $sizeinv = imagesy($img);
  168. } else {
  169. $size = imagesy($img);
  170. $sizeinv = imagesx($img);
  171. }
  172. $diffs = [
  173. ($color2[0] - $color1[0]) / $size,
  174. ($color2[1] - $color1[1]) / $size,
  175. ($color2[2] - $color1[2]) / $size,
  176. ];
  177. for ($i = 0; $i < $size; ++$i) {
  178. $r = $color1[0] + ((int) $diffs[0] * $i);
  179. $g = $color1[1] + ((int) $diffs[1] * $i);
  180. $b = $color1[2] + ((int) $diffs[2] * $i);
  181. if ($direction === 'h') {
  182. imageline($img, $i, 0, $i, $sizeinv, imagecolorallocate($img, $r, $g, $b));
  183. } else {
  184. imageline($img, 0, $i, $sizeinv, $i, imagecolorallocate($img, $r, $g, $b));
  185. }
  186. }
  187. return $img;
  188. }
  189. /**
  190. * Draw a shape
  191. *
  192. * @access private
  193. * @param resource|\GdImage $image
  194. * @param int $action
  195. * @param int $color
  196. */
  197. private function drawshape($image, $action, $color)
  198. {
  199. switch ($action % 7) {
  200. case 0:
  201. imagefilledrectangle($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
  202. break;
  203. case 1:
  204. case 2:
  205. imagefilledellipse($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
  206. break;
  207. case 3:
  208. $points = [$this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY()];
  209. version_compare(PHP_VERSION, '8.1', '<') ? imagefilledpolygon($image, $points, 4, $color) : imagefilledpolygon($image, $points, $color);
  210. break;
  211. default:
  212. $start = (int) ($this->getInt() * 360 / 256);
  213. $end = (int) ($start + $this->getInt() * 180 / 256);
  214. imagefilledarc($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $start, $end, $color, IMG_ARC_PIE);
  215. }
  216. }
  217. }