Guest User

Untitled

a guest
Feb 13th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. <?php
  2. /**
  3. * 随机头像生成
  4. * 生成算法来自Github,具体出处找不到了,做了少量修改
  5. * @author loveyu
  6. */
  7.  
  8. /**
  9. * 头像HASH值
  10. */
  11. define("AVATAR_HASH", (isset($_GET['hash']) && !empty($_GET['hash'])) ? md5($_GET['hash']) : md5(rand(0, 1000000)));
  12.  
  13. /**
  14. * 头像大小
  15. */
  16. define("AVATAR_SIZE", (isset($_GET['size']) && $_GET['size'] > 10) ? $_GET['size'] : 100);
  17.  
  18. /**
  19. * generate sprite for corners and sides
  20. * @param $shape
  21. * @param $R
  22. * @param $G
  23. * @param $B
  24. * @param $rotation
  25. * @return resource
  26. */
  27. function getsprite($shape, $R, $G, $B, $rotation){
  28. global $spriteZ;
  29. $sprite = imagecreatetruecolor($spriteZ, $spriteZ);
  30. imageantialias($sprite, true);
  31. $fg = imagecolorallocate($sprite, $R, $G, $B);
  32. $bg = imagecolorallocate($sprite, 255, 255, 255);
  33. imagefilledrectangle($sprite, 0, 0, $spriteZ, $spriteZ, $bg);
  34. switch($shape){
  35. case 0: // triangle
  36. $shape = array(0.5, 1, 1, 0, 1, 1);
  37. break;
  38. case 1: // parallelogram
  39. $shape = array(0.5, 0, 1, 0, 0.5, 1, 0, 1);
  40. break;
  41. case 2: // mouse ears
  42. $shape = array(0.5, 0, 1, 0, 1, 1, 0.5, 1, 1, 0.5);
  43. break;
  44. case 3: // ribbon
  45. $shape = array(0, 0.5, 0.5, 0, 1, 0.5, 0.5, 1, 0.5, 0.5);
  46. break;
  47. case 4: // sails
  48. $shape = array(0, 0.5, 1, 0, 1, 1, 0, 1, 1, 0.5);
  49. break;
  50. case 5: // fins
  51. $shape = array(1, 0, 1, 1, 0.5, 1, 1, 0.5, 0.5, 0.5);
  52. break;
  53. case 6: // beak
  54. $shape = array(0, 0, 1, 0, 1, 0.5, 0, 0, 0.5, 1, 0, 1);
  55. break;
  56. case 7: // chevron
  57. $shape = array(0, 0, 0.5, 0, 1, 0.5, 0.5, 1, 0, 1, 0.5, 0.5);
  58. break;
  59. case 8: // fish
  60. $shape = array(0.5, 0, 0.5, 0.5, 1, 0.5, 1, 1, 0.5, 1, 0.5, 0.5, 0, 0.5);
  61. break;
  62. case 9: // kite
  63. $shape = array(0, 0, 1, 0, 0.5, 0.5, 1, 0.5, 0.5, 1, 0.5, 0.5, 0, 1);
  64. break;
  65. case 10: // trough
  66. $shape = array(0, 0.5, 0.5, 1, 1, 0.5, 0.5, 0, 1, 0, 1, 1, 0, 1);
  67. break;
  68. case 11: // rays
  69. $shape = array(0.5, 0, 1, 0, 1, 1, 0.5, 1, 1, 0.75, 0.5, 0.5, 1, 0.25);
  70. break;
  71. case 12: // double rhombus
  72. $shape = array(0, 0.5, 0.5, 0, 0.5, 0.5, 1, 0, 1, 0.5, 0.5, 1, 0.5, 0.5, 0, 1);
  73. break;
  74. case 13: // crown
  75. $shape = array(0, 0, 1, 0, 1, 1, 0, 1, 1, 0.5, 0.5, 0.25, 0.5, 0.75, 0, 0.5, 0.5, 0.25);
  76. break;
  77. case 14: // radioactive
  78. $shape = array(0, 0.5, 0.5, 0.5, 0.5, 0, 1, 0, 0.5, 0.5, 1, 0.5, 0.5, 1, 0.5, 0.5, 0, 1);
  79. break;
  80. default: // tiles
  81. $shape = array(0, 0, 1, 0, 0.5, 0.5, 0.5, 0, 0, 0.5, 1, 0.5, 0.5, 1, 0.5, 0.5, 0, 1);
  82. break;
  83. }
  84. /* apply ratios */
  85. for($i = 0; $i < count($shape); $i++){
  86. $shape[$i] = $shape[$i] * $spriteZ;
  87. }
  88. imagefilledpolygon($sprite, $shape, count($shape) / 2, $fg);
  89. /* rotate the sprite */
  90. for($i = 0; $i < $rotation; $i++){
  91. $sprite = imagerotate($sprite, 90, $bg);
  92. }
  93. return $sprite;
  94. }
  95.  
  96. /**
  97. * generate sprite for center block
  98. * @param $shape
  99. * @param $fR
  100. * @param $fG
  101. * @param $fB
  102. * @param $bR
  103. * @param $bG
  104. * @param $bB
  105. * @param $usebg
  106. * @return resource
  107. */
  108. function getcenter($shape, $fR, $fG, $fB, $bR, $bG, $bB, $usebg){
  109. global $spriteZ;
  110. $sprite = imagecreatetruecolor($spriteZ, $spriteZ);
  111. imageantialias($sprite, true);
  112. $fg = imagecolorallocate($sprite, $fR, $fG, $fB);
  113. /* make sure there's enough contrast before we use background color of side sprite */
  114. if($usebg > 0 && (abs($fR - $bR) > 127 || abs($fG - $bG) > 127 || abs($fB - $bB) > 127)){
  115. $bg = imagecolorallocate($sprite, $bR, $bG, $bB);
  116. } else{
  117. $bg = imagecolorallocate($sprite, 255, 255, 255);
  118. }
  119. imagefilledrectangle($sprite, 0, 0, $spriteZ, $spriteZ, $bg);
  120. switch($shape){
  121. case 0: // empty
  122. $shape = array();
  123. break;
  124. case 1: // fill
  125. $shape = array(0, 0, 1, 0, 1, 1, 0, 1);
  126. break;
  127. case 2: // diamond
  128. $shape = array(0.5, 0, 1, 0.5, 0.5, 1, 0, 0.5);
  129. break;
  130. case 3: // reverse diamond
  131. $shape = array(0, 0, 1, 0, 1, 1, 0, 1, 0, 0.5, 0.5, 1, 1, 0.5, 0.5, 0, 0, 0.5);
  132. break;
  133. case 4: // cross
  134. $shape = array(0.25, 0, 0.75, 0, 0.5, 0.5, 1, 0.25, 1, 0.75, 0.5, 0.5, 0.75, 1, 0.25, 1, 0.5, 0.5, 0, 0.75, 0, 0.25, 0.5, 0.5);
  135. break;
  136. case 5: // morning star
  137. $shape = array(0, 0, 0.5, 0.25, 1, 0, 0.75, 0.5, 1, 1, 0.5, 0.75, 0, 1, 0.25, 0.5);
  138. break;
  139. case 6: // small square
  140. $shape = array(0.33, 0.33, 0.67, 0.33, 0.67, 0.67, 0.33, 0.67);
  141. break;
  142. case 7: // checkerboard
  143. $shape = array(0, 0, 0.33, 0, 0.33, 0.33, 0.66, 0.33, 0.67, 0, 1, 0, 1, 0.33, 0.67, 0.33, 0.67, 0.67, 1, 0.67, 1, 1, 0.67, 1, 0.67, 0.67,
  144. 0.33, 0.67, 0.33, 1, 0, 1, 0, 0.67, 0.33, 0.67, 0.33, 0.33, 0, 0.33);
  145. break;
  146. }
  147. /* apply ratios */
  148. for($i = 0; $i < count($shape); $i++){
  149. $shape[$i] = $shape[$i] * $spriteZ;
  150. }
  151. if(count($shape) > 0){
  152. imagefilledpolygon($sprite, $shape, count($shape) / 2, $fg);
  153. }
  154. return $sprite;
  155. }
  156.  
  157. /**
  158. * 调整颜色
  159. * @param $r
  160. * @param $g
  161. * @param $b
  162. */
  163. function fix_rgb(&$r, &$g, &$b){
  164. if($r==0){
  165. $r = 10;
  166. }
  167. if($g==0){
  168. $g = 10;
  169. }
  170. if($b ==0){
  171. $b = 10;
  172. }
  173. while($r < 210){
  174. $r *= 1.3;
  175. }
  176. while($g < 100){
  177. $g *= 1.3;
  178. }
  179. while($b < 180){
  180. $b *= 1.3;
  181. }
  182. if($r > 255){
  183. $r = 511 - $r;
  184. }
  185. if($g > 255){
  186. $g = 511 - $g;
  187. }
  188. if($b > 255){
  189. $b = 511 - $b;
  190. }
  191. }
  192.  
  193.  
  194. $csh = hexdec(substr(AVATAR_HASH, 0, 1)); // corner sprite shape
  195. $ssh = hexdec(substr(AVATAR_HASH, 1, 1)); // side sprite shape
  196. $xsh = hexdec(substr(AVATAR_HASH, 2, 1)) & 7; // center sprite shape
  197.  
  198. $cro = hexdec(substr(AVATAR_HASH, 3, 1)) & 3; // corner sprite rotation
  199. $sro = hexdec(substr(AVATAR_HASH, 4, 1)) & 3; // side sprite rotation
  200. $xbg = hexdec(substr(AVATAR_HASH, 5, 1)) % 2; // center sprite background
  201.  
  202. /* corner sprite foreground color */
  203. $cfr = hexdec(substr(AVATAR_HASH, 6, 2));
  204. $cfg = hexdec(substr(AVATAR_HASH, 8, 2));
  205. $cfb = hexdec(substr(AVATAR_HASH, 10, 2));
  206. fix_rgb($cfr, $cfg, $cfb);
  207.  
  208. /* side sprite foreground color */
  209. $sfr = hexdec(substr(AVATAR_HASH, 12, 2));
  210. $sfg = hexdec(substr(AVATAR_HASH, 14, 2));
  211. $sfb = hexdec(substr(AVATAR_HASH, 16, 2));
  212. fix_rgb($sfr, $sfg, $sfb);
  213.  
  214. /* final angle of rotation */
  215. $angle = hexdec(substr(AVATAR_HASH, 18, 2));
  216.  
  217.  
  218. /* size of each sprite */
  219. $spriteZ = 128;
  220.  
  221. /* start with blank 3x3 identicon */
  222. $identicon = imagecreatetruecolor($spriteZ * 3, $spriteZ * 3);
  223. imageantialias($identicon, true);
  224.  
  225. /* assign white as background */
  226. $bg = imagecolorallocate($identicon, 255, 255, 255);
  227. imagefilledrectangle($identicon, 0, 0, $spriteZ, $spriteZ, $bg);
  228.  
  229. /* generate corner sprites */
  230. $corner = getsprite($csh, $cfr, $cfg, $cfb, $cro);
  231. imagecopy($identicon, $corner, 0, 0, 0, 0, $spriteZ, $spriteZ);
  232. $corner = imagerotate($corner, 90, $bg);
  233. imagecopy($identicon, $corner, 0, $spriteZ * 2, 0, 0, $spriteZ, $spriteZ);
  234. $corner = imagerotate($corner, 90, $bg);
  235. imagecopy($identicon, $corner, $spriteZ * 2, $spriteZ * 2, 0, 0, $spriteZ, $spriteZ);
  236. $corner = imagerotate($corner, 90, $bg);
  237. imagecopy($identicon, $corner, $spriteZ * 2, 0, 0, 0, $spriteZ, $spriteZ);
  238.  
  239. /* generate side sprites */
  240. $side = getsprite($ssh, $sfr, $sfg, $sfb, $sro);
  241. imagecopy($identicon, $side, $spriteZ, 0, 0, 0, $spriteZ, $spriteZ);
  242. $side = imagerotate($side, 90, $bg);
  243. imagecopy($identicon, $side, 0, $spriteZ, 0, 0, $spriteZ, $spriteZ);
  244. $side = imagerotate($side, 90, $bg);
  245. imagecopy($identicon, $side, $spriteZ, $spriteZ * 2, 0, 0, $spriteZ, $spriteZ);
  246. $side = imagerotate($side, 90, $bg);
  247. imagecopy($identicon, $side, $spriteZ * 2, $spriteZ, 0, 0, $spriteZ, $spriteZ);
  248.  
  249. /* generate center sprite */
  250. $center = getcenter($xsh, $cfr, $cfg, $cfb, $sfr, $sfg, $sfb, $xbg);
  251. imagecopy($identicon, $center, $spriteZ, $spriteZ, 0, 0, $spriteZ, $spriteZ);
  252.  
  253. // $identicon=imagerotate($identicon,$angle,$bg);
  254.  
  255. /* make white transparent */
  256. imagecolortransparent($identicon, $bg);
  257.  
  258. /* create blank image according to specified dimensions */
  259. $resized = imagecreatetruecolor(AVATAR_SIZE, AVATAR_SIZE);
  260. imageantialias($resized, true);
  261.  
  262. /* assign white as background */
  263. $bg = imagecolorallocate($resized, 255, 255, 255);
  264. imagefilledrectangle($resized, 0, 0, AVATAR_SIZE, AVATAR_SIZE, $bg);
  265.  
  266. /* resize identicon according to specification */
  267. imagecopyresampled($resized, $identicon, 0, 0, (imagesx($identicon) - $spriteZ * 3) / 2, (imagesx($identicon) - $spriteZ * 3) / 2, AVATAR_SIZE, AVATAR_SIZE, $spriteZ * 3, $spriteZ * 3);
  268.  
  269. /* make white transparent */
  270. imagecolortransparent($resized, $bg);
  271.  
  272. /* and finally, send to standard output */
  273. header("Content-Type: image/png");
  274. imagepng($resized);
Add Comment
Please, Sign In to add comment