function GenerateMap($size) { global $sessionID, $mapName, $captureBox, $printSize, $normalizedCapture, $rotation, $scaleDenominator, $printDpi, $drawNorthArrow; $userInfo = new MgUserInformation($sessionID); $siteConnection = new MgSiteConnection(); $siteConnection->Open($userInfo); $resourceService = $siteConnection->CreateService(MgServiceType::ResourceService); $renderingService = $siteConnection->CreateService(MgServiceType::RenderingService); $map = new MgMap(); $map->Open($resourceService, $mapName); $selection = new MgSelection($map); // Calculate the generated picture size $envelope = $captureBox->Envelope(); $normalizedE = $normalizedCapture->Envelope(); $size1 = new Size($envelope->getWidth(), $envelope->getHeight()); $size2 = new Size($normalizedE->getWidth(), $normalizedE->getHeight()); $toSize = new Size($size1->width / $size2->width * $size->width, $size1->height / $size2->height * $size->height); $center = $captureBox->GetCentroid()->GetCoordinate(); $map->SetDisplayDpi($printDpi); $colorString = $map->GetBackgroundColor(); // The returned color string is in AARRGGBB format. But the constructor of MgColor needs a string in RRGGBBAA format $colorString = substr($colorString, 2, 6) . substr($colorString, 0, 2); $color = new MgColor($colorString); $mgReader = $renderingService->RenderMap($map, $selection, $center, $scaleDenominator, $toSize->width, $toSize->height, $color, "PNG", false); $tempImage = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "mgo" . uniqid(); $mgReader->ToFile($tempImage); $image = imagecreatefrompng($tempImage); unlink($tempImage); // Rotate the picture back to be normalized $normalizedImg = imagerotate($image, -$rotation, 0); // Free the original image imagedestroy($image); // Crop the normalized image $croppedImg = imagecreatetruecolor($size->width, $size->height); imagecopy($croppedImg, $normalizedImg, 0, 0, (imagesx($normalizedImg) - $size->width) / 2, (imagesy($normalizedImg) - $size->height) / 2, $size->width, $size->height); // Free the normalized image imagedestroy($normalizedImg); if ($drawNorthArrow) { // Draw the north arrow on the map DrawNorthArrow($croppedImg); } header("Content-type: image/png"); imagepng($croppedImg); //Uncomment to see what PHP-isms get spewed out that may be tripping up rendering //Also replace instances of "////print_r" with "//print_r" to insta-uncomment all debugging calls /* ob_start(); imagepng($croppedImg); $im = base64_encode(ob_get_contents()); ob_end_clean(); header("Content-type: text/html", true); echo "<img src='data:image/png;base64,".$im."' alt='legend image'></img>"; */ imagedestroy($croppedImg); }
function GenerateMap($size) { global $sessionID, $mapName, $captureBox, $printSize, $normalizedCapture, $rotation, $scaleDenominator, $printDpi; InitializeWebTier(); $userInfo = new MgUserInformation($sessionID); $siteConnection = new MgSiteConnection(); $siteConnection->Open($userInfo); $resourceService = $siteConnection->CreateService(MgServiceType::ResourceService); $renderingService = $siteConnection->CreateService(MgServiceType::RenderingService); $map = new MgMap(); $map->Open($resourceService, $mapName); $selection = new MgSelection($map); // Calculate the generated picture size $envelope = $captureBox->Envelope(); $normalizedE = $normalizedCapture->Envelope(); $size1 = new Size($envelope->getWidth(), $envelope->getHeight()); $size2 = new Size($normalizedE->getWidth(), $normalizedE->getHeight()); $toSize = new Size($size1->width / $size2->width * $size->width, $size1->height / $size2->height * $size->height); $center = $captureBox->GetCentroid()->GetCoordinate(); $map->SetDisplayDpi($printDpi); $colorString = $map->GetBackgroundColor(); // The returned color string is in AARRGGBB format. But the constructor of MgColor needs a string in RRGGBBAA format $colorString = substr($colorString, 2, 6) . substr($colorString, 0, 2); $color = new MgColor($colorString); $mgReader = $renderingService->RenderMap($map, $selection, $center, $scaleDenominator, $toSize->width, $toSize->height, $color, "PNG", false); $tempImage = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "mgo" . uniqid(); $mgReader->ToFile($tempImage); $image = imagecreatefrompng($tempImage); unlink($tempImage); // Rotate the picture back to be normalized $normalizedImg = imagerotate($image, -$rotation, 0); // Free the original image imagedestroy($image); // Crop the normalized image $croppedImg = imagecreatetruecolor($size->width, $size->height); imagecopy($croppedImg, $normalizedImg, 0, 0, (imagesx($normalizedImg) - $size->width) / 2, (imagesy($normalizedImg) - $size->height) / 2, $size->width, $size->height); // Free the normalized image imagedestroy($normalizedImg); // Draw the north arrow on the map DrawNorthArrow($croppedImg); header("Content-type: image/png"); imagepng($croppedImg); imagedestroy($croppedImg); }