vizhash16x16.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. * salt used when generating the image
  56. *
  57. * @access private
  58. * @var string
  59. */
  60. private $salt;
  61. /**
  62. * constructor
  63. *
  64. * @access public
  65. * @return void
  66. */
  67. public function __construct()
  68. {
  69. $this->width = 16;
  70. $this->height = 16;
  71. $this->salt = serversalt::get();
  72. }
  73. /**
  74. * Generate a 16x16 png corresponding to $text.
  75. *
  76. * @access public
  77. * @param string $text
  78. * @return string PNG data. Or empty string if GD is not available.
  79. */
  80. public function generate($text)
  81. {
  82. if (!function_exists('gd_info')) {
  83. return '';
  84. }
  85. // We hash the input string.
  86. $hash=hash('sha1', $text.$this->salt).hash('md5', $text.$this->salt);
  87. $hash=$hash.strrev($hash); # more data to make graphics
  88. $hashlen=strlen($hash);
  89. // We convert the hash into an array of integers.
  90. $this->VALUES=array();
  91. for ($i=0; $i<$hashlen; $i=$i+2) {
  92. array_push($this->VALUES, hexdec(substr($hash, $i, 2)));
  93. }
  94. $this->VALUES_INDEX=0; // to walk the array.
  95. // Then use these integers to drive the creation of an image.
  96. $image = imagecreatetruecolor($this->width, $this->height);
  97. $r0 = $this->getInt();
  98. $r=$r0;
  99. $g0 = $this->getInt();
  100. $g=$g0;
  101. $b0 = $this->getInt();
  102. $b=$b0;
  103. // First, create an image with a specific gradient background.
  104. $op='v';
  105. if (($this->getInt()%2)==0) {
  106. $op='h';
  107. };
  108. $image = $this->degrade($image, $op, array($r0, $g0, $b0), array(0, 0, 0));
  109. for ($i=0; $i<7; $i=$i+1) {
  110. $action=$this->getInt();
  111. $color = imagecolorallocate($image, $r, $g, $b);
  112. $r = ($r0 + $this->getInt()/25)%256;
  113. $g = ($g0 + $this->getInt()/25)%256;
  114. $b = ($b0 + $this->getInt()/25)%256;
  115. $r0=$r;
  116. $g0=$g;
  117. $b0=$b;
  118. $this->drawshape($image, $action, $color);
  119. }
  120. $color = imagecolorallocate($image, $this->getInt(), $this->getInt(), $this->getInt());
  121. $this->drawshape($image, $this->getInt(), $color);
  122. ob_start();
  123. imagepng($image);
  124. $imagedata = ob_get_contents();
  125. ob_end_clean();
  126. imagedestroy($image);
  127. return $imagedata;
  128. }
  129. /**
  130. * Returns a single integer from the $VALUES array (0...255)
  131. *
  132. * @access private
  133. * @return int
  134. */
  135. private function getInt()
  136. {
  137. $v= $this->VALUES[$this->VALUES_INDEX];
  138. $this->VALUES_INDEX++;
  139. $this->VALUES_INDEX %= count($this->VALUES); // Warp around the array
  140. return $v;
  141. }
  142. /**
  143. * Returns a single integer from the array (roughly mapped to image width)
  144. *
  145. * @access private
  146. * @return int
  147. */
  148. private function getX()
  149. {
  150. return $this->width*$this->getInt()/256;
  151. }
  152. /**
  153. * Returns a single integer from the array (roughly mapped to image height)
  154. *
  155. * @access private
  156. * @return int
  157. */
  158. private function getY()
  159. {
  160. return $this->height*$this->getInt()/256;
  161. }
  162. /**
  163. * Gradient function
  164. *
  165. * taken from:
  166. * http://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
  167. *
  168. * @access private
  169. * @param resource $img
  170. * @param string $direction
  171. * @param array $color1
  172. * @param array $color2
  173. * @return resource
  174. */
  175. private function degrade($img, $direction, $color1, $color2)
  176. {
  177. if ($direction=='h') {
  178. $size = imagesx($img);
  179. $sizeinv = imagesy($img);
  180. } else {
  181. $size = imagesy($img);
  182. $sizeinv = imagesx($img);
  183. }
  184. $diffs = array(
  185. (($color2[0]-$color1[0])/$size),
  186. (($color2[1]-$color1[1])/$size),
  187. (($color2[2]-$color1[2])/$size)
  188. );
  189. for ($i=0;$i<$size;$i++) {
  190. $r = $color1[0]+($diffs[0]*$i);
  191. $g = $color1[1]+($diffs[1]*$i);
  192. $b = $color1[2]+($diffs[2]*$i);
  193. if ($direction=='h') {
  194. imageline($img, $i, 0, $i, $sizeinv, imagecolorallocate($img, $r, $g, $b));
  195. } else {
  196. imageline($img, 0, $i, $sizeinv, $i, imagecolorallocate($img, $r, $g, $b));
  197. }
  198. }
  199. return $img;
  200. }
  201. /**
  202. * Draw a shape
  203. *
  204. * @access private
  205. * @param resource $image
  206. * @param int $action
  207. * @param int $color
  208. * @return void
  209. */
  210. private function drawshape($image, $action, $color)
  211. {
  212. switch ($action%7) {
  213. case 0:
  214. ImageFilledRectangle($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
  215. break;
  216. case 1:
  217. case 2:
  218. ImageFilledEllipse($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
  219. break;
  220. case 3:
  221. $points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(),$this->getX(), $this->getY());
  222. ImageFilledPolygon($image, $points, 4, $color);
  223. break;
  224. default:
  225. $start=$this->getInt()*360/256; $end=$start+$this->getInt()*180/256;
  226. ImageFilledArc($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $start, $end, $color, IMG_ARC_PIE);
  227. }
  228. }
  229. }