smokinggun source code
back to Code | back to Home

Click two points to draw a line.

Info  |  Source
Info
A demonstration of using PHP to generate a graphic based upon a user's mouse clicks.

How it works: Notes
PHP Source
Below is the PHP source to generate the gif line. View the HTML source for the javascript code.
<?
# this code is in here since I got 17,000 hits yesterday from someone
# using this script
if (eregi("Simulator",$HTTP_REFERER))
    {
    
# please run this script locally, otherwise I will just take it offline
    
header("Location: http://i.cnn.net/cnn/images/main/cnn_logo_260.gif");
    exit;
    }
    
# draws a line based upon cordinates ($x1,$x2,$y1,$y2)
# syntax:
# image.php?$x1=&$x2=&$y1=&$y2=

$x1 $_REQUEST["x1"];
$x2 $_REQUEST["x2"];
$y1 $_REQUEST["y1"];
$y2 $_REQUEST["y2"];

$width abs($x1-$x2);
$height abs($y1-$y2);


$imgOut ImageCreate($width,$height);
$white ImageColorAllocate($imgOut255255255);
$black ImageColorAllocate($imgOut000);
$gray ImageColorAllocate($imgOut255255255);

# fill background color
imageFill($imgOut$width$height$white);

# get slope
if ($width != && $height != 0)
    
$m = ($x1-$x2)/($y1-$y2);
    
else 
$m 0;

# draw line based on slope
if ($m >= 0
    {
    
imageline($imgOut11$width+1$height+1$black);
    
#draw shadow
    #imageline($imgOut, 0, 0, $width, $height, $black);
    
}
else  
    {
    
imageline($imgOut$width+111,$height+1$black);
    
#draw shadow
    #imageline($imgOut, $width, 0, 0,$height, $black);
    
}

imagecolortransparent ($imgOut$white);

    
Header("Content-type: image/png");
    
ImagePNG($imgOut);
    
ImageDestroy($imgOut);
?>