Last time I talked about a simple drawing method that avoids GD, and then I used GD to draw the simplest "drawing" - a straight line.
This time I will continue to draw a straight line downwards. The parts that were explained in detail in the last code will not be repeated this time.
<?
Header("Content-type: image/png");
$im = ImageCreate (200, 100);
$col_black = ImageColorAllocate($im, 0,0,0);
$col_orn = ImageColorAllocate($im, 255,192,0);
// Let’s use orange today.
// Exactly the same usage as the imageline function,
ImageDashedLine($im,0,100,199,100,$col_orn);
// This draws a dotted line.
// Let's do a test below. Used to illustrate a problem.
$col_yel = ImageColorAllocate($im, 255,255,0);
//yellow.
ImageLine($im,0,99,199,99,$col_yel);
// Draw a yellow line at the bottom edge of the image.
ImageLine($im,200,0,200,100,$col_orn);
// Tried to draw a orange line on the far right edge of the image, but nothing happened.
// This shows that the coordinate range of an image with a width of 200 and a height of 100 is (0,0) to (199,99).
ImagePNG($im);
ImageDestroy($im);
// Let’s end this section first.
?>
The next effect will be great! I am also learning now and selling now. PHP 4.0.6 and above adds this usage - you can draw lines in alternate colors! An example is as follows:
<?
Header("Content-type: image/png");
$im = ImageCreate (200, 100);
$col_black = ImageColorAllocate($im, 0,0,0);
$col_orn = ImageColorAllocate($im, 255,192,0);
$col_red = ImageColorAllocate($im, 255,0,0);
$style=array($col_red,$col_red,$col_black,$col_orn,$col_orn,$col_orn,$col_black);
ImageSetStyle($im, $style);
ImageLine($im, 0, 50, 199, 50, IMG_COLOR_STYLED);
ImagePNG($im);
ImageDestroy($im);
?>
Look at the effect.
Please explain the three lines I separated with blank lines. An array $style is defined, whose members are a series of colors;
Then a function was executed, and then IMG_COLOR_STYLED "color" was used to draw such a magical "straight line"——
Red, black, orange alternating effect. If you look carefully, you will find that the alternating sequence of red, black, and orange is what we define
The sequence of $style array members: red, red, black, orange, orange, orange, black, and then the cycle starts again...
Do you understand? Note that this function is only supported after PHP 4.0.6.
With the basics of line drawing that I explained in detail, I want to write the functions for drawing other geometric figures in one go. What I need to remind everyone is that no matter what kind of geometric figure you draw, it is nothing more than grasping several elements of this figure. Not counting color first, the elements of various graphics are as follows:
point, two elements: abscissa and ordinate
rectangle, four elements: abscissa and ordinate
arc in the upper left corner and lower right corner. Understand this way: arc can include arc, Elliptical arc; draw an arc and draw 360 degrees to form a circle; draw an elliptical arc and draw 360 degrees to form a circle.
It forms an ellipse; therefore, there are six elements of this arc: the horizontal and vertical coordinates of the center point, the length of the horizontal axis, the length of the vertical axis, and the beginning and end points of the arc.
Look at the following example.
<?
Header("Content-type: image/png");
$im = ImageCreate (200, 100);
$col_blk = ImageColorAllocate($im, 0,0,0);
$col_orn = ImageColorAllocate($im, 255,192,0);
$col_red = ImageColorAllocate($im, 255,0,0);
$col_grn = ImageColorAllocate($im, 0,255,0);
$col_blu = ImageColorAllocate($im, 0,0,255);
ImageSetPixel($im,20,10,$col_orn);
// It’s a small dot, I wonder if it can be seen?
ImageRectangle($im,25,20,95,55,$col_blu);
// Blue rectangle.
ImageArc($im,20,85,50,40,225,360,$col_grn);
// Green elliptical arc, center at (20,85), horizontal axis 50, vertical axis 40, 225 degrees to 360 degrees.
// It can be seen that the start and end points of the arc here are measured in angles.
// It is calculated clockwise with the horizontal right direction as 0 degrees.
ImageArc($im,160,60,40,40,0,360,$col_orn);
// Orange full circle. As long as the length of the horizontal axis is equal to the length of the vertical axis, it is a perfect circle.
// We all learned in high school: a circle is a special case of an ellipse!
//Finally draw another arc. Can the center of the circle be outside the image?
ImageArc($im,160,140,240,240,0,360,$col_red);
// Can!
ImagePNG($im);
ImageDestroy($im);
?>
Of course it is inevitable to paint a certain area in a certain color when drawing. GD has three coloring methods, one is rectangular area coloring,
One is to color the enclosed area where the specified point is located, and the other is to color the area surrounded by the specified color. Look at the following examples:
<?
Header("Content-type: image/png");
$im = ImageCreate (200, 100);
$col_blk = ImageColorAllocate($im, 0,0,0);
$col_orn = ImageColorAllocate($im, 255,192,0);
$col_yel = ImageColorAllocate($im, 255,255,0);
$col_red = ImageColorAllocate($im, 255,0,0);
$col_grn = ImageColorAllocate($im, 0,255,0);
$col_blu = ImageColorAllocate($im, 0,0,255);
ImageFilledRectangle($im,20,10,100,50,$col_blu);
ImageFilledRectangle($im,5,40,50,90,$col_red);
ImageFilledRectangle($im,40,80,100,95,$col_orn);
ImageFilledRectangle($im,90,35,110,90,$col_yel);
//The above is the first coloring. Draw the rectangle directly.
// I deliberately surrounded a small area with four rectangles of different colors.
// Used to illustrate the second coloring.
ImagePNG($im);
ImageDestroy($im);
// Take a look at the effect.
?>
Then:
<?
Header("Content-type: image/png");
$im = ImageCreate (200, 100);
$col_blk = ImageColorAllocate($im, 0,0,0);
$col_orn = ImageColorAllocate($im, 255,192,0);
$col_yel = ImageColorAllocate($im, 255,255,0);
$col_red = ImageColorAllocate($im, 255,0,0);
$col_grn = ImageColorAllocate($im, 0,255,0);
$col_blu = ImageColorAllocate($im, 0,0,255);
ImageFilledRectangle($im,20,10,100,50,$col_blu);
ImageFilledRectangle($im,5,40,50,90,$col_red);
ImageFilledRectangle($im,40,80,100,95,$col_orn);
ImageFilledRectangle($im,90,35,110,90,$col_yel);
//The above is the first coloring. Draw the rectangle directly.
// I deliberately surrounded a small area with four rectangles of different colors.
> // Used to illustrate the second coloring.
ImageFill($im,70,70,$col_grn);
// This is the second coloring.
ImageRectangle($im,120,40,190,90,$col_grn);
// Let's draw a rectangle to make the frame. In fact, any shape of border can be used as a frame.
ImageFilltoBorder($im,130,50,$col_grn,$col_orn);
// Paint the green rectangle orange.
// As long as the specified point is within the scope of this "box", it has nothing to do with the position of the point in the area.
// This function actually works like this:
//Start from the specified point and look outward for the boundary of the specified color. If found, stop.
// If you can't find it, paint the passing points with the required color.
ImagePNG($im);
ImageDestroy($im);
// Take a look at the effect.
// Now the picture we made is colorful, but in the browser, on the picture,
// Right click->Properties: Only 214 bytes!
?>
Let’s stop here this time.