Mandelbrot in PHP

After reading up on SkyLined’s post I thought it might be nice to try some in PHP. I first Googled for an example and quickly found this post:

http://cow.neondragon.net/index.php/2680-Php-Fractal-Generator

Unfortunately that is quite inefficient. So I made some adjustments. I can now render a 500*500 pixel fractal in 15-16 seconds on my laptop (1.2Ghz DualCore, one used) (see image). That is quite faster than the example.
(I’ll test on this server and at work for reference, watch for updates). I’ll post the code here soon.

Next up is a mashup between SkyLined’s and my script to have PHP render the ASCII art fractals and animations, maybe I can render animated gifs :-)

4 Comments to “Mandelbrot in PHP”

  1. Eric
    2008/05/27

    Nice example, curious for the results.
    Lets use the DH server for real-time fractal rendering ;)

  2. zylox
    2008/06/27

    hi! have you got any news on this? i would be interested, also in the sourcecode!

  3. George
    2010/01/28

    are you going to post the code anytime soon?

  4. Cipher
    2010/01/28

    Terribly sorry about this. Here is the code. provide ?w=500&h=500 for width and height, there are no default values.

    < ?php
    /* Timing */
    set_time_limit(0);
    function microtime_float() {
    list($usec, $sec) = explode(' ', microtime());
    return ((float)$usec + (float)$sec);
    }
    $time_start = microtime_float();
    /* Lil Mandlebrot Set Fractal Generator */
    define ('IMAGE_WIDTH', intval($_GET['w']));
    define ('IMAGE_HEIGHT', intval($_GET['h']));
    define ('X_MIN', -1.8);
    define ('X_MAX', 0.5);
    define ('Y_MIN', -1);
    define ('Y_MAX', 1);
    define ('MAX_ITERATIONS', 100); // if you change this to a number bigger than 200, make sure you create a new palette.png
    $image = imagecreate(IMAGE_WIDTH, IMAGE_HEIGHT);
    // Load the palette to find colours
    $colours = array();
    $palette = imagecreatefrompng('palette.png');
    $i_width = imagesx($palette);
    for ($i=0; $i<$i_width; ++$i) {
    $rgb = imagecolorat($palette, $i, 0);
    $colours[$i] = imagecolorallocate($image, ($rgb >> 16) & 0xFF, ($rgb >> 8) & 0xFF, $rgb & 0xFF);
    }
    $colours[MAX_ITERATIONS+1] = imagecolorallocate($image, 0, 0, 0);
    $value_Y = (Y_MAX - Y_MIN) / (IMAGE_HEIGHT-1);
    $value_X = (X_MAX - X_MIN) / (IMAGE_WIDTH-1);
    for ($i=0; $i for ($j=0; $j // What values of x and y does this pixel represent?
    $x = X_MIN+$i*$value_X;
    $y = Y_MIN+$j*$value_Y;
    // C=x+yi
    $iteration = 0;
    $z0 = 0; // The initial value of z is 0+0i
    $z1 = 0; // The initial value of z is 0+0i
    //$magnitude = 0;
    // Optimization: Store x^2 and y^2 so we don't have to keep calculating it
    $x2 = 0;
    $y2 = 0;
    // If the |z| > 2 ever, then the sequence will tend to infinity so we can exit the loop
    while ($iteration < = MAX_ITERATIONS && $x2+$y2 <= 4) {
    // In order to get the next term in the sequence
    // we need to square the previous number and then add
    // the original complex number
    $k0 = $z0;
    $k1 = $z1;
    // z is a complex number. When we square a complex number in the form x+yi:
    // New real component: x^2-y^2
    // New imaginary component: 2*real*imaginary
    $z0 = $x2 - $y2+ $x;
    $z1 = 2 * $k0 * $k1 + $y;
    $x2 = $z0 * $z0;
    $y2 = $z1 * $z1;
    ++$iteration;
    }
    imagesetpixel($image, $i, $j, $colours[$iteration]);
    }
    }
    $time_end = microtime_float();
    $black = imagecolorallocate($image, 255, 255, 255);
    $font = 'E:\Documents\Websites\arial.ttf';
    imagettftext($image, 12, 0, 12, 12, $black, $font, ($time_end - $time_start));
    header('Content-type: image/png');
    imagepng($image);
    ?>

Leave a Comment

*

*