forked from Xiphe/GMapsMakerCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGMapsMarker.php
312 lines (260 loc) · 7.08 KB
/
GMapsMarker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
namespace Xiphe\GMaps;
/**
* A small API that creates custom markers for Google Maps
*
* @author Hannes Diercks <[email protected]>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE
* @link https://github.com/Xiphe/GMapsMakerCreator/
*/
class Marker {
const DS = DIRECTORY_SEPARATOR;
public static $basePath = './';
public static $cacheLiveTime = 2592000; // 30 Days
public static $checkCacheEvery = 604800; // 7 Days
private static $_checkedCache = false;
private static $_basePathIsAbsolute = false;
private $_defaults = array(
'content' => '•',
'font-weight' => 'normal',
'color' => '000000',
'border-color' => '9D3E38',
'background-color' => 'FE7D72',
'font-size' => 17,
'left' => 6,
'top' => 16,
'2x' => 0
);
public $settings = array();
public function __construct($options = array())
{
self::_makeAbsoluteBasePath();
foreach ($this->_defaults as $key => $value) {
if (!empty($options[$key])) {
$value = $options[$key];
} elseif (!empty($_GET[$key])) {
$normalizer = array($this, 'normalize_' . str_replace('-', '_', $key));
if (is_callable($normalizer)) {
$value = call_user_func($normalizer, $_GET[$key]);
} else {
$value = $_GET[$key];
}
}
$this->settings[$key] = $value;
}
}
public function normalize_content($value)
{
if (!is_string($value)) {
return 'A';
}
if (strlen($value) > 2) {
return substr($value, 0, 2);
}
return $value;
}
public function normalize_font_weight($value)
{
if (!in_array($value, array('normal', 'bold'))) {
$value = 'normal';
}
return $value;
}
public function normalize_color($value, $default = false)
{
$default = $default ? $default : $this->_defaults['color'];
if (!is_string($value)) {
return $default;
}
$value = preg_replace('/[^0-9A-F]/', "", strtoupper($value));
if (!in_array(strlen($value), array(3, 6))) {
return $default;
}
return $value;
}
public function normalize_border_color($value)
{
return $this->normalize_color($value, $this->_defaults['border-color']);
}
public function normalize_background_color($value)
{
return $this->normalize_color($value, $this->_defaults['background-color']);
}
public function normalize_font_size($value)
{
$value = intval($value);
if ($value > 20 || $value < 2) {
$value = $this->_defaults['font-size'];
}
return $value;
}
public function normalize_top($value)
{
$value = intval($value);
if ($value > 90 || $value < -30) {
$value = $this->_defaults['top'];
}
return $value;
}
public function normalize_left($value)
{
return $this->normalize_top($value);
}
public function normalize_2x($value)
{
return (boolean) $value;
}
public function create()
{
ksort($this->settings);
$serial = md5(serialize($this->settings));
$file = self::$basePath . 'cache' . self::DS . $serial . '.png';
if (!file_exists($file)) {
$image = $this->getBackground();
$border = $this->getBorder();
imagecopy($image, $border, 0, 0, 0, 0, 19, 31);
imagettftext(
$image,
$this->settings['font-size'],
0,
$this->settings['left'],
$this->settings['top'],
$this->getTextColorFor($image),
$this->getFontFile(),
$this->getText()
);
imagepng($image, $file);
} else {
touch($file);
}
return $file;
}
public function getText()
{
return preg_replace('~^(&([a-zA-Z0-9]);)~', htmlentities('${1}'), $this->settings['content']);
}
public function getTextColorFor($img)
{
$color = $this->hexToRGB($this->settings['color']);
return ImageColorAllocate($img, $color[0], $color[1], $color[2]);
}
public function getFontFile()
{
return self::$basePath . 'res' . self::DS . 'fonts' . self::DS . 'SourceSansPro-' . strtolower($this->settings['font-weight']) . '.ttf';
}
public function getBackground()
{
return $this->getImageInColor(
self::$basePath . 'res' . self::DS . 'img' . self::DS . 'background.png',
$this->hexToRGB($this->settings['background-color'])
);
}
public function getBorder()
{
return $this->getImageInColor(
self::$basePath . 'res' . self::DS . 'img' . self::DS . 'border.png',
$this->hexToRGB($this->settings['border-color'])
);
}
public function __toString()
{
header('Content-Type: image/png');
$file = $this->create();
$expires = filemtime($file) + self::$cacheLiveTime;
header("Expires: " . date(DATE_RFC1123, $expires));
echo file_get_contents($file);
exit();
}
public function getMarkerFile($onlyFilename = false)
{
$file = $this->create();
return !$onlyFilename ? $file : basename($file);
}
/* http://snipplr.com/view/4621/ */
public function hexToRGB($hex)
{
$color = array();
if(strlen($hex) == 3) {
$color[] = hexdec(substr($hex, 0, 1) . $r);
$color[] = hexdec(substr($hex, 1, 1) . $g);
$color[] = hexdec(substr($hex, 2, 1) . $b);
}
else if(strlen($hex) == 6) {
$color[] = hexdec(substr($hex, 0, 2));
$color[] = hexdec(substr($hex, 2, 2));
$color[] = hexdec(substr($hex, 4, 2));
}
return $color;
}
/* Inspired by http://stackoverflow.com/questions/5753388/php-gd-color-replacement-with-alpha-gives-images-a-border */
public function getImageInColor($source, $newColor)
{
$source = imagecreatefrompng($source);
$w = imagesx($source);
$h = imagesy($source);
$target = imagecreatetruecolor($w, $h);
$transparent = imagecolorallocatealpha($target, 0, 0, 0, 127);
imagefill($target, 0, 0, $transparent);
// Work through pixels
for($y=0;$y<$h;$y++) {
for($x=0;$x<$w;$x++) {
$rgb = imagecolorsforindex($source, imagecolorat($source, $x, $y));
$pixelColor = imagecolorallocatealpha($target, $newColor[0], $newColor[1], $newColor[2], $rgb['alpha']);
imagesetpixel($target, $x, $y, $pixelColor);
}
}
imageSaveAlpha($target, true);
return $target;
}
private static function _makeAbsoluteBasePath()
{
if (!self::$_basePathIsAbsolute) {
self::$_basePathIsAbsolute = true;
self::$basePath = realpath(dirname(__FILE__) . self::DS . self::$basePath) . self::DS;
}
}
private static function _ensureFileExists($file)
{
if (!file_exists($file)) {
$path = dirname($file);
if (!is_dir($path)) {
@mkdir($path, 0777, true);
}
$handle = @fopen($file, 'w');
if ($handle) {
@fclose($handle);
}
unset($handle);
if (!file_exists($file) || !is_writable($file)) {
throw new Exception("File does not exist or is not writable: $file");
}
}
}
private static function _checkCache()
{
$cacheFolder = self::$basePath . 'cache' . self::DS;
$flag = $cacheFolder . '.gmapsmaker';
$check = false;
if (!file_exists($flag)) {
$check = true;
self::_ensureFileExists($flag);
}
if (!$check && self::$checkCacheEvery + filemtime($flag) > time()) {
return;
}
foreach (glob($cacheFolder . '*') as $file) {
if (filemtime($file) + self::$cacheLiveTime < time()) {
unlink($file);
}
}
touch($flag);
}
public function __destruct()
{
if (!self::$_checkedCache) {
self::$_checkedCache = true;
self::_checkCache();
}
}
}
class Exception extends \Exception {};