Warning : mysql_connect() [function.mysql-connect ]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111) in /var/www/smokinggun/live/lib/sg_main.php on line 260
Warning : mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /var/www/smokinggun/live/lib/sg_main.php on line 262
durring an attempt to connect
smokinggun | SOURCE CODE
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:
Capture mouse position for first two clicks.
Add position data to a URL which points to a PHP script.
PHP Script generates a GIF based upon the position data. This is returned to the browser.
Javascript is used to draw the GIF as a new image in the document body.
Notes
Scrolling offsets are not taken into account.
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 ( $imgOut , 255 , 255 , 255 ); $black = ImageColorAllocate ( $imgOut , 0 , 0 , 0 ); $gray = ImageColorAllocate ( $imgOut , 255 , 255 , 255 ); # fill background color imageFill ( $imgOut , $width , $height , $white ); # get slope if ( $width != 0 && $height != 0 ) $m = ( $x1 - $x2 )/( $y1 - $y2 ); else $m = 0 ; # draw line based on slope if ( $m >= 0 ) { imageline ( $imgOut , 1 , 1 , $width + 1 , $height + 1 , $black ); #draw shadow #imageline($imgOut, 0, 0, $width, $height, $black); } else { imageline ( $imgOut , $width + 1 , 1 , 1 , $height + 1 , $black ); #draw shadow #imageline($imgOut, $width, 0, 0,$height, $black); } imagecolortransparent ( $imgOut , $white ); Header ( "Content-type: image/png" ); ImagePNG ( $imgOut ); ImageDestroy ( $imgOut ); ?>