# 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)
• 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:

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

if (\$iteration == MAX_ITERATIONS) {

6. Another good optimization is to use "\$x2 = \$z[0] * \$z[0]" instead of "pow(\$z[0], 2)" – approx. 30% faster.

7. 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).Â

8. 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.

9. this isnt just cpu resource heavy, it used 400mb in cache to calculate the image

10. Rather badly written I must say – a page full of Notices, I’m going to do a nice clean rewrite ðŸ˜‰

11. Could somebody help to begin a fractal…
I’ve to make a fractal for school but I don’t know how.

Wybren

12. this works like a charm, thanks!

I integrated this into http://randomimage.setgetgo.com for anyone who wants to see what they look like (since the script is no longer accessible)

13. 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.