vizhash16x16.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // VizHash_GD 0.0.4 beta ZeroBin 0.15
  3. // Visual Hash implementation in php4+GD, stripped down and modified version for ZeroBin
  4. // See: http://sebsauvage.net/wiki/doku.php?id=php:vizhash_gd
  5. // This is free software under the zlib/libpng licence
  6. // http://www.opensource.org/licenses/zlib-license.php
  7. /* Example:
  8. $vz = new vizhash16x16();
  9. $data = $vz->generate('hello');
  10. header('Content-type: image/png');
  11. echo $data;
  12. exit;
  13. */
  14. class vizhash16x16
  15. {
  16. private $VALUES;
  17. private $VALUES_INDEX;
  18. private $width;
  19. private $height;
  20. private $salt;
  21. function __construct()
  22. {
  23. $this->width=16;
  24. $this->height=16;
  25. // Read salt from file (and create it if does not exist).
  26. // The salt will make vizhash avatar unique on each ZeroBin installation
  27. // to prevent IP checking.
  28. $saltfile = PATH . 'data/salt.php';
  29. if (!is_file($saltfile))
  30. file_put_contents($saltfile,'<?php /* |'.$this->randomSalt().'| */ ?>');
  31. $items=explode('|',file_get_contents($saltfile));
  32. $this->salt = $items[1];
  33. }
  34. // Generate a 16x16 png corresponding to $text.
  35. // Input: $text (string)
  36. // Output: PNG data. Or empty string if GD is not available.
  37. function generate($text)
  38. {
  39. if (!function_exists('gd_info')) return '';
  40. // We hash the input string.
  41. $hash=hash('sha1',$text.$this->salt).hash('md5',$text.$this->salt);
  42. $hash=$hash.strrev($hash); # more data to make graphics
  43. // We convert the hash into an array of integers.
  44. $this->VALUES=array();
  45. for($i=0; $i<strlen($hash); $i=$i+2){ array_push($this->VALUES,hexdec(substr($hash,$i,2))); }
  46. $this->VALUES_INDEX=0; // to walk the array.
  47. // Then use these integers to drive the creation of an image.
  48. $image = imagecreatetruecolor($this->width,$this->height);
  49. $r0 = $this->getInt();$r=$r0;
  50. $g0 = $this->getInt();$g=$g0;
  51. $b0 = $this->getInt();$b=$b0;
  52. // First, create an image with a specific gradient background.
  53. $op='v'; if (($this->getInt()%2)==0) { $op='h'; };
  54. $image = $this->degrade($image,$op,array($r0,$g0,$b0),array(0,0,0));
  55. for($i=0; $i<7; $i=$i+1)
  56. {
  57. $action=$this->getInt();
  58. $color = imagecolorallocate($image, $r,$g,$b);
  59. $r = ($r0 + $this->getInt()/25)%256;
  60. $g = ($g0 + $this->getInt()/25)%256;
  61. $b = ($b0 + $this->getInt()/25)%256;
  62. $r0=$r; $g0=$g; $b0=$b;
  63. $this->drawshape($image,$action,$color);
  64. }
  65. $color = imagecolorallocate($image,$this->getInt(),$this->getInt(),$this->getInt());
  66. $this->drawshape($image,$this->getInt(),$color);
  67. ob_start();
  68. imagepng($image);
  69. $imagedata = ob_get_contents();
  70. ob_end_clean();
  71. imagedestroy($image);
  72. return $imagedata;
  73. }
  74. // Generate a large random hexadecimal salt.
  75. private function randomSalt()
  76. {
  77. $randomSalt='';
  78. for($i=0;$i<6;$i++) { $randomSalt.=base_convert(mt_rand(),10,16); }
  79. return $randomSalt;
  80. }
  81. private function getInt() // Returns a single integer from the $VALUES array (0...255)
  82. {
  83. $v= $this->VALUES[$this->VALUES_INDEX];
  84. $this->VALUES_INDEX++;
  85. $this->VALUES_INDEX %= count($this->VALUES); // Warp around the array
  86. return $v;
  87. }
  88. private function getX() // Returns a single integer from the array (roughly mapped to image width)
  89. {
  90. return $this->width*$this->getInt()/256;
  91. }
  92. private function getY() // Returns a single integer from the array (roughly mapped to image height)
  93. {
  94. return $this->height*$this->getInt()/256;
  95. }
  96. # Gradient function taken from:
  97. # http://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html
  98. private function degrade($img,$direction,$color1,$color2)
  99. {
  100. if($direction=='h') { $size = imagesx($img); $sizeinv = imagesy($img); }
  101. else { $size = imagesy($img); $sizeinv = imagesx($img);}
  102. $diffs = array(
  103. (($color2[0]-$color1[0])/$size),
  104. (($color2[1]-$color1[1])/$size),
  105. (($color2[2]-$color1[2])/$size)
  106. );
  107. for($i=0;$i<$size;$i++)
  108. {
  109. $r = $color1[0]+($diffs[0]*$i);
  110. $g = $color1[1]+($diffs[1]*$i);
  111. $b = $color1[2]+($diffs[2]*$i);
  112. if($direction=='h') { imageline($img,$i,0,$i,$sizeinv,imagecolorallocate($img,$r,$g,$b)); }
  113. else { imageline($img,0,$i,$sizeinv,$i,imagecolorallocate($img,$r,$g,$b)); }
  114. }
  115. return $img;
  116. }
  117. private function drawshape($image,$action,$color)
  118. {
  119. switch($action%7)
  120. {
  121. case 0:
  122. ImageFilledRectangle ($image,$this->getX(),$this->getY(),$this->getX(),$this->getY(),$color);
  123. break;
  124. case 1:
  125. case 2:
  126. ImageFilledEllipse ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color);
  127. break;
  128. case 3:
  129. $points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(),$this->getX(), $this->getY());
  130. ImageFilledPolygon ($image, $points, 4, $color);
  131. break;
  132. case 4:
  133. case 5:
  134. case 6:
  135. $start=$this->getInt()*360/256; $end=$start+$this->getInt()*180/256;
  136. ImageFilledArc ($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(),$start,$end,$color,IMG_ARC_PIE);
  137. break;
  138. }
  139. }
  140. }
  141. ?>