Ooer.com by Chris Neale

Mondrian Art in PHP

One of my favourite artists is a chap called Mondrian. He lived in the early 20th century in America, and was quite experiemental in his work. He tried to break an image down into its most base forms, to represent things in as little form as possible. He was relatively successful as an artist, and made some pictures I particularly enjoy looking at.


As a sort of homage to Mondrian I set out to write a simple script that generated PNG files in a similar vein to his early works. He tended to make very geometric images with strong primary colour blocks and heavy bold black lines. GD is ideally suited to recreating this sort of work as its basically lines and rectangles.
As a start for our Mondrian image we will need a canvas to draw on. This is simply a 400*400 pixel true colour image.

<?php
$canvas
= imagecreatetruecolor(400,400);
?>

The next stage required is to define some colours to use for painting. In accordance with the Mondrian original we'll use blue, red, yellow, and black for the lines. The block filling colours are entered into an array for easier use later on. We'll also need white for setting the background.

<?php
$white
= imagecolorallocate($canvas,255,255,255);
$black = imagecolorallocate($canvas,0,0,0);
$fill[] = imagecolorallocate($canvas,255,0,0);
$fill[] = imagecolorallocate($canvas,255,255,0);
$fill[] = imagecolorallocate($canvas,0,0,255);
?>

Finally we also need to set the line thickness, paint the canvas white, and define the number of blocks that will be filled in in the final image. This is done by generating a random integer.

<?php
imagesetthickness
($canvas,2);
imagefill($canvas,0,0,$white);
srand((double)microtime()*1000000);
$segments = rand(2,6);
?>

Whether or not you will need the srand() statement depends which version of PHP you are using. Later versions automatically set the random number seed. Once all that is completed we're set to start generating the Mondrian art work.
Move to page: 1 2 3

Comments

Comments are not currently being accepted for this article.
Sidebar
Published:
10/01/2007
Views:
9024
Author:
Chris Neale
Labels:
Print:
Sidebar: