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
//https://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);
}

September 10, 2009 Posted by | Code, PHP | , , , , , , , | 2 Comments

Fuzzy status of who’s online at reddit

This is some quick and dirty little code to test without much precision if any particular user is online over at Reddit.

$url=’http://www.reddit.com/user/HattoriHanzo.json&#8217;;

//get the url | json decode it to an array
$j=json_decode(file_get_contents($url),1);
//this just grabs the first element and then its timestamp
$t=$j[data][children][0][data][created_utc];

//this is a delay of 15 min. use std. timestamp math
if((time()-$t) < (60 * 15)){ //and they are online ;) } //or offline ;( [/sourcecode]

September 10, 2009 Posted by | Code, PHP | , , , , | 1 Comment