PHP code example of cosmicpe / maprender

1. Go to this page and download the library: Download cosmicpe/maprender library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cosmicpe / maprender example snippets


use cosmicpe\maprender\MapRender;

$render = MapRender::create($plugin);
$image = yield from $render->render($world, $x1, $z1, $x2, $z2);
$output = $plugin->getDataFolder() . DIRECTORY_SEPARATOR . "map-render.png";
imagepng($image, $output);

/** @param Closure(GdImage) : void */
public function render(World $world, int $x1, int $z1, int $x2, int $z2, Closure $callback) : void{
	$render = MapRender::create($plugin);
	$task = $render->render($world, $x1, $z1, $x2, $z2);
	Await::f2c(function() use($task, $callback) : Generator{
		$image = yield from $task;
		$callback($image);
	});
}

$radius = 5;
$world = $server->getWorldManager()->getDefaultWorld();
$spawn = $world->getSpawnLocation();
$x = $spawn->getFloorX() >> Chunk::COORD_BIT_SIZE;
$z = $spawn->getFloorZ() >> Chunk::COORD_BIT_SIZE;

$render = MapRender::create($plugin);
$image = yield from $render->render($world, $x - $radius, $z - $radius, $x + $radius, $z + $radius);
$output = $plugin->getDataFolder() . DIRECTORY_SEPARATOR . "map-render.png";
imagepng($image, $output);

$resource = fopen("php://memory", "rb+");
try{
	imagepng($image, $resource);
	fseek($resource, 0);
	$bytes = stream_get_contents($resource);
}finally{
	fclose($resource);
}

$output = $plugin->getDataFolder() . DIRECTORY_SEPARATOR . "map-render.png";
file_put_contents($output, $bytes);

$width = imagesx($image) * 4;
$image = imagescale($image, $width, -1, IMG_NEAREST_NEIGHBOUR);
imagesavealpha($image, true);
imagepng($image, $output);

$parent = MapRender::create($plugin);

// set custom chunk_loads_per_tick, chunk_gens_per_tick
$chunk_loads_per_tick = 4;
$chunk_gens_per_tick = 1;
$render = new MapRender(
	$parent->plugin,
	$parent->palette,
	$parent->chunks_per_tick,
	$chunk_loads_per_tick,
	$chunk_gens_per_tick,
	$parent->min_subchunk_index,
	$parent->max_subchunk_index
);