How To: Geo Target Banners?

murphy25890

New member
Apr 30, 2007
30
0
0
I need some dating banners that geo targets the city of the user. Any thoughts on how I can do this?

Thank you in advance,
D
 


You mean like dynamically generated images?

Below is a quick example I googled of how to write text onto an image:

Code:
<?
// load the image from the file specified:
$im = imagecreatefrompng("image.png");
// if there's an error, stop processing the page:
if(!$im)
{
 die("");
}
 
// define some colours to use with the image
$yellow = imagecolorallocate($im, 255, 255, 0);
$black = imagecolorallocate($im, 0, 0, 0);
 
// get the width and the height of the image
$width = imagesx($im);
$height = imagesy($im);
 
// draw a black rectangle across the bottom, say, 20 pixels of the image:
imagefilledrectangle($im, 0, ($height-20) , $width, $height, $black);
 
// now we want to write in the centre of the rectangle:
$font = 4; // store the int ID of the system font we're using in $font
$text = "vdhri.net"; // store the text we're going to write in $text
// calculate the left position of the text:
$leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2;
// finally, write the string:
imagestring($im, $font, $leftTextPos, $height-18, $text, $yellow);
 
// output the image
// tell the browser what we're sending it
Header('Content-type: image/png');
// output the image as a png
imagepng($im);
 
// tidy up
imagedestroy($im);
?>

Except in this case you could replace the value of $text with the city doing something like this:

Code:
$geo = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
$text = $geo['geoplugin_city'];

So you can play with the above code above, by using a pre-existing image, and then moving the x,y position of the text a little.

I would also suggest that the code saves the new png as banner-city-name.png , and maybe use file_exists() to pull it up as a way to cache city banners already created so you can ease up on the CPU load and speed up subsequent loads.