[Solved]-Image Mapping
Posted: Tue Aug 02, 2005 9:52 am
Before i get screamed at, i already did a lot of searching and i have yet to find a solution. I am starting to think that imagemapping isn't possible in php. I know it must be so here it goes.
I have wrote this php script that takes information from a database and plots it on the screen. So, enter a city and it grabs all the zip codes, finds the long and lat of the point (information from the database) for a particular zip code and then plots that information on the png image. Now all i want to do is make the area where the zip code text to be a "Hot Spot" or i guess the right term would be an imagemap. Here is the code that works fine minus the image mapping part.
So that works fine for plotting out the information on the png image. But what i really want, is to be able to make the text areas a hot spot. The annoying part is, the information is already in the php code, i just can't get it to imagemap a certain region. Any help would be great.
Thanks.
I have wrote this php script that takes information from a database and plots it on the screen. So, enter a city and it grabs all the zip codes, finds the long and lat of the point (information from the database) for a particular zip code and then plots that information on the png image. Now all i want to do is make the area where the zip code text to be a "Hot Spot" or i guess the right term would be an imagemap. Here is the code that works fine minus the image mapping part.
Code: Select all
<?php
$city = "somewhere";
// connection information
$hostName = "#######";
$userName = "#####";
$password = "#######";
$dbName = "#######";
// make connection to database
mysql_connect($hostName, $userName, $password) or die("Unable to connect to host $hostName");
mysql_select_db($dbName) or die("Unable to select database $dbName");
// Select all the fields in all the records from ZCTA
$query = "SELECT * FROM zcta WHERE city LIKE '%$city%'";
$result = mysql_query($query);
$number = mysql_num_rows($result);
//----Do the old classic IF statement
if ($number == 0) {
print " - Sorry, there were no records matching that criteria. Please click the back button if you wish to continue.";
} else {
//----------------------------It's mapping time------------------------------\\
//Prepare some of the necessary values
$im = imagecreatefrompng("seattle2.png");
$white = imagecolorallocate ($im, 255,255,255);
$aldy = imagecolorallocate ($im, 34,73,128);
$black = imagecolorallocate ($im, 0,0,0);
$scale_x = imagesx($im);
$scale_y = imagesy($im);
$textcolor = imagecolorallocate($im, 234, 13, 13);
//----This is the most important part for getting the php script to "loop" the "query-->then grab-->then display" routine.
for($i=0; $i<$number; $i++){
$zipcode = mysql_result($result,$i, "zip");
$lat = mysql_result($result,$i, "lat");
$lon = mysql_result($result,$i, "lon");
$pt = getlocationcoords($lat, $lon, $scale_x, $scale_y);
imagefilledrectangle($im,$pt["x"]-17,$pt["y"]-5,$pt["x"]+25,$pt["y"]+7,$aldy);
imagestring($im,4,$pt["x"]-15,$pt["y"]-6,"$zipcode",$white);
}
// **------------------------------------------------------------------------------------------------------**
//-----Copyright Label-----// off by Default
//imagestring($im,50,1,$scale_y-20,"###########",$black);
} //------Close up our IF statement
// Show the png image -_*-*_- Jpeg could be used as well
header("Content-Type: image/png");
imagepng($im);
imagedestroy($im);
// Convert the lon/lat values into screen coordinates by taking all of that
// info above stored in cache and use to plot values and Labels in the right spot
function getlocationcoords($lat, $lon) {
$x_min = 0; $x_max = 1444;
$y_min = 0; $y_max = 1007;
$lon_min = 120.47; $lon_max = 125.00;
$lat_min = 46.11; $lat_max = 49.26;
$x = $x_min + ($x_max - $x_min) *
( 0.99 - ($lon - $lon_min) / ($lon_max - $lon_min) );
$y = $y_max - ($y_max - $y_min) *
( ($lat - $lat_min) / ($lat_max - $lat_min) );
return array("x"=>round($x),"y"=>round($y));
}
// Close the database connection
mysql_close();
// - Closed
//end
?>Thanks.