Hate My Blog

Drawing the reddit alien with PHP and GD image library

Played hookie to my databases class and made this instead. Gotta love college.

reddit alien made with php

reddit alien made with php

If you make changes I would love to see the output.

//this code with generate a reddit alien in php code.
//its a big mess of numbers, deal with it.
//it generates a .png w/ transparency of the alien 200x180
//http://hatemyblog.wordpress.com/2009/09/10/drawing-the-reddit-alien-with-php-and-gd-image-library/


$im = @imagecreatetruecolor(200, 180);
imagesavealpha($im, true);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$red= imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);

//give it a defualt transparent bg.
$trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im, 0, 0, $trans_colour);

//feet
imagefilledarc  ($im, 100, 150, 50, 20, 180, 0, $black,IMG_ARC_PIE);
imagefilledarc  ($im, 100, 152, 44, 20, 180, 0, $white,IMG_ARC_PIE);
imagelinethick($im,75,151,124,151,$black,2);

//arms
imagefilledellipse($im, 100, 105, 75, 60, $black );
imagefilledellipse($im, 100, 105, 68, 55, $white );

//ears
imagefilledellipse($im, 135, 49, 18, 18, $black );
imagefilledellipse($im, 135, 49, 13, 13, $white );
imagefilledellipse($im, 65, 49, 18, 18, $black );
imagefilledellipse($im, 65, 49, 13, 13, $white );

//atenna
imagelinethick($im,95,40,105,20,$black,4);
imagelinethick($im,105,19,118,20,$black,4);
imagefilledellipse($im, 125, 22, 15, 15, $black );
imagefilledellipse($im, 125, 22, 8, 8, $white );

//body
imagefilledellipse($im, 100, 100, 60, 100, $black );
imagefilledellipse($im, 100, 100, 55, 95, $white );
//head
imagefilledellipse($im, 100, 60, 80, 50, $black );
imagefilledellipse($im, 100, 60, 75, 45, $white );
//eyes
imagefilledellipse($im, 85, 53, 10, 10, $red);
imagefilledellipse($im, 115, 53, 10, 10, $red);
//mouth
imagearc($im, 100, 60, 50, 30, 25, 155, $black );
imagearc($im, 100, 61, 50, 30, 25, 155, $black );
imagearc($im, 100, 62, 50, 30, 25, 155, $black );

//save it
imagepng($im,'reddit.png');
imagedestroy($im);
echo '<img style="background:blue;" src="reddit.png">';



//helper function i ripped from
//
function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
    /* this way it works well only for orthogonal lines
    imagesetthickness($image, $thick);
    return imageline($image, $x1, $y1, $x2, $y2, $color);
    */
    if ($thick == 1) {
        return imageline($image, $x1, $y1, $x2, $y2, $color);
    }
    $t = $thick / 2 - 0.5;
    if ($x1 == $x2 || $y1 == $y2) {
        return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
    }
    $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
    $a = $t / sqrt(1 + pow($k, 2));
    $points = array(
        round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
        round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
        round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
        round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
    );
    imagefilledpolygon($image, $points, 4, $color);
    return imagepolygon($image, $points, 4, $color);
}
About these ads

September 10, 2009 - Posted by | Code, PHP | , , , , , , ,

2 Comments »

  1. Could really, really use a call to imageantialias().

    Comment by Charles | September 10, 2009 | Reply

  2. He is too fat :D

    Comment by Mihael Konjevic | September 10, 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: