Source of /checkpoint/checkpoint.php


<?php
if ($_SERVER['SCRIPT_FILENAME'] == __FILE__)
{
    
$allowviewsource = 1;
    require_once(
"../common.php");
    if (!isset(
$_REQUEST['viewsource']))
        
error_page("This page cannot be viewed alone.");
}

$font = "./FreeSansBold.ttf"// The real Checkpoint thing isn't this font, but it's kinda similar, and this one is legal for me to use...
$ftops = array('linespacing' => 1.1);

function
CheckpointGo($message, $size, $angle)
{
    
$img = CreateCheckpointImageAntialias($message, $size, $angle);
    
OutputImage($img);
    
ImageDestroy($img);
}

function
GetParams()
{
    if (!empty(
$_REQUEST['message']))
    {
        
$message = $_REQUEST['message'];
        if (
get_magic_quotes_gpc())
            
$message = stripslashes($message);
    }
    else
        
$message = "15 CHECKPOINTS UNLOCKED\n\"Found Checkpoint Generator\"";

    if (!empty(
$_REQUEST['size']) && is_numeric($_REQUEST['size']))
        
$size = floatval($_REQUEST['size']);
    else
        
$size = 12;

    if (!empty(
$_REQUEST['angle']) && is_numeric($_REQUEST['angle']))
        
$angle = floatval($_REQUEST['angle']) / 180.0 * M_PI;
    else
        
$angle = FALSE;

    return array(
$message, $size, $angle);
}

function
GetParamsClean()
{
    if (!empty(
$_REQUEST['message']))
    {
        
$message = $_REQUEST['message'];
        if (
get_magic_quotes_gpc())
            
$message = stripslashes($message);
    }
    else
        
$message = "100 CHECKPOINTS LOST\n\"Didn't include a message\"";
    
$linecount = cleannl($message);
    if (
strlen($message) > 250 || $linecount 5)
        
$message = "75 CHECKPOINTS LOST\n\"Message too long\"";

    if (!empty(
$_REQUEST['size']))
        
$size = $_REQUEST['size'];
    else
        
$size = 12;
    if (!
is_numeric($size))
    {
        
$size = 12;
        
$message = "100 CHECKPOINTS LOST\n\"Size not numeric\"";
    }
    
$size = floatval($size);
    if (
$size 1)
    {
        
$size = 12;
        
$message = "50 CHECKPOINTS LOST\n\"Size too small\"";
    }
    if (
$size 20)
    {
        
$size = 12;
        
$message = "50 CHECKPOINTS LOST\n\"Size too large\"";
    }

    if (!empty(
$_REQUEST['angle']))
    {
        
$angle = $_REQUEST['angle'];
        if (!
is_numeric($angle))
        {
            
$size = 12;
            
$message = "100 CHECKPOINTS LOST\n\"Angle not numeric\"";
            
$angle = FALSE;
        }
        else
            
$angle = floatval($angle) / 180.0 * M_PI;
    }
    else
        
$angle = FALSE;

    return array(
$message, $size, $angle);
}

function
cleannl(&$message)
{
    
$str = "";
    
$linecount = 1;
    
$lastwasreturn = FALSE;
    for (
$i = 0$i strlen($message); $i++)
    {
        
$c = $message{$i};
        if (
$c == "\n" && $lastwasreturn)
            ;
        else if (
$c == "\r" || $c == "\n")
        {
            
$str .= "\n";
            
$linecount++;
        }
        else
            
$str .= $c;
        
$lastwasreturn = ($c == "\r");
    }
    
$message = $str;
    return
$linecount;
}

function
CreateCheckpointImage($message, $textsize, $angle)
{
    
$size = FindSize($message, $textsize);
    
$img = ImageCreateTrueColor($size['cx'], $size['cy']);
    
ClearImage($img);
    
DrawBackground($img, $size);
    
DrawLogo($img, $size, StarAngle($message, $angle));
    
DrawText($img, $message, $size);
    return
$img;
}

function
CreateCheckpointImageAntialias($message, $textsize, $angle)
{
    
// ImageAntiAlias doesn't work with alpha channels... do it by oversampling. Draw it hueg and scale it down.
    
$scale = 5.0;
    
$img = CreateCheckpointImage($message, $textsize * $scale, $angle);
    
$newimg = ImageCreateTrueColor(round(ImageSX($img)/$scale), round(ImageSY($img)/$scale));
    
ImageAlphaBlending($newimg, FALSE);
    
// The scaling isn't perfect, on the transparent edges, but it's OK-ish. Better than nothing.
    // see: http://bugs.php.net/bug.php?id=55034
    
ImageCopyResampled($newimg, $img, 0, 0, 0, 0, ImageSX($newimg), ImageSY($newimg), ImageSX($img), ImageSY($img));
    
ImageDestroy($img);
    return
$newimg;
}

function
ClearImage($img)
{
    
$col_transp = ImageColorAllocateAlpha($img, 0xFF, 0xFF, 0xFF, 0x7F);
    
ImageAlphaBlending($img, FALSE);
    
ImageFilledRectangle($img, 0, 0, ImageSX($img), ImageSY($img), $col_transp);
    
ImageAlphaBlending($img, TRUE);
}

function
FindSize($message, $textsize)
{
    global
$font, $ftops;
    
$bbox = ImageFTBBox($textsize, 0, $font, $message, $ftops);
    
$textwidth = $bbox[2] - $bbox[0];
    
$textheight = $bbox[1] - $bbox[7];
    
    
// Red area has a $textsize/2 border around text, then grey angled background area has a $textsize border around that
    // Also logo on the left is a circle, so is $imageheight in width too
    
$topborder = $textsize * 1.5;
    
$bottomborder = $textsize * 1.5;
    
$imageheight = $textheight + $topborder + $bottomborder;
    
$leftborder = $textsize * 0.5 + $imageheight;
    
$rightborder = $textsize * 0.5;
    
$imagewidth = $textwidth + $leftborder + $rightborder;
    
    
$textx = -$bbox[6] + $leftborder;
    
$texty = -$bbox[7] + $topborder;
    
    return array(
'cx' => $imagewidth, 'cy' => $imageheight, 'textcx' => $textwidth, 'textcy' => $textheight, 'x' => $textx, 'y' => $texty, 'size' => $textsize);
}

function
DrawBackground($img, $size)
{
    
$col_back = ImageColorAllocate($img, 0x7D, 0x08, 0x1B);
    
$col_behind = ImageColorAllocate($img, 0x3D, 0x3D, 0x3D);
    
ImageFilledPolygon($img, array($size['cy']*0.5, $size['size']*0.25, $size['cx']-$size['size']*1.5, $size['size']*1.5, $size['cx']-$size['size']*1.5, $size['cy']-$size['size']*0.25, $size['cy']*0.5, $size['cy']-$size['size']*1.5), 4, $col_behind);
    
ImageFilledRectangle($img, $size['cy']*0.5, $size['size'], $size['cx'], $size['cy'] - $size['size'], $col_back);
}

function
DrawLogo($img, $size, $angle)
{
    
$size = $size['cy']; // This is the only scale we care about here - just work with this.
    
$col_back  = ImageColorAllocate($img, 0xF4, 0xD6, 0x1D);
    
$col_star  = ImageColorAllocate($img, 0x6C, 0x6C, 0x6C);
    
$col_arrow = ImageColorAllocate($img, 0xAA, 0x1D, 0x38);
    
ImageFilledEllipse($img, $size/2.0, $size/2.0, $size, $size, $col_back);
    
$shape = StarShape($size, $angle);
    
ImageFilledPolygon($img, $shape, count($shape)/2, $col_star);
    
$shape = ArrowShape($size);
    
ImageFilledPolygon($img, $shape, count($shape)/2, $col_arrow);
}
$arrowshape = array(
    
0.4, 0.1,
    
0.4, 0.5,
    
0.3, 0.5,
    
0.5, 0.7,
    
0.7, 0.5,
    
0.6, 0.5,
    
0.6, 0.1,
);
function
ArrowShape($size)
{
    global
$arrowshape;
    
$scaled = array();
    foreach (
$arrowshape as $i)
        
$scaled[] = $i * $size;
    return
$scaled;
}
$starshape = array(
     
1.0,   1.0,
     
0.8,   0.2,
     
1.15,  0.0,
     
0.8,  -0.2,
     
1.0,  -1.0,
     
0.2,  -0.8,
     
0.0,  -1.15,
    -
0.2,  -0.8,
    -
1.0,  -1.0,
    -
0.8,  -0.2,
    -
1.15,  0.0,
    -
0.8,   0.2,
    -
1.0,   1.0,
    -
0.2,   0.8,
     
0.0,   1.15,
     
0.2,   0.8,
     
//1.0,   1.0,
);
$starcameray = 11.0;
$starcameraz = 3.0;
$starcameradist = sqrt($starcameray * $starcameray + $starcameraz * $starcameraz);
$starcameraangle = atan($starcameray / $starcameraz);
$starsize = 0.4 / M_SQRT2;
$starcentrex = 0.5;
$starcentrey = 0.7;
function
StarShape($size, $angle)
{
    global
$starshape, $starcameradist, $starcameraangle, $starsize, $starcentrex, $starcentrey;
    
$starpoints = array();
    
// Turn interleaved array of coordinates into an array of points
    
for ($i = 0$i count($starshape); $i += 2)
        
$starpoints[] = array('x' => $starshape[$i], 'y' => $starshape[$i + 1], 'z' => 0);
    
// Rotate star around vertical axis according to specified angle
    
foreach ($starpoints as $k=>$v)
        
$starpoints[$k] = array('x' => $v['x'] * cos($angle) - $v['y'] * sin($angle), 'y' => $v['y'] * cos($angle) + $v['x'] * sin($angle), 'z' => 0);
    
// Rotate and translate star according to camera position
    
foreach ($starpoints as $k=>$v)
        
$starpoints[$k] = array('x' => $v['x'], 'y' => $v['y'] * cos($starcameraangle), 'z' => $starcameradist - $v['y'] * sin($starcameraangle));
    
// Perform perspective calculation
    
foreach ($starpoints as $k=>$v)
        
$starpoints[$k] = array('x' => $v['x'] / $v['z'] * $starcameradist, 'y' => $v['y'] / $v['z'] * $starcameradist);
    
// Translate into screen position and re-interleave points
    
$newshape = array();
    foreach (
$starpoints as $v)
    {
        
$newshape[] = ($v['x'] * $starsize + $starcentrex) * $size;
        
$newshape[] = ($v['y'] * $starsize + $starcentrey) * $size;
    }
    return
$newshape;
}

function
StarAngle($message, $angle)
{
    if (
$angle !== FALSE)
        return
$angle;
    
// Return an angle between 0 and pi/2, based on a hash of the message
    
$hash = crc32($message);
    return
$hash / 2147483648.0 * M_PI_2;
}

function
DrawText($img, $message, $size)
{
    global
$font, $ftops;
    
$col_text = ImageColorAllocate($img, 0xFF, 0xFF, 0xFF);
    
ImageFTText($img, $size['size'], 0, $size['x'], $size['y'], $col_text, $font, $message, $ftops);
}

function
OutputImage($img)
{
    
ImageAlphaBlending($img, FALSE);
    
ImageSaveAlpha($img, TRUE);
    
header("Content-type: image/png");
    
ImagePNG($img);
}
?>

Valid HTML 4.01 StrictValid CSSLevel Triple-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0
Retrieved from https://​www.mrphlip.com/​checkpoint/​checkpoint.php?​viewsource=yep.
Copyright © 2006–13 Phillip Bradbury. Some rights reserved.
This page is best viewed in a web browser at a screen resolution. Attempts to do otherwise may fail miserably.
Contact me