PHP Classes

File: examples/generate.php

Recommend this page to a friend!
  Classes of Vitalij Mik   SimpleX Noise Algorithm   examples/generate.php   Download  
File: examples/generate.php
Role: Example script
Content type: text/plain
Description: Example script
Class: SimpleX Noise Algorithm
Generate noise values using the SimpleX algorithm
Author: By
Last change:
Date: 2 years ago
Size: 1,549 bytes
 

Contents

Class file image Download
<?php

declare(strict_types=1);
error_reporting(-1);
require_once
__DIR__ . '/../vendor/autoload.php';

$mapWidth = $_GET['w'] ?? 100;
$mapHeight = $_GET['h'] ?? 100;
$offsetX = $_GET['x'] ?? 0;
$offsetY = $_GET['y'] ?? 0;
$scale = $_GET['scale'] ?? 5;
$zoom = $_GET['zoom'] ?? 0.025;
$octaves = $_GET['octaves'] ?? 4;
$persistence = $_GET['persistence'] ?? 0.5;
$elevation = $_GET['elevation'] ?? 1.0;
$gradient = $_GET['gradient'] ?? 'greyscale.png';

$gradient = imagecreatefrompng(__DIR__ . '/gradients/' . $gradient);

$colors = [];

for (
$i = 0; $i < 256; $i++) {
   
$colors[] = imagecolorat($gradient, $i, 1);
}


$displayWidth = $mapWidth * $scale;
$displayHeight = $mapHeight * $scale;


$noiseImage = imagecreatetruecolor((int)$mapWidth, (int)$mapHeight);
$displayImage = imagecreatetruecolor($displayWidth, $displayHeight);

$noise2D = new \BlackScorp\SimplexNoise\Noise2D(
    (float)
$zoom,
    (int)
$octaves,
    (float)
$persistence,
    (float)
$elevation
);

for (
$x = 0; $x < $mapWidth; $x++) {
    for (
$y = 0; $y < $mapHeight; $y++) {
       
$locationX = $offsetX + $x;
       
$locationY = $offsetY + $y;
       
$greyValue = $noise2D->getGreyValue($locationX, $locationY);
       
$color = $colors[$greyValue];
       
imagesetpixel($noiseImage, $x, $y, $color);
    }
}


imagecopyresampled(
   
$displayImage,
   
$noiseImage,
   
0,
   
0,
   
0,
   
0,
   
$displayWidth,
   
$displayHeight,
    (int)
$mapWidth,
    (int)
$mapHeight
);


header('Content-Type:image/png');
imagepng($displayImage);
imagedestroy($displayImage);