Vizhash16x16.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 = array();
  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. $r = $r0 = $this->getInt();
  87. $g = $g0 = $this->getInt();
  88. $b = $b0 = $this->getInt();
  89. // First, create an image with a specific gradient background.
  90. $op = 'v';
  91. if (($this->getInt() % 2) == 0) {
  92. $op = 'h';
  93. }
  94. $image = $this->degrade($image, $op, array($r0, $g0, $b0), array(0, 0, 0));
  95. for ($i = 0; $i < 7; ++$i) {
  96. $action = $this->getInt();
  97. $color = imagecolorallocate($image, $r, $g, $b);
  98. $r = $r0 = (int) ($r0 + $this->getInt() / 25) % 256;
  99. $g = $g0 = (int) ($g0 + $this->getInt() / 25) % 256;
  100. $b = $b0 = (int) ($b0 + $this->getInt() / 25) % 256;
  101. $this->drawshape($image, $action, $color);
  102. }
  103. $color = imagecolorallocate($image, $this->getInt(), $this->getInt(), $this->getInt());
  104. $this->drawshape($image, $this->getInt(), $color);
  105. ob_start();
  106. imagepng($image);
  107. $imagedata = ob_get_contents();
  108. ob_end_clean();
  109. imagedestroy($image);
  110. return $imagedata;
  111. }
  112. /**
  113. * Returns a single integer from the $VALUES array (0...255)
  114. *
  115. * @access private
  116. * @return int
  117. */
  118. private function getInt()
  119. {
  120. $v = $this->VALUES[$this->VALUES_INDEX];
  121. ++$this->VALUES_INDEX;
  122. $this->VALUES_INDEX %= count($this->VALUES); // Wrap around the array
  123. return $v;
  124. }
  125. /**
  126. * Returns a single integer from the array (roughly mapped to image width)
  127. *
  128. * @access private
  129. * @return int
  130. */
  131. private function getX()
  132. {
  133. return (int) ($this->width * $this->getInt() / 256);
  134. }
  135. /**
  136. * Returns a single integer from the array (roughly mapped to image height)
  137. *
  138. * @access private
  139. * @return int
  140. */
  141. private function getY()
  142. {
  143. return (int) ($this->height * $this->getInt() / 256);
  144. }
  145. /**
  146. * Gradient function
  147. *
  148. * taken from:
  149. * @link https://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
  150. *
  151. * @access private
  152. * @param resource $img
  153. * @param string $direction
  154. * @param array $color1
  155. * @param array $color2
  156. * @return resource
  157. */
  158. private function degrade($img, $direction, $color1, $color2)
  159. {
  160. if ($direction == 'h') {
  161. $size = imagesx($img);
  162. $sizeinv = imagesy($img);
  163. } else {
  164. $size = imagesy($img);
  165. $sizeinv = imagesx($img);
  166. }
  167. $diffs = array(
  168. ($color2[0] - $color1[0]) / $size,
  169. ($color2[1] - $color1[1]) / $size,
  170. ($color2[2] - $color1[2]) / $size,
  171. );
  172. for ($i = 0; $i < $size; ++$i) {
  173. $r = $color1[0] + ((int) $diffs[0] * $i);
  174. $g = $color1[1] + ((int) $diffs[1] * $i);
  175. $b = $color1[2] + ((int) $diffs[2] * $i);
  176. if ($direction == 'h') {
  177. imageline($img, $i, 0, $i, $sizeinv, imagecolorallocate($img, $r, $g, $b));
  178. } else {
  179. imageline($img, 0, $i, $sizeinv, $i, imagecolorallocate($img, $r, $g, $b));
  180. }
  181. }
  182. return $img;
  183. }
  184. /**
  185. * Draw a shape
  186. *
  187. * @access private
  188. * @param resource $image
  189. * @param int $action
  190. * @param int $color
  191. */
  192. private function drawshape($image, $action, $color)
  193. {
  194. switch ($action % 7) {
  195. case 0:
  196. imagefilledrectangle($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
  197. break;
  198. case 1:
  199. case 2:
  200. imagefilledellipse($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
  201. break;
  202. case 3:
  203. $points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY());
  204. version_compare(PHP_VERSION, '8.1', '<') ? imagefilledpolygon($image, $points, 4, $color) : imagefilledpolygon($image, $points, $color);
  205. break;
  206. default:
  207. $start = (int) ($this->getInt() * 360 / 256);
  208. $end = (int) ($start + $this->getInt() * 180 / 256);
  209. imagefilledarc($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $start, $end, $color, IMG_ARC_PIE);
  210. }
  211. }
  212. }