Perlin noise generator in PHP

Perlin noiseGenerating Perlin noise requires heavy calculation and PHP (in my opinion) is not the best language for this task but still it can be useful.
The algorithm is an adaptation form this pseudo code.
The class seems to work but I'm not if it works 100% correctly. The tricky part was to deal with the types because PHP tends to convert large integers to float, and the algorithm uses bitwise operators on some stages.

This class is just for testing purposes it consumes a lot of CPU cycles and I hardly recommend to avoid using it in live environment. Try to keep the cycles low as well as the octaves number close to 1.

Sample SVG result: noise.svg.
Example usage (generate SVG map of terrain):

 
  require("class.perlin.php");
  $per = new Perlin;
 
  $xl = 100;
  $yl = 40;
 
  header("Content-type: image/svg+xml");
  echo '<?xml version="1.0" standalone="no"?>';
  echo '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
  echo '<svg width="400" height="400"
  xmlns="http://www.w3.org/2000/svg" version="1.1">';
  $size = 3;
  for ($x = 0; $x < $xl; $x++){
    for ($y = 0; $y < $yl; $y++){
      $n = $per->perlinNoise2d($x, $y)*255;
      $c = '#986121';
      if ($n < 50){
        $c = '#0000ff';
      }
      if ($n > 200){
        $c = '#FFFFFF';
      }
      echo "<rect x=\"".($x*$size)."\" y=\"".($y*$size)."\"
            width=\"$size\" height=\"$size\" fill=\"$c\"/>\n";
    }
  }
  echo '</svg>';
 

Download: classperlinphp.zip

  • http://svg.startpagina.nl stelt

    Why not show the actual resulting SVG here? (as opposed to the raster)

  • http://blog.horemag.net AquilaX

    You can found sample output here.

  • http://www.dinfluence.net Christian

    I have tried using your class and i get everything in a single color except for some pixels on the top of the image :/

  • http://blog.horemag.net AquilaX

    Play with $persistence and $octaves variables also $r1, $r2, $r3 gives different result depending on the values.

  • Derek

    I’m sorry but I don’t think this class works. Especially not well enough to generate the image you have displayed. It does some variation near the left side and the rest is always 1 solid color. Even changing the variables to all imaginable combination it still results in the same type of thing.

    I’d really love a version that worked but this was posted awhile ago so I doubt I’ll get a response here.

  • http://blog.horemag.net AquilaX

    Derek:
    That’s because the loop sizes are
    $xl = 100;
    $yl = 40;
    and the SVG’s dimensions are 400×400px.

    Just make both equal.

  • Dylan

    Hmm it seems this doesn’t work. There is minimal randomness produced… try copying/pasting all your own code into it’s own area and see for yourself. I also ported this to javascript and am getting weird results… the noise doesn’t spread over the entire area, but eventually maxes out at -1

  • Joseph

    Yep, same here, only the two top rows are getting random data. After that, it evens out to a solid color.