Vizhash16x16.php 6.3 KB

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