1
0

vizhash16x16.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.17
  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. private $VALUES;
  26. private $VALUES_INDEX;
  27. private $width;
  28. private $height;
  29. private $salt;
  30. function __construct()
  31. {
  32. $this->width=16;
  33. $this->height=16;
  34. $this->salt = serversalt::get();
  35. }
  36. // Generate a 16x16 png corresponding to $text.
  37. // Input: $text (string)
  38. // Output: PNG data. Or empty string if GD is not available.
  39. function generate($text)
  40. {
  41. if (!function_exists('gd_info')) return '';
  42. // We hash the input string.
  43. $hash=hash('sha1',$text.$this->salt).hash('md5',$text.$this->salt);
  44. $hash=$hash.strrev($hash); # more data to make graphics
  45. // We convert the hash into an array of integers.
  46. $this->VALUES=array();
  47. for($i=0; $i<strlen($hash); $i=$i+2){ array_push($this->VALUES,hexdec(substr($hash,$i,2))); }
  48. $this->VALUES_INDEX=0; // to walk the array.
  49. // Then use these integers to drive the creation of an image.
  50. $image = imagecreatetruecolor($this->width,$this->height);
  51. $r0 = $this->getInt();$r=$r0;
  52. $g0 = $this->getInt();$g=$g0;
  53. $b0 = $this->getInt();$b=$b0;
  54. // First, create an image with a specific gradient background.
  55. $op='v'; if (($this->getInt()%2)==0) { $op='h'; };
  56. $image = $this->degrade($image,$op,array($r0,$g0,$b0),array(0,0,0));
  57. for($i=0; $i<7; $i=$i+1)
  58. {
  59. $action=$this->getInt();
  60. $color = imagecolorallocate($image, $r,$g,$b);
  61. $r = ($r0 + $this->getInt()/25)%256;
  62. $g = ($g0 + $this->getInt()/25)%256;
  63. $b = ($b0 + $this->getInt()/25)%256;
  64. $r0=$r; $g0=$g; $b0=$b;
  65. $this->drawshape($image,$action,$color);
  66. }
  67. $color = imagecolorallocate($image,$this->getInt(),$this->getInt(),$this->getInt());
  68. $this->drawshape($image,$this->getInt(),$color);
  69. ob_start();
  70. imagepng($image);
  71. $imagedata = ob_get_contents();
  72. ob_end_clean();
  73. imagedestroy($image);
  74. return $imagedata;
  75. }
  76. // Generate a large random hexadecimal salt.
  77. private function randomSalt()
  78. {
  79. $randomSalt='';
  80. for($i=0;$i<6;$i++) { $randomSalt.=base_convert(mt_rand(),10,16); }
  81. return $randomSalt;
  82. }
  83. private function getInt() // Returns a single integer from the $VALUES array (0...255)
  84. {
  85. $v= $this->VALUES[$this->VALUES_INDEX];
  86. $this->VALUES_INDEX++;
  87. $this->VALUES_INDEX %= count($this->VALUES); // Warp around the array
  88. return $v;
  89. }
  90. private function getX() // Returns a single integer from the array (roughly mapped to image width)
  91. {
  92. return $this->width*$this->getInt()/256;
  93. }
  94. private function getY() // Returns a single integer from the array (roughly mapped to image height)
  95. {
  96. return $this->height*$this->getInt()/256;
  97. }
  98. # Gradient function taken from:
  99. # http://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
  100. private function degrade($img,$direction,$color1,$color2)
  101. {
  102. if($direction=='h') { $size = imagesx($img); $sizeinv = imagesy($img); }
  103. else { $size = imagesy($img); $sizeinv = imagesx($img);}
  104. $diffs = array(
  105. (($color2[0]-$color1[0])/$size),
  106. (($color2[1]-$color1[1])/$size),
  107. (($color2[2]-$color1[2])/$size)
  108. );
  109. for($i=0;$i<$size;$i++)
  110. {
  111. $r = $color1[0]+($diffs[0]*$i);
  112. $g = $color1[1]+($diffs[1]*$i);
  113. $b = $color1[2]+($diffs[2]*$i);
  114. if($direction=='h') { imageline($img,$i,0,$i,$sizeinv,imagecolorallocate($img,$r,$g,$b)); }
  115. else { imageline($img,0,$i,$sizeinv,$i,imagecolorallocate($img,$r,$g,$b)); }
  116. }
  117. return $img;
  118. }
  119. private function drawshape($image,$action,$color)
  120. {
  121. switch($action%7)
  122. {
  123. case 0:
  124. ImageFilledRectangle ($image,$this->getX(),$this->getY(),$this->getX(),$this->getY(),$color);
  125. break;
  126. case 1:
  127. case 2:
  128. ImageFilledEllipse ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
  129. break;
  130. case 3:
  131. $points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(),$this->getX(), $this->getY());
  132. ImageFilledPolygon ($image, $points, 4, $color);
  133. break;
  134. case 4:
  135. case 5:
  136. case 6:
  137. $start=$this->getInt()*360/256; $end=$start+$this->getInt()*180/256;
  138. ImageFilledArc ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(),$start,$end,$color,IMG_ARC_PIE);
  139. break;
  140. }
  141. }
  142. }
  143. ?>