Vizhash16x16.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.4 beta PrivateBin 0.22
  12. */
  13. namespace PrivateBin;
  14. /**
  15. * Vizhash16x16
  16. *
  17. * Example:
  18. * $vz = new Vizhash16x16();
  19. * $data = $vz->generate('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. * @return void
  59. */
  60. public function __construct()
  61. {
  62. $this->width = 16;
  63. $this->height = 16;
  64. }
  65. /**
  66. * Generate a 16x16 png corresponding to $text.
  67. *
  68. * The given text should to be 128 to 150 characters long
  69. *
  70. * @access public
  71. * @param string $text
  72. * @return string PNG data. Or empty string if GD is not available.
  73. */
  74. public function generate($text)
  75. {
  76. if (!function_exists('gd_info')) {
  77. return '';
  78. }
  79. $textlen = strlen($text);
  80. // We convert the hash into an array of integers.
  81. $this->VALUES = array();
  82. for ($i = 0; $i < $textlen; $i = $i + 2) {
  83. array_push($this->VALUES, hexdec(substr($text, $i, 2)));
  84. }
  85. $this->VALUES_INDEX = 0; // to walk the array.
  86. // Then use these integers to drive the creation of an image.
  87. $image = imagecreatetruecolor($this->width, $this->height);
  88. $r = $r0 = $this->getInt();
  89. $g = $g0 = $this->getInt();
  90. $b = $b0 = $this->getInt();
  91. // First, create an image with a specific gradient background.
  92. $op = 'v';
  93. if (($this->getInt() % 2) == 0) {
  94. $op = 'h';
  95. };
  96. $image = $this->degrade($image, $op, array($r0, $g0, $b0), array(0, 0, 0));
  97. for ($i = 0; $i < 7; ++$i) {
  98. $action = $this->getInt();
  99. $color = imagecolorallocate($image, $r, $g, $b);
  100. $r = $r0 = ($r0 + $this->getInt() / 25) % 256;
  101. $g = $g0 = ($g0 + $this->getInt() / 25) % 256;
  102. $b = $b0 = ($b0 + $this->getInt() / 25) % 256;
  103. $this->drawshape($image, $action, $color);
  104. }
  105. $color = imagecolorallocate($image, $this->getInt(), $this->getInt(), $this->getInt());
  106. $this->drawshape($image, $this->getInt(), $color);
  107. ob_start();
  108. imagepng($image);
  109. $imagedata = ob_get_contents();
  110. ob_end_clean();
  111. imagedestroy($image);
  112. return $imagedata;
  113. }
  114. /**
  115. * Returns a single integer from the $VALUES array (0...255)
  116. *
  117. * @access private
  118. * @return int
  119. */
  120. private function getInt()
  121. {
  122. $v = $this->VALUES[$this->VALUES_INDEX];
  123. ++$this->VALUES_INDEX;
  124. $this->VALUES_INDEX %= count($this->VALUES); // Warp around the array
  125. return $v;
  126. }
  127. /**
  128. * Returns a single integer from the array (roughly mapped to image width)
  129. *
  130. * @access private
  131. * @return int
  132. */
  133. private function getX()
  134. {
  135. return $this->width * $this->getInt() / 256;
  136. }
  137. /**
  138. * Returns a single integer from the array (roughly mapped to image height)
  139. *
  140. * @access private
  141. * @return int
  142. */
  143. private function getY()
  144. {
  145. return $this->height * $this->getInt() / 256;
  146. }
  147. /**
  148. * Gradient function
  149. *
  150. * taken from:
  151. * http://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
  152. *
  153. * @access private
  154. * @param resource $img
  155. * @param string $direction
  156. * @param array $color1
  157. * @param array $color2
  158. * @return resource
  159. */
  160. private function degrade($img, $direction, $color1, $color2)
  161. {
  162. if ($direction == 'h') {
  163. $size = imagesx($img);
  164. $sizeinv = imagesy($img);
  165. } else {
  166. $size = imagesy($img);
  167. $sizeinv = imagesx($img);
  168. }
  169. $diffs = array(
  170. (($color2[0] - $color1[0]) / $size),
  171. (($color2[1] - $color1[1]) / $size),
  172. (($color2[2] - $color1[2]) / $size)
  173. );
  174. for ($i = 0; $i < $size; ++$i) {
  175. $r = $color1[0] + ($diffs[0] * $i);
  176. $g = $color1[1] + ($diffs[1] * $i);
  177. $b = $color1[2] + ($diffs[2] * $i);
  178. if ($direction == 'h') {
  179. imageline($img, $i, 0, $i, $sizeinv, imagecolorallocate($img, $r, $g, $b));
  180. } else {
  181. imageline($img, 0, $i, $sizeinv, $i, imagecolorallocate($img, $r, $g, $b));
  182. }
  183. }
  184. return $img;
  185. }
  186. /**
  187. * Draw a shape
  188. *
  189. * @access private
  190. * @param resource $image
  191. * @param int $action
  192. * @param int $color
  193. * @return void
  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. ImageFilledPolygon($image, $points, 4, $color);
  208. break;
  209. default:
  210. $start = $this->getInt() * 360 /256;
  211. $end = $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. }