PHP Classes

How Can PHP Generate Noise Images Using the SimpleX Noise Algorithm: Generate noise values using the SimpleX algorithm

Recommend this page to a friend!
  Info   View files Example   View files View files (14)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 53 This week: 1All time: 10,590 This week: 560Up
Version License PHP version Categories
simplexnoise 1.0.0BSD License5Algorithms, PHP 5, Graphics
Description 

Author

This package can generate noise images using the SimpleX algorithm.

It can take several parameters to configure the generation of noise values like the zoom level, octaves, persistence, and elevation.

The class can return the noise level values in different positions given the X and Y coordinates of the points in the noise plane.

Innovation Award
PHP Programming Innovation award nominee
August 2022
Number 9
Noise is a signal that can cause certain disturbances. It is often associated with sound but can also be associated with an image.

Noise images can be helpful. For instance, you may use noise images to generate CAPTCHA validation images that may obfuscate a picture with text that the user needs to recognize.

The noise image can help obfuscate the text to make it harder for robots to recognize the text while humans may be able to identify it.

This package can generate noise data for creating noise images for CAPTCHA validation or other purposes.

It uses the SimpleX noise algorithm. This algorithm has several advantages over classic forms of generating noise like the Perlin algorithm.

For instance, the simplex algorithm requires less computing power to create noise.

Manuel Lemos
Picture of Vitalij Mik
  Performance   Level  
Name: Vitalij Mik <contact>
Classes: 7 packages by
Country: Germany Germany
Age: 37
All time rank: 3497195 in Germany Germany
Week rank: 416 Up16 in Germany Germany Up
Innovation award
Innovation award
Nominee: 5x

Example

<?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);


Details

PHP SimplexNoise

this is just a php version of the original code at

https://weber.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf

Installation

composer require blackscorp/simplexnoise

Usage

$noise2D = new \BlackScorp\SimplexNoise\Noise2D();
$greyValue = $noise2D->getGreyValue($locationX, $locationY);
var_dump($greyValue); //a value between 0 and 255

Examples

for more examples and details please take a look at examples folder. just copy more png images into exampels/gradients in order to create cool effects

currently only 2D is implemented


  Files folder image Files  
File Role Description
Files folder imageexamples (2 files, 1 directory)
Files folder imagesrc (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
Files folder imagegradients (6 files)
  Accessible without login Plain text file generate.php Example Example script
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files  /  examples  /  gradients  
File Role Description
  Accessible without login Image file example1.png Icon Icon image
  Accessible without login Image file fire-gradient.png Icon Icon image
  Accessible without login Image file greyscale-inverted.png Icon Icon image
  Accessible without login Image file greyscale.png Icon Icon image
  Accessible without login Image file land-and-sea.png Icon Icon image
  Accessible without login Image file tropical.png Icon Icon image

  Files folder image Files  /  src  
File Role Description
  Plain text file Noise2D.php Class Class source
  Plain text file NoiseData.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:53
This week:1
All time:10,590
This week:560Up