vizhash16x16.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * VizHash_GD
  4. *
  5. * Visual Hash implementation in php4+GD,
  6. * stripped down and modified version for ZeroBin
  7. *
  8. * @link http://sebsauvage.net/wiki/doku.php?id=php:vizhash_gd
  9. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  10. * @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  11. * @version 0.0.4 beta ZeroBin 0.20
  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. // We convert the hash into an array of integers.
  86. $this->VALUES=array();
  87. for($i=0; $i<strlen($hash); $i=$i+2){ array_push($this->VALUES,hexdec(substr($hash,$i,2))); }
  88. $this->VALUES_INDEX=0; // to walk the array.
  89. // Then use these integers to drive the creation of an image.
  90. $image = imagecreatetruecolor($this->width,$this->height);
  91. $r0 = $this->getInt();$r=$r0;
  92. $g0 = $this->getInt();$g=$g0;
  93. $b0 = $this->getInt();$b=$b0;
  94. // First, create an image with a specific gradient background.
  95. $op='v'; if (($this->getInt()%2)==0) { $op='h'; };
  96. $image = $this->degrade($image,$op,array($r0,$g0,$b0),array(0,0,0));
  97. for($i=0; $i<7; $i=$i+1)
  98. {
  99. $action=$this->getInt();
  100. $color = imagecolorallocate($image, $r,$g,$b);
  101. $r = ($r0 + $this->getInt()/25)%256;
  102. $g = ($g0 + $this->getInt()/25)%256;
  103. $b = ($b0 + $this->getInt()/25)%256;
  104. $r0=$r; $g0=$g; $b0=$b;
  105. $this->drawshape($image,$action,$color);
  106. }
  107. $color = imagecolorallocate($image,$this->getInt(),$this->getInt(),$this->getInt());
  108. $this->drawshape($image,$this->getInt(),$color);
  109. ob_start();
  110. imagepng($image);
  111. $imagedata = ob_get_contents();
  112. ob_end_clean();
  113. imagedestroy($image);
  114. return $imagedata;
  115. }
  116. /**
  117. * Returns a single integer from the $VALUES array (0...255)
  118. *
  119. * @access private
  120. * @return int
  121. */
  122. private function getInt()
  123. {
  124. $v= $this->VALUES[$this->VALUES_INDEX];
  125. $this->VALUES_INDEX++;
  126. $this->VALUES_INDEX %= count($this->VALUES); // Warp around the array
  127. return $v;
  128. }
  129. /**
  130. * Returns a single integer from the array (roughly mapped to image width)
  131. *
  132. * @access private
  133. * @return int
  134. */
  135. private function getX()
  136. {
  137. return $this->width*$this->getInt()/256;
  138. }
  139. /**
  140. * Returns a single integer from the array (roughly mapped to image height)
  141. *
  142. * @access private
  143. * @return int
  144. */
  145. private function getY()
  146. {
  147. return $this->height*$this->getInt()/256;
  148. }
  149. /**
  150. * Gradient function
  151. *
  152. * taken from:
  153. * http://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
  154. *
  155. * @access private
  156. * @param resource $img
  157. * @param string $direction
  158. * @param array $color1
  159. * @param array $color2
  160. * @return resource
  161. */
  162. private function degrade($img,$direction,$color1,$color2)
  163. {
  164. if($direction=='h') { $size = imagesx($img); $sizeinv = imagesy($img); }
  165. else { $size = imagesy($img); $sizeinv = imagesx($img);}
  166. $diffs = array(
  167. (($color2[0]-$color1[0])/$size),
  168. (($color2[1]-$color1[1])/$size),
  169. (($color2[2]-$color1[2])/$size)
  170. );
  171. for($i=0;$i<$size;$i++)
  172. {
  173. $r = $color1[0]+($diffs[0]*$i);
  174. $g = $color1[1]+($diffs[1]*$i);
  175. $b = $color1[2]+($diffs[2]*$i);
  176. if($direction=='h') { imageline($img,$i,0,$i,$sizeinv,imagecolorallocate($img,$r,$g,$b)); }
  177. else { imageline($img,0,$i,$sizeinv,$i,imagecolorallocate($img,$r,$g,$b)); }
  178. }
  179. return $img;
  180. }
  181. /**
  182. * Draw a shape
  183. *
  184. * @access private
  185. * @param resource $image
  186. * @param int $action
  187. * @param int $color
  188. * @return void
  189. */
  190. private function drawshape($image,$action,$color)
  191. {
  192. switch($action%7)
  193. {
  194. case 0:
  195. ImageFilledRectangle ($image,$this->getX(),$this->getY(),$this->getX(),$this->getY(),$color);
  196. break;
  197. case 1:
  198. case 2:
  199. ImageFilledEllipse ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
  200. break;
  201. case 3:
  202. $points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(),$this->getX(), $this->getY());
  203. ImageFilledPolygon ($image, $points, 4, $color);
  204. break;
  205. default:
  206. $start=$this->getInt()*360/256; $end=$start+$this->getInt()*180/256;
  207. ImageFilledArc ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(),$start,$end,$color,IMG_ARC_PIE);
  208. }
  209. }
  210. }