Skip to content

Commit

Permalink
Modified. Code cleaned for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
cixtor committed Sep 17, 2014
1 parent ee708c2 commit 4a6249b
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,33 @@ function generate_grid(){
}

// Configurable variables
$config = (object)array();
$config->width = $config->height = 300;
$config = (object) array();
$config->width = 300;
$config->height = $config->width;
$config->square = $config->width / 5;

$canvas = imagecreatetruecolor($config->width, $config->height);

// Allocate colors
$white = imagecolorallocate($canvas, 255, 255, 255);
$black = imagecolorallocate($canvas, 51, 51, 51);
$color = imagecolorallocate($canvas, rand(0,255), rand(0,255), rand(0,255));
// Initiate image and allocate colors
$canvas = imagecreatetruecolor( $config->width, $config->height );
$white = imagecolorallocate( $canvas, 255, 255, 255 );
$black = imagecolorallocate( $canvas, 51, 51, 51 );
$color = imagecolorallocate( $canvas, rand(0,255), rand(0,255), rand(0,255) );

$grid = generate_grid();
foreach($grid as $y=>$row){
foreach($row as $x=>$cell){
$x1 = $x * $config->square;
foreach( $grid as $y0 => $row ){
foreach( $row as $x- => $cell ){
$x1 = $x0 * $config->square;
$x2 = $x1 + $config->square;
$y1 = $y * $config->square;
$y1 = $y0 * $config->square;
$y2 = $y1 + $config->square;
imagefilledrectangle($canvas, $x1, $y1, $x2, $y2, $cell?$color:$white);
$cell_color = $cell ? $color : $white;
imagefilledrectangle( $canvas, $x1, $y1, $x2, $y2, $cell_color );
}
}

// Output and free from memory
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : 'avatar.php';
if( preg_match('/\/avatar\.(php|png|jpg|jpeg|gif)/', $request_uri, $match) ){
switch($match[1]){
switch( $match[1] ){
case 'jpeg':
case 'jpg':
header('Content-Type: image/jpeg');
Expand Down

0 comments on commit 4a6249b

Please sign in to comment.