Draw a Line by Mouse Click on Map(Image)

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
Behseini
Forum Newbie
Posts: 3
Joined: Sun Oct 31, 2010 5:38 am

Draw a Line by Mouse Click on Map(Image)

Post by Behseini »

Hi Guys!
I just wonder if it is possible to draw a line by mouse click on an image and calculate the distance between two clicked points.I know how to calculate the distance but I like to know can PHP support a function like other programming languages to draw on image as well?
I know we can hard copy to draw line and shape in PHP by GD but how about mouse event handling?!
Thanks
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Draw a Line by Mouse Click on Map(Image)

Post by Jonah Bron »

Not in real time. If you want the user to be able to draw a line right there on the spot, you need Javascript and probably the <canvas> tag.

You might want to read this:
http://en.wikipedia.org/wiki/Server-side_scripting
And this:
http://en.wikipedia.org/wiki/Client-side_scripting

PHP is meant to be used as a server-side scripting language.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Draw a Line by Mouse Click on Map(Image)

Post by McInfo »

Here are the basics.

form.php

Code: Select all

<form method="post" action="">
    <input type="image" src="png.php" name="img" />
</form>

<pre><?php
print_r($_POST);
/*
Array
(
    [img_x] => 20
    [img_y] => 10
)
*/
?></pre>
png.php

Code: Select all

<?php
header('Content-Type: image/png');
$image = imagecreatetruecolor(300, 200);
$color = imagecolorallocate($image, 255, 255, 255);
imageline($image, 0, 0, 20, 10, $color);
imagepng($image);
imagedestroy($image);
Save these files to your server and run form.php in your browser. Notice how $_POST changes as you click different places on the image. Also notice that the image that appears in form.php is generated by another PHP script (png.php).

To allow the user to interact with the image, capture the user-submitted points in form.php and store them in $_SESSION or a database. In png.php, retrieve the points and use them to draw lines on the image.

If you want to modify an existing image, the image file needs to exist in a location that is accessible to the PHP server. Create an image resource from the file using the appropriate imagecreatefrom*() function.
Post Reply