static function getSpriteImageMap($AppearanceID) { $PNGPath = SPRITE_PATH . "{$AppearanceID}.png"; $MapPath = FSPATH . "cg_render/{$AppearanceID}-linedata.json.gz"; if (file_exists($MapPath) && filemtime($MapPath) >= filemtime($PNGPath)) { $Map = JSON::decode(gzuncompress(file_get_contents($MapPath))); } else { if (!file_exists($PNGPath)) { return null; } list($PNGWidth, $PNGHeight) = getimagesize($PNGPath); $PNG = imagecreatefrompng($PNGPath); imagesavealpha($PNG, true); $allcolors = array(); function coords($w, $h) { for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { (yield array($x, $y)); } } } foreach (coords($PNGWidth, $PNGHeight) as $pos) { list($x, $y) = $pos; $rgb = imagecolorat($PNG, $x, $y); $colors = imagecolorsforindex($PNG, $rgb); $hex = strtoupper('#' . CoreUtils::pad(dechex($colors['red'])) . CoreUtils::pad(dechex($colors['green'])) . CoreUtils::pad(dechex($colors['blue']))); $opacity = $colors['alpha'] ?? 0; if ($opacity === 127) { continue; } $allcolors[$hex][$opacity][] = array($x, $y); } $mapping = 0; $currLine = null; $lines = array(); $lastx = -2; $lasty = -2; $_colorsAssoc = array(); $colorno = 0; foreach ($allcolors as $hex => $opacities) { if (!isset($_colorsAssoc[$hex])) { $_colorsAssoc[$hex] = $colorno; $colorno++; } foreach ($opacities as $opacity => $coords) { foreach ($coords as $pos) { list($x, $y) = $pos; if ($x - 1 != $lastx || $y != $lasty) { if (isset($currLine)) { $lines[] = $currLine; } $currLine = array('x' => $x, 'y' => $y, 'width' => 1, 'colorid' => $_colorsAssoc[$hex], 'opacity' => $opacity); } else { $currLine['width']++; } $lastx = $x; $lasty = $y; } } } if (isset($currLine)) { $lines[] = $currLine; } $Output = array('width' => $PNGWidth, 'height' => $PNGHeight, 'linedata' => array(), 'colors' => array_flip($_colorsAssoc)); foreach ($lines as $line) { $Output['linedata'][] = $line; } $Map = $Output; file_put_contents($MapPath, gzcompress(JSON::encode($Output), 9)); } return $Map; }
function coords($city) { $maps_request = 'http://' . MAPS_HOST . '/maps/geo?output=json&key=' . MAPS_KEY . '&sensor=false&q=' . urlencode($city); $response = file_get_contents($maps_request); $info = json_decode($response); if (is_object($info) && is_array($info->Placemark)) { return array($info->Placemark[0]->address, $info->Placemark[0]->Point->coordinates[1], $info->Placemark[0]->Point->coordinates[0]); } return array('Not Found', 0, 0); } $found = false; $cities = json_decode(file_get_contents('cities.json')); if (!is_array($cities)) { $cities = array(); } list($city, $lat, $lng) = coords($_REQUEST['input']); foreach ($cities as $k => $c) { if ($city == $c->name) { $c->count++; $found = true; break; } } if (!$found) { $c = new StdClass(); $c->name = $city; $c->count = 1; $c->lat = $lat; $c->lng = $lng; $cities[] = $c; }