vizhash16x16.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.19
  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. private function getInt() // Returns a single integer from the $VALUES array (0...255)
  77. {
  78. $v= $this->VALUES[$this->VALUES_INDEX];
  79. $this->VALUES_INDEX++;
  80. $this->VALUES_INDEX %= count($this->VALUES); // Warp around the array
  81. return $v;
  82. }
  83. private function getX() // Returns a single integer from the array (roughly mapped to image width)
  84. {
  85. return $this->width*$this->getInt()/256;
  86. }
  87. private function getY() // Returns a single integer from the array (roughly mapped to image height)
  88. {
  89. return $this->height*$this->getInt()/256;
  90. }
  91. # Gradient function taken from:
  92. # http://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
  93. private function degrade($img,$direction,$color1,$color2)
  94. {
  95. if($direction=='h') { $size = imagesx($img); $sizeinv = imagesy($img); }
  96. else { $size = imagesy($img); $sizeinv = imagesx($img);}
  97. $diffs = array(
  98. (($color2[0]-$color1[0])/$size),
  99. (($color2[1]-$color1[1])/$size),
  100. (($color2[2]-$color1[2])/$size)
  101. );
  102. for($i=0;$i<$size;$i++)
  103. {
  104. $r = $color1[0]+($diffs[0]*$i);
  105. $g = $color1[1]+($diffs[1]*$i);
  106. $b = $color1[2]+($diffs[2]*$i);
  107. if($direction=='h') { imageline($img,$i,0,$i,$sizeinv,imagecolorallocate($img,$r,$g,$b)); }
  108. else { imageline($img,0,$i,$sizeinv,$i,imagecolorallocate($img,$r,$g,$b)); }
  109. }
  110. return $img;
  111. }
  112. private function drawshape($image,$action,$color)
  113. {
  114. switch($action%7)
  115. {
  116. case 0:
  117. ImageFilledRectangle ($image,$this->getX(),$this->getY(),$this->getX(),$this->getY(),$color);
  118. break;
  119. case 1:
  120. case 2:
  121. ImageFilledEllipse ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
  122. break;
  123. case 3:
  124. $points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(),$this->getX(), $this->getY());
  125. ImageFilledPolygon ($image, $points, 4, $color);
  126. break;
  127. default:
  128. $start=$this->getInt()*360/256; $end=$start+$this->getInt()*180/256;
  129. ImageFilledArc ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(),$start,$end,$color,IMG_ARC_PIE);
  130. }
  131. }
  132. }