Skip to content
This repository has been archived by the owner on Oct 5, 2018. It is now read-only.

changed Cache behaviour #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 23 additions & 30 deletions staticmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

protected $maxWidth = 1024;
protected $maxHeight = 1024;
protected $maxtiles = 0;

protected $tileSize = 256;
protected $tileSrcUrl = array('mapnik' => 'http://tile.openstreetmap.org/{Z}/{X}/{Y}.png',
Expand Down Expand Up @@ -185,6 +186,10 @@ public function initCoords()
$this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize);
}

public function normalizeTileCoord($position){
return ($position < 0) ? $this->maxtiles + $position : $position%$this->maxtiles;
}

public function createBaseMap()
{
$this->image = imagecreatetruecolor($this->width, $this->height);
Expand All @@ -198,10 +203,11 @@ public function createBaseMap()
$this->offsetY += floor($this->height / 2);
$this->offsetX += floor($startX - floor($this->centerX)) * $this->tileSize;
$this->offsetY += floor($startY - floor($this->centerY)) * $this->tileSize;

$this->maxtiles = sqrt(pow(2,(2*$this->zoom)));

for ($x = $startX; $x <= $endX; $x++) {
for ($y = $startY; $y <= $endY; $y++) {
$url = str_replace(array('{Z}', '{X}', '{Y}'), array($this->zoom, $x, $y), $this->tileSrcUrl[$this->maptype]);
$url = str_replace(array('{Z}', '{X}', '{Y}'), array($this->zoom, $this->normalizeTileCoord($x), $y), $this->tileSrcUrl[$this->maptype]);
$tileData = $this->fetchTile($url);
if ($tileData) {
$tileImage = imagecreatefromstring($tileData);
Expand All @@ -215,6 +221,8 @@ public function createBaseMap()
imagecopy($this->image, $tileImage, $destX, $destY, 0, 0, $this->tileSize, $this->tileSize);
}
}
$this->mkdir_recursive(dirname($this->mapCacheIDToFilename()), 0777);
imagepng($this->image, $this->mapCacheIDToFilename(), 9);
}


Expand Down Expand Up @@ -307,7 +315,7 @@ public function checkMapCache()

public function serializeParams()
{
return join("&", array($this->zoom, $this->lat, $this->lon, $this->width, $this->height, serialize($this->markers), $this->maptype));
return join("&", array($this->zoom, $this->lat, $this->lon, $this->width, $this->height, $this->maptype));
}

public function mapCacheIDToFilename()
Expand Down Expand Up @@ -367,40 +375,25 @@ public function sendHeader()
public function makeMap()
{
$this->initCoords();
$this->createBaseMap();
if ($this->useMapCache){
if($this->checkMapCache()){
$this->image = imagecreatefrompng($this->mapCacheIDToFilename());
} else{
$this->createBaseMap();
}
} else{
$this->createBaseMap();
}
if (count($this->markers)) $this->placeMarkers();
if ($this->osmLogo) $this->copyrightNotice();
}

public function showMap()
{
$this->parseParams();
if ($this->useMapCache) {
// use map cache, so check cache for map
if (!$this->checkMapCache()) {
// map is not in cache, needs to be build
$this->makeMap();
$this->mkdir_recursive(dirname($this->mapCacheIDToFilename()), 0777);
imagepng($this->image, $this->mapCacheIDToFilename(), 9);
$this->sendHeader();
if (file_exists($this->mapCacheIDToFilename())) {
return file_get_contents($this->mapCacheIDToFilename());
} else {
return imagepng($this->image);
}
} else {
// map is in cache
$this->sendHeader();
return file_get_contents($this->mapCacheIDToFilename());
}

} else {
// no cache, make map, send headers and deliver png
$this->makeMap();
$this->sendHeader();
return imagepng($this->image);

}
$this->makeMap();
$this->sendHeader();
return imagepng($this->image);
}

}
Expand Down