This tutorial concentrates on drawing lines. There are 2 more basic commands involved in line drawing. The first is line is concerned with setting the thickness of the line in pixels. It works well for horizontal and vertical lines, but it seems for diagonal lines you need to make it wider than it appears. imagesetthickness($image,4); The second line of code is concerned with drawing the line. You need to set the x,y coordinate of the start of the line and the x,y coordinate of the end of the line, as well as the color. imageline($image,x1,y1,x2,y2,$color); | |
Right click, select all, ctrl+c to copy. | |
If you look at the codes and the above image it produced, you will notice that the thickness was set each time I drew a different size line. | |
You can do a lot of things with lines and loops. It may be soon to start with loops but they are not too difficult to understand once you get the concept. What a loop does is tell the server to do something every x time until you get to the end of the loop. This example draws lines to create a simple gradient. There are more extensive codes for gradients, but this is a good one to learn about loops. | |
Right click, select all, ctrl+c to copy. | |
Each color (R/G/B) has a range from 0 to 255. From 0 to 254 by steps of 2 (0, 2, 4, 6, ...) --> 127 possibilities; 127 (R) 127 (G) 127 (B) colors. This loop tells the server to draw a vertical line for each color, and keep drawing until it reaches the end of the loop, in this case 255. The line thickness was set to 2.
imagesetthickness($image,2);
This example uses a "for" loop. $i (is used most often to refer to integer but can be any letter of the alphabet) is set to equal zero; $i <=255; means we want the loop to do something from zero to 255. $i+=2 means to do the thing every 2 integers. ($i++ would mean every single line), but we are using 2. The linethickness is 2. The opening curly bracket starts the command of what exactly to do. I want a color that changes from 0-255 for each line. I want it to draw the lines in those colors. The imageline needs to draw a vertical line at 0,0,0,50, then 2,0,2,50 and so on until it gets to 255,0,255,50. The color would change from 0,0,0 to 255,255,255. Finally you close the loop with a closing curly bracket.
for($i = 0; $i <= 255; $i+=2){ $mywhite=imagecolorallocate($image,$i,$i,$i); ImageLine($image, $i, 0, $i, 50, $mywhite); } |