#1 Designing Rubik's cube in PHP: The basics, phpgd usage
2009-07-06
Firstly I should introduce you one library we will need today. It's called phpgd and it brings some useful functions. We wouldn't get far without them so it's important to have this lib installed. In Linux you can simply install it using your add/remove application. Once you have it installed you can check if it's enabled using phpinfo(); 
Ok, let's say the phpgd is ready, what are the new possibilities you can do with that now? We will start with something simple for beginning. Keep in your mind that every line/polygon/whatever in the final image needs to be scripted first, so, first think than write. We will script a square on a blank screen now. There are 2 ways how to do it /I believe you are able to figure out any third/forth way but let's say there are only 2/. You can command four lines and position them so they make a square. Yeah, that's possible of course but fortunatelly there is a native function for rendering a square. My point is that if you aren't sure what you can/can't do, go through this list of all phpgd functions in the official php documentation http://php.net/manual/en/ref.image.php and find the best which fits your idea. Let's get back to what we started, firstly we need to define the blank image.<?php/** This code doesn't need much explanation, the important things are mentioned in the comments. Maybe you are looking for the php ending tag ?>. Well, it's not important to write it there, it's not a mistake. Both versions with/without this tag are acceptable. If you don't see the output in your browser, press ctrl+a so you select the image, it should went blue. Or fill the image with any other color than white. E.g. black:
* Blank image 400x400 using phpgd...
*/
// set the HTTP header type to PNG
header("Content-type: image/png");
// set a new image 400x400px
$im = imagecreatetruecolor(400, 400);
// set the white color
$white = imagecolorallocate($im, 255, 255, 255);
// fill the image with white (defined in previous step) starting at position [0,0]
imagefill($im, 0, 0, $white);
// send the new PNG image to the browser
ImagePNG($im);
// destroy the reference pointer to the image in memory to free up resources
ImageDestroy($im);
$black = imagecolorallocate($im, 0, 0, 0);
Let's move on now and script the square. We will use the function imagerectangle()
imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
The usage is pretty simple. The first parameter is the "image" (variable $im in our case). Next four pamaremeter are the coordinates. I should also mention that the [0;0] coordinate is the top left corner of the image. [400;400] is the bottom right corner of the image (in our case). So we can make a border around the image for instance... <?php/** This brings a nice black square. I think I should say a few words about setting the colors in phpgd. The function imagecolorallocate() has four parameters.
* Blank image 400x400 with a black sqare on it using phpgd...
*/
// set the HTTP header type to PNG
header("Content-type: image/png");
// set a new image 400x400px
$im = imagecreatetruecolor(400, 400);
// set the colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// fill the image with white (defined in previous step) starting at position [0,0]
imagefill($im, 0, 0, $white);
// make a border around the image
imagerectangle($im, 0, 0, 399, 399, $black);
// send the new PNG image to the browser
ImagePNG($im);
// destroy the reference pointer to the image in memory to free up resources
ImageDestroy($im);
imagecolorallocate ( resource $image , int $red , int $green , int $blue )
This function returns a color identifier representing the color composed of the given RGB components. The first parameter defines the "image", next three define the color. These parameters are integers between 0 and 255 or hexadecimals between 0x00 and 0xFF. So how would you define for example a blue color? Simply... $blue = imagecolorallocate($im, 0, 0, 255);
$darkblue = imagecolorallocate($im, 0, 0, 100);
$lightblue = imagecolorallocate($im, 100, 100, 255);
I added a light blue and dark blue color, I hope it helps you to understand how it works.
I think that's enough for today. Next time we will script a cube and make it all a little more "universal". So see you
















