PHP Fractal Generator

I was bored so I thought I’d write a PHP script to generate fractals (like you do). The Mandlebrot set is probably the most well-known fractal so I chose to write a script to generate fractals using the Mandlebrot set:

This is a 500×500 fractal plotted for x (real) values between -1.5 and 0.5 and y (imaginary) values between -1 and 1.

How it Works

The mathematics behind the mandlebrot set involve:

  • complex numbers (x+yi)
  • quadratics
  • argand diagram
  • limits

A complex numbers consists of a real component and an imaginary component. i is the square root of -1. A complex number could look like 3+2i, where 3 is the real component and 2 is the imaginary component.

An argand diagram is like a two dimensional number line. With normal real numbers, we can plot them on a one dimension number line, with negative infinity on one end, and positive infinity on the other end. Because complex numbers have an imaginary component too, we need a two-dimensional plane to plot the number.

The Mandlebrot set involves iterating through a sequence:

In words: the first value in the sequence is 0. To find the next value in the sequence, we square the current value and add the complex number (which is the variable). Depending on the input, the sequence will either tend towards infinity or it will remain bounded and oscillate/repeat. If the sequence remains bounded, then it is inside the Mandlebrot set. On my script, I’ve marked these pixels as black.

For those values outside the Mandlebrot set, we use colour to denote how long it takes to reach infinity (in fact we use a shortcut and stop when |z| > 2).

Quadratics come in when we square a complex number – you can just square them as you would with any other quadratic. However, i^2 = -1.

Conclusion

I think it’s pretty amazing how we can generate infinitely complex patterns using such simple mathematics. There is probably only about 10 lines of mathematics in the script; the rest is all PHP/image stuff.

A few notes:

  • The script is pretty server intensive. The source code I’ve provided is configured to produce a 100×100 image by default. This took 2 minutes to execute on my machine, but 5 seconds on my friends machine. Both of them are roughly the same spec. Your mileage may vary.
  • If you try to generate an image with too many pixels, your PC may crash. You have been warned 🙂
  • Try changing the x/y min/max values to zoom or to pan.
  • Feel free to hack the script, rip it apart or do whatever you want with it. It’s public domain.

21 thoughts on “PHP Fractal Generator

  1. Obvious first thing you could do to speed it up is remove that unnecessary sqrt() call from the main loop body, and just check if |z|>4 instead.

  2. I’ve updated the source with Tim’s fix, it seems to cut roughly about 40% from the execution time. Also a bug fix so it works properly when you generate a rectangular image.

  3. The math operations can be further optimized: before the loop, you can set ((X_MAX X_MIN) / (IMAGE_WIDTH1)) to a variable so that you don’t need to constantly recalculate this (same for $y). Another trivial change but might help speed things up 🙂

  4. I found this page while looking for image creation script. I need WordPress plugin like WP_Identicon ( http://scott.sherrillmix.com/blog/blogger/wp_identicon/ ) or WP_MonsterID ( http://scott.sherrillmix.com/blog/blogger/wp_monsterid/ ) but with more stylish resulting images. I’m not very good at math, could you point me, how could I use some big number (actually, the email addresh hash) as "seed" for this script? It must genegate unique and optically different image for each given hash.

  5. Great script!

    Noticed some typos:

    1. "y2" instead of "$y2":

    while ($iteration =" ($iterations may be equal to MAX_ITERATIONS + 1):

    if ($iteration == MAX_ITERATIONS) {

  6. Hi Alex,

    Thanks a lot! Your optimization by removing pow() is a beast! On mine it cut the executing time by 90%. (I think my version of PHP must have a broken pow function or something). 

  7. This script is nice but it is a bit slow.  Somebody has already mentioned the pow replacement but also to speed it up a lot you should use plain vairables instead of your 2 variable array.  This cut the time from 45 seconds to 25 for a 500 X 500.  It would be nice if you updated your script with the latest optimisations for those who are not good at php optimisation who still want to make big and pretty pictures.   Also I would recommend cutting the MAX_ITERATIONS down to 100 because I think it looks slightly better and of course the execution time is cut in half.

     This is a lovely script though.  Well done.

  8. Replacing arrays with variables, and replacing pow reduced rendering time of a 400×400, 200 iterations from 23.1 s to 7.9 s on my macbook air.

    Very nice!

    I think the next change I’ll make is to add an algorithm to generate the palette.

Leave a Reply

Your email address will not be published. Required fields are marked *