Last active
August 29, 2015 13:57
-
-
Save trepmal/9377156 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$email = isset( $_GET['email'] ) ? $_GET['email'] : 'null'; | |
$salt = isset( $_GET['salt'] ) ? $_GET['salt'] : ''; | |
// hash email, split it up | |
$hash = str_split( sha1( $email . $salt ), 2 ); | |
// use first 3 values for foreground | |
$foreground = "{$hash[0]}{$hash[1]}{$hash[2]}"; | |
// and next 3 values for background | |
$background = "{$hash[3]}{$hash[4]}{$hash[5]}"; | |
/** | |
* Figure out the colors for each square | |
*/ | |
/* | |
This is how we'll mirror. So generate up to half of | |
the squares, but round up in case of odd-sized grid | |
1 5 5 1 1 4 1 | |
2 6 6 2 2 5 2 | |
3 7 7 3 3 6 3 | |
4 8 8 4 | |
*/ | |
$grid_size = 6; | |
$middle = ceil( $grid_size/2 ); // round up to capture middle col for odd grids | |
$gen_up_to = $grid_size*$middle; // calc number of actual squares | |
$colors = array(); | |
for( $i = 0; $i < $gen_up_to; $i++ ) { | |
if ( hexdec( $hash[$i] )%10 < 5 ) { | |
$colors[] = $foreground; | |
} else { | |
$colors[] = $background; | |
} | |
} | |
/** | |
* mirror the design. if odd grid size, exclude the middle column | |
*/ | |
if ( $grid_size%2 == 0 ) { | |
// chunk to maintain symmetry | |
/* otherwise: | |
1 5 8 4 1 4 3 | |
2 6 7 3 2 5 2 | |
3 7 6 2 3 6 1 | |
4 8 5 1 | |
*/ | |
$mirror = array_reverse( array_chunk( $colors, $grid_size ) ); | |
} else { | |
$mirror = array_reverse( array_chunk( array_slice( $colors, 0, $grid_size * ( $middle - 1 ) ), $grid_size ) ); | |
} | |
foreach( $mirror as $m ) { | |
$colors = array_merge( $colors, $m ); | |
} | |
/** | |
* Now actually put the image together | |
*/ | |
$unit = 100; | |
$im = imagecreatetruecolor($grid_size*$unit, $grid_size*$unit); | |
$wpos = 0; | |
$hpos = 0; | |
foreach ( $colors as $color ) { | |
$color = '0x00'.$color; | |
imagefilledrectangle( $im, $wpos, $hpos, $wpos+$unit, $hpos+$unit, $color ); | |
$hpos += $unit; | |
// new col | |
if ( $hpos == $grid_size*$unit ) { | |
$wpos += $unit; | |
$hpos = 0; | |
} | |
} | |
header( 'Content-Type: image/png' ); | |
header( 'Content-Disposition: filename="avatar.png"' ); | |
imagepng( $im ); | |
imagedestroy( $im ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment