Vizhash16x16.php 6.5 KB

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