Skip to content

Instantly share code, notes, and snippets.

@benedmunds
Created April 10, 2014 06:26
Show Gist options
  • Save benedmunds/10347729 to your computer and use it in GitHub Desktop.
Save benedmunds/10347729 to your computer and use it in GitHub Desktop.
Create gMaps Marker dynamically with PHP
<?php
$c = $_GET['color'];
$t = $_GET['text'];
$cHex = str_split($c, 2);
header("Content-Type: image/png");
$im = @imagecreate(40, 40)
or die("Cannot Initialize new GD image stream");
$x1 = 5;
$x2 = 35;
$y1 = 5;
$y2 = 30;
$radius = 5;
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
$white = imagecolorallocate($im, 0xff, 0xff, 0xff);
$color = imagecolorallocate($im, '0x' . $cHex[0], '0x' . $cHex[1], '0x' . $cHex[2]);
imagecolortransparent($im, $black);
// draw rectangle without corners
imagefilledrectangle($im, $x1 + $radius, $y1, $x2 - $radius, $y2, $white);
imagefilledrectangle($im, $x1, $y1 + $radius, $x2, $y2 - $radius, $white);
// draw circled corners
imagefilledellipse($im, $x1 + $radius, $y1 + $radius, $radius * 2, $radius * 2, $white);
imagefilledellipse($im, $x2 - $radius, $y1 + $radius, $radius * 2, $radius * 2, $white);
imagefilledellipse($im, $x1 + $radius, $y2 - $radius, $radius * 2, $radius * 2, $white);
imagefilledellipse($im, $x2 - $radius, $y2 - $radius, $radius * 2, $radius * 2, $white);
$x1 += 2;
$x2 -= 2;
$y1 += 2;
$y2 -= 2;
// draw rectangle without corners
imagefilledrectangle($im, $x1 + $radius, $y1, $x2 - $radius, $y2, $color);
imagefilledrectangle($im, $x1, $y1 + $radius, $x2, $y2 - $radius, $color);
// draw circled corners
imagefilledellipse($im, $x1 + $radius, $y1 + $radius, $radius * 2, $radius * 2, $color);
imagefilledellipse($im, $x2 - $radius, $y1 + $radius, $radius * 2, $radius * 2, $color);
imagefilledellipse($im, $x1 + $radius, $y2 - $radius, $radius * 2, $radius * 2, $color);
imagefilledellipse($im, $x2 - $radius, $y2 - $radius, $radius * 2, $radius * 2, $color);
$points1 = array(
15, 30, // point 1
25, 30, // point 2
20, 35 // point 3
);
$points2 = array(
15, 28, // point 1
25, 28, // point 2
20, 33 // point 3
);
imagefilledpolygon($im, $points1, 3, $white);
imagefilledpolygon($im, $points2, 3, $color);
$font = 'C:\Windows\Fonts\arial.ttf';
if (strlen($t) > 3) {
$text = str_split($t, 2);
$t = $text[0] . '...';
$fs = 11;
$fx = 8;
} elseif (strlen($t) == 3) {
$fs = 11;
$fx = 8;
} elseif (strlen($t) == 2) {
$fs = 12;
$fx = 12;
} else {
$fs = 12;
$fx = 16;
}
imagettftext($im, $fs, 0, $fx, 23, $white, $font, $t);
imagepng($im);
imagedestroy($im);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment