vizhash16x16.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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')) return '';
  83. // We hash the input string.
  84. $hash=hash('sha1',$text.$this->salt).hash('md5',$text.$this->salt);
  85. $hash=$hash.strrev($hash); # more data to make graphics
  86. $hashlen=strlen($hash);
  87. // We convert the hash into an array of integers.
  88. $this->VALUES=array();
  89. for($i=0; $i<$hashlen; $i=$i+2){ array_push($this->VALUES,hexdec(substr($hash,$i,2))); }
  90. $this->VALUES_INDEX=0; // to walk the array.
  91. // Then use these integers to drive the creation of an image.
  92. $image = imagecreatetruecolor($this->width,$this->height);
  93. $r0 = $this->getInt();$r=$r0;
  94. $g0 = $this->getInt();$g=$g0;
  95. $b0 = $this->getInt();$b=$b0;
  96. // First, create an image with a specific gradient background.
  97. $op='v'; if (($this->getInt()%2)==0) { $op='h'; };
  98. $image = $this->degrade($image,$op,array($r0,$g0,$b0),array(0,0,0));
  99. for($i=0; $i<7; $i=$i+1)
  100. {
  101. $action=$this->getInt();
  102. $color = imagecolorallocate($image, $r,$g,$b);
  103. $r = ($r0 + $this->getInt()/25)%256;
  104. $g = ($g0 + $this->getInt()/25)%256;
  105. $b = ($b0 + $this->getInt()/25)%256;
  106. $r0=$r; $g0=$g; $b0=$b;
  107. $this->drawshape($image,$action,$color);
  108. }
  109. $color = imagecolorallocate($image,$this->getInt(),$this->getInt(),$this->getInt());
  110. $this->drawshape($image,$this->getInt(),$color);
  111. ob_start();
  112. imagepng($image);
  113. $imagedata = ob_get_contents();
  114. ob_end_clean();
  115. imagedestroy($image);
  116. return $imagedata;
  117. }
  118. /**
  119. * Returns a single integer from the $VALUES array (0...255)
  120. *
  121. * @access private
  122. * @return int
  123. */
  124. private function getInt()
  125. {
  126. $v= $this->VALUES[$this->VALUES_INDEX];
  127. $this->VALUES_INDEX++;
  128. $this->VALUES_INDEX %= count($this->VALUES); // Warp around the array
  129. return $v;
  130. }
  131. /**
  132. * Returns a single integer from the array (roughly mapped to image width)
  133. *
  134. * @access private
  135. * @return int
  136. */
  137. private function getX()
  138. {
  139. return $this->width*$this->getInt()/256;
  140. }
  141. /**
  142. * Returns a single integer from the array (roughly mapped to image height)
  143. *
  144. * @access private
  145. * @return int
  146. */
  147. private function getY()
  148. {
  149. return $this->height*$this->getInt()/256;
  150. }
  151. /**
  152. * Gradient function
  153. *
  154. * taken from:
  155. * http://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
  156. *
  157. * @access private
  158. * @param resource $img
  159. * @param string $direction
  160. * @param array $color1
  161. * @param array $color2
  162. * @return resource
  163. */
  164. private function degrade($img,$direction,$color1,$color2)
  165. {
  166. if($direction=='h') { $size = imagesx($img); $sizeinv = imagesy($img); }
  167. else { $size = imagesy($img); $sizeinv = imagesx($img);}
  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. {
  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') { imageline($img,$i,0,$i,$sizeinv,imagecolorallocate($img,$r,$g,$b)); }
  179. else { imageline($img,0,$i,$sizeinv,$i,imagecolorallocate($img,$r,$g,$b)); }
  180. }
  181. return $img;
  182. }
  183. /**
  184. * Draw a shape
  185. *
  186. * @access private
  187. * @param resource $image
  188. * @param int $action
  189. * @param int $color
  190. * @return void
  191. */
  192. private function drawshape($image,$action,$color)
  193. {
  194. switch($action%7)
  195. {
  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; $end=$start+$this->getInt()*180/256;
  209. ImageFilledArc ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(),$start,$end,$color,IMG_ARC_PIE);
  210. }
  211. }
  212. }