vizhash16x16.php 6.3 KB

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