Guest User

https://sourceforge.net/projects/identicons/

a guest
Mar 2nd, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.68 KB | None | 0 0
  1. <?php
  2.  
  3. /* generate sprite for corners and sides */
  4. function getsprite($shape,$R,$G,$B,$rotation) {
  5.     global $spriteZ;
  6.     $sprite=imagecreatetruecolor($spriteZ,$spriteZ);
  7.     imageantialias($sprite,TRUE);
  8.     $fg=imagecolorallocate($sprite,$R,$G,$B);
  9.     $bg=imagecolorallocate($sprite,255,255,255);
  10.     imagefilledrectangle($sprite,0,0,$spriteZ,$spriteZ,$bg);
  11.     switch($shape) {
  12.         case 0: // triangle
  13.             $shape=array(
  14.                 0.5,1,
  15.                 1,0,
  16.                 1,1
  17.             );
  18.             break;
  19.         case 1: // parallelogram
  20.             $shape=array(
  21.                 0.5,0,
  22.                 1,0,
  23.                 0.5,1,
  24.                 0,1
  25.             );
  26.             break;
  27.         case 2: // mouse ears
  28.             $shape=array(
  29.                 0.5,0,
  30.                 1,0,
  31.                 1,1,
  32.                 0.5,1,
  33.                 1,0.5
  34.             );
  35.             break;
  36.         case 3: // ribbon
  37.             $shape=array(
  38.                 0,0.5,
  39.                 0.5,0,
  40.                 1,0.5,
  41.                 0.5,1,
  42.                 0.5,0.5
  43.             );
  44.             break;
  45.         case 4: // sails
  46.             $shape=array(
  47.                 0,0.5,
  48.                 1,0,
  49.                 1,1,
  50.                 0,1,
  51.                 1,0.5
  52.             );
  53.             break;
  54.         case 5: // fins
  55.             $shape=array(
  56.                 1,0,
  57.                 1,1,
  58.                 0.5,1,
  59.                 1,0.5,
  60.                 0.5,0.5
  61.             );
  62.             break;
  63.         case 6: // beak
  64.             $shape=array(
  65.                 0,0,
  66.                 1,0,
  67.                 1,0.5,
  68.                 0,0,
  69.                 0.5,1,
  70.                 0,1
  71.             );
  72.             break;
  73.         case 7: // chevron
  74.             $shape=array(
  75.                 0,0,
  76.                 0.5,0,
  77.                 1,0.5,
  78.                 0.5,1,
  79.                 0,1,
  80.                 0.5,0.5
  81.             );
  82.             break;
  83.         case 8: // fish
  84.             $shape=array(
  85.                 0.5,0,
  86.                 0.5,0.5,
  87.                 1,0.5,
  88.                 1,1,
  89.                 0.5,1,
  90.                 0.5,0.5,
  91.                 0,0.5
  92.             );
  93.             break;
  94.         case 9: // kite
  95.             $shape=array(
  96.                 0,0,
  97.                 1,0,
  98.                 0.5,0.5,
  99.                 1,0.5,
  100.                 0.5,1,
  101.                 0.5,0.5,
  102.                 0,1
  103.             );
  104.             break;
  105.         case 10: // trough
  106.             $shape=array(
  107.                 0,0.5,
  108.                 0.5,1,
  109.                 1,0.5,
  110.                 0.5,0,
  111.                 1,0,
  112.                 1,1,
  113.                 0,1
  114.             );
  115.             break;
  116.         case 11: // rays
  117.             $shape=array(
  118.                 0.5,0,
  119.                 1,0,
  120.                 1,1,
  121.                 0.5,1,
  122.                 1,0.75,
  123.                 0.5,0.5,
  124.                 1,0.25
  125.             );
  126.             break;
  127.         case 12: // double rhombus
  128.             $shape=array(
  129.                 0,0.5,
  130.                 0.5,0,
  131.                 0.5,0.5,
  132.                 1,0,
  133.                 1,0.5,
  134.                 0.5,1,
  135.                 0.5,0.5,
  136.                 0,1
  137.             );
  138.             break;
  139.         case 13: // crown
  140.             $shape=array(
  141.                 0,0,
  142.                 1,0,
  143.                 1,1,
  144.                 0,1,
  145.                 1,0.5,
  146.                 0.5,0.25,
  147.                 0.5,0.75,
  148.                 0,0.5,
  149.                 0.5,0.25
  150.             );
  151.             break;
  152.         case 14: // radioactive
  153.             $shape=array(
  154.                 0,0.5,
  155.                 0.5,0.5,
  156.                 0.5,0,
  157.                 1,0,
  158.                 0.5,0.5,
  159.                 1,0.5,
  160.                 0.5,1,
  161.                 0.5,0.5,
  162.                 0,1
  163.             );
  164.             break;
  165.         default: // tiles
  166.             $shape=array(
  167.                 0,0,
  168.                 1,0,
  169.                 0.5,0.5,
  170.                 0.5,0,
  171.                 0,0.5,
  172.                 1,0.5,
  173.                 0.5,1,
  174.                 0.5,0.5,
  175.                 0,1
  176.             );
  177.             break;
  178.     }
  179.     /* apply ratios */
  180.     for ($i=0;$i<count($shape);$i++)
  181.         $shape[$i]=$shape[$i]*$spriteZ;
  182.     imagefilledpolygon($sprite,$shape,count($shape)/2,$fg);
  183.     /* rotate the sprite */
  184.     for ($i=0;$i<$rotation;$i++)
  185.         $sprite=imagerotate($sprite,90,$bg);
  186.     return $sprite;
  187. }
  188.  
  189. /* generate sprite for center block */
  190. function getcenter($shape,$fR,$fG,$fB,$bR,$bG,$bB,$usebg) {
  191.     global $spriteZ;
  192.     $sprite=imagecreatetruecolor($spriteZ,$spriteZ);
  193.     imageantialias($sprite,TRUE);
  194.     $fg=imagecolorallocate($sprite,$fR,$fG,$fB);
  195.     /* make sure there's enough contrast before we use background color of side sprite */
  196.     if ($usebg>0 && (abs($fR-$bR)>127 || abs($fG-$bG)>127 || abs($fB-$bB)>127))
  197.         $bg=imagecolorallocate($sprite,$bR,$bG,$bB);
  198.     else
  199.         $bg=imagecolorallocate($sprite,255,255,255);
  200.     imagefilledrectangle($sprite,0,0,$spriteZ,$spriteZ,$bg);
  201.     switch($shape) {
  202.         case 0: // empty
  203.             $shape=array();
  204.             break;
  205.         case 1: // fill
  206.             $shape=array(
  207.                 0,0,
  208.                 1,0,
  209.                 1,1,
  210.                 0,1
  211.             );
  212.             break;
  213.         case 2: // diamond
  214.             $shape=array(
  215.                 0.5,0,
  216.                 1,0.5,
  217.                 0.5,1,
  218.                 0,0.5
  219.             );
  220.             break;
  221.         case 3: // reverse diamond
  222.             $shape=array(
  223.                 0,0,
  224.                 1,0,
  225.                 1,1,
  226.                 0,1,
  227.                 0,0.5,
  228.                 0.5,1,
  229.                 1,0.5,
  230.                 0.5,0,
  231.                 0,0.5
  232.             );
  233.             break;
  234.         case 4: // cross
  235.             $shape=array(
  236.                 0.25,0,
  237.                 0.75,0,
  238.                 0.5,0.5,
  239.                 1,0.25,
  240.                 1,0.75,
  241.                 0.5,0.5,
  242.                 0.75,1,
  243.                 0.25,1,
  244.                 0.5,0.5,
  245.                 0,0.75,
  246.                 0,0.25,
  247.                 0.5,0.5
  248.             );
  249.             break;
  250.         case 5: // morning star
  251.             $shape=array(
  252.                 0,0,
  253.                 0.5,0.25,
  254.                 1,0,
  255.                 0.75,0.5,
  256.                 1,1,
  257.                 0.5,0.75,
  258.                 0,1,
  259.                 0.25,0.5
  260.             );
  261.             break;
  262.         case 6: // small square
  263.             $shape=array(
  264.                 0.33,0.33,
  265.                 0.67,0.33,
  266.                 0.67,0.67,
  267.                 0.33,0.67
  268.             );
  269.             break;
  270.         case 7: // checkerboard
  271.             $shape=array(
  272.                 0,0,
  273.                 0.33,0,
  274.                 0.33,0.33,
  275.                 0.66,0.33,
  276.                 0.67,0,
  277.                 1,0,
  278.                 1,0.33,
  279.                 0.67,0.33,
  280.                 0.67,0.67,
  281.                 1,0.67,
  282.                 1,1,
  283.                 0.67,1,
  284.                 0.67,0.67,
  285.                 0.33,0.67,
  286.                 0.33,1,
  287.                 0,1,
  288.                 0,0.67,
  289.                 0.33,0.67,
  290.                 0.33,0.33,
  291.                 0,0.33
  292.             );
  293.             break;
  294.     }
  295.     /* apply ratios */
  296.     for ($i=0;$i<count($shape);$i++)
  297.         $shape[$i]=$shape[$i]*$spriteZ;
  298.     if (count($shape)>0)
  299.         imagefilledpolygon($sprite,$shape,count($shape)/2,$fg);
  300.     return $sprite;
  301. }
  302.  
  303. /* parse hash string */
  304.  
  305. $csh=hexdec(substr($_GET["hash"],0,1)); // corner sprite shape
  306. $ssh=hexdec(substr($_GET["hash"],1,1)); // side sprite shape
  307. $xsh=hexdec(substr($_GET["hash"],2,1))&7; // center sprite shape
  308.  
  309. $cro=hexdec(substr($_GET["hash"],3,1))&3; // corner sprite rotation
  310. $sro=hexdec(substr($_GET["hash"],4,1))&3; // side sprite rotation
  311. $xbg=hexdec(substr($_GET["hash"],5,1))%2; // center sprite background
  312.  
  313. /* corner sprite foreground color */
  314. $cfr=hexdec(substr($_GET["hash"],6,2));
  315. $cfg=hexdec(substr($_GET["hash"],8,2));
  316. $cfb=hexdec(substr($_GET["hash"],10,2));
  317.  
  318. /* side sprite foreground color */
  319. $sfr=hexdec(substr($_GET["hash"],12,2));
  320. $sfg=hexdec(substr($_GET["hash"],14,2));
  321. $sfb=hexdec(substr($_GET["hash"],16,2));
  322.  
  323. /* final angle of rotation */
  324. $angle=hexdec(substr($_GET["hash"],18,2));
  325.  
  326. /* size of each sprite */
  327. $spriteZ=128;
  328.  
  329. /* start with blank 3x3 identicon */
  330. $identicon=imagecreatetruecolor($spriteZ*3,$spriteZ*3);
  331. imageantialias($identicon,TRUE);
  332.  
  333. /* assign white as background */
  334. $bg=imagecolorallocate($identicon,255,255,255);
  335. imagefilledrectangle($identicon,0,0,$spriteZ,$spriteZ,$bg);
  336.  
  337. /* generate corner sprites */
  338. $corner=getsprite($csh,$cfr,$cfg,$cfb,$cro);
  339. imagecopy($identicon,$corner,0,0,0,0,$spriteZ,$spriteZ);
  340. $corner=imagerotate($corner,90,$bg);
  341. imagecopy($identicon,$corner,0,$spriteZ*2,0,0,$spriteZ,$spriteZ);
  342. $corner=imagerotate($corner,90,$bg);
  343. imagecopy($identicon,$corner,$spriteZ*2,$spriteZ*2,0,0,$spriteZ,$spriteZ);
  344. $corner=imagerotate($corner,90,$bg);
  345. imagecopy($identicon,$corner,$spriteZ*2,0,0,0,$spriteZ,$spriteZ);
  346.  
  347. /* generate side sprites */
  348. $side=getsprite($ssh,$sfr,$sfg,$sfb,$sro);
  349. imagecopy($identicon,$side,$spriteZ,0,0,0,$spriteZ,$spriteZ);
  350. $side=imagerotate($side,90,$bg);
  351. imagecopy($identicon,$side,0,$spriteZ,0,0,$spriteZ,$spriteZ);
  352. $side=imagerotate($side,90,$bg);
  353. imagecopy($identicon,$side,$spriteZ,$spriteZ*2,0,0,$spriteZ,$spriteZ);
  354. $side=imagerotate($side,90,$bg);
  355. imagecopy($identicon,$side,$spriteZ*2,$spriteZ,0,0,$spriteZ,$spriteZ);
  356.  
  357. /* generate center sprite */
  358. $center=getcenter($xsh,$cfr,$cfg,$cfb,$sfr,$sfg,$sfb,$xbg);
  359. imagecopy($identicon,$center,$spriteZ,$spriteZ,0,0,$spriteZ,$spriteZ);
  360.  
  361. // $identicon=imagerotate($identicon,$angle,$bg);
  362.  
  363. /* make white transparent */
  364. imagecolortransparent($identicon,$bg);
  365.  
  366. /* create blank image according to specified dimensions */
  367. $resized=imagecreatetruecolor($_GET["size"],$_GET["size"]);
  368. imageantialias($resized,TRUE);
  369.  
  370. /* assign white as background */
  371. $bg=imagecolorallocate($resized,255,255,255);
  372. imagefilledrectangle($resized,0,0,$_GET["size"],$_GET["size"],$bg);
  373.  
  374. /* resize identicon according to specification */
  375. imagecopyresampled($resized,$identicon,0,0,(imagesx($identicon)-$spriteZ*3)/2,(imagesx($identicon)-$spriteZ*3)/2,$_GET["size"],$_GET["size"],$spriteZ*3,$spriteZ*3);
  376.  
  377. /* make white transparent */
  378. imagecolortransparent($resized,$bg);
  379.  
  380. /* and finally, send to standard output */
  381. header("Content-Type: image/png");
  382. imagepng($resized);
  383.  
  384. ?>
Add Comment
Please, Sign In to add comment