PHP plot (2)

Source: Internet
Author: User

Last time I talked about a simple method of drawing GD, And then I used GD as the simplest "Graph"-a straight line.
This time I will draw a straight line down. I will not repeat the details in the previous code.

<?
Header ("Content-type: image/png ");
$ Im = ImageCreate (200,100 );
$ Col_black = ImageColorAllocate ($ im, 0, 0 );
$ Col_orn = ImageColorAllocate ($ im, 255,192, 0 );
// Use orange today.
// Use exactly the same as the imageline function,
ImageDashedLine ($ im, 0,100,199,100, $ col_orn );
// Draw a dotted line.

// Let's perform a test. To illustrate a problem.
$ Col_yel = ImageColorAllocate ($ im, 255,255, 0 );
// Yellow.
ImageLine ($ im, $ col_yel );
// Draw a yellow line at the bottom of the image.
ImageLine ($ im, 200,0, 200,100, $ col_orn );
// Try to draw a clear line at the far right of the image, and there is no result.
// This indicates that the coordinates of an image with a width of 200 and a height of 100 are () ).

ImagePNG ($ im );
ImageDestroy ($ im );
// This section ends first.
?>


Next, the effect will be great! I am also selling it now. PHP4.0.6 or above adds this usage-you can use the alternative
Draw lines in color! Example:

<?
Header ("Content-type: image/png ");
$ Im = ImageCreate (200,100 );
$ Col_black = ImageColorAllocate ($ im, 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_black );
ImageSetStyle ($ im, $ style );
ImageLine ($ im, 0, 50,199, 50, IMG_COLOR_STYLED );

ImagePNG ($ im );
ImageDestroy ($ im );
?>

Let's see the effect.

Here, I will explain the three lines separated by empty lines. Defines an array $ style, whose members are a series of colors;
Then a function is executed, and then IMG_COLOR_STYLED "color" is used to draw such a magical "Straight Line "--
Red, black, and orange. After a closer look, you will find that the red, black, and orange alternate order is what we define.
The sequence of members in the $ style array: Red, red, black, orange, and black ......
Do you understand? Note that this function is supported after PHP4.0.6.

 

I want to replace the functions that draw other geometric figures with the basics of drawing lines I have explained in detail. Note that
What kind of geometric image to draw is nothing more than grasping the elements of this image. Color is not counted first. The elements of various images are as follows:

Point, two elements: abscissa and ordinate

Rectangle. four elements: the horizontal and vertical coordinates in the upper left corner and lower right corner.

An arc can include an arc or an elliptical arc. When an arc is drawn at 360 degrees, it can be a circle, and an elliptical arc is drawn at 360 degrees.
The arc has six elements: the center point, the vertical coordinate, the horizontal axis length, the vertical axis length, the beginning and end points of the arc.

See the following example.

<?
Header ("Content-type: image/png ");
$ Im = ImageCreate (200,100 );
$ Col_blk = ImageColorAllocate ($ im, 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, 255 );

ImageSetPixel ($ im, 20, 10, $ col_orn );
// A Little Bit. Do you know if you can see it?
ImageRectangle ($ im, 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 starting and ending points of the arc here are measured in angle,
// It is calculated in clockwise direction from the horizontal to the right.
ImageArc ($ im, 160,360, 40, $ col_orn );
// Orange circle. As long as the horizontal axis is long and the vertical axis looks like, it is a positive circle.
// We learned in high school: the circle is a special case of an elliptical world!
// Draw an arc at the end. Can the center be outside the image?
ImageArc ($ im, 160,140,240,240, 0,360, $ col_red );
// Yes!

ImagePNG ($ im );
ImageDestroy ($ im );
?>

Of course, it is inevitable to paint a certain area into a certain color. GD has three coloring Methods: rectangular area coloring,
One is the closed area color of the specified vertex, and the other is the area color surrounded by the specified color. See the following example:
<?
Header ("Content-type: image/png ");
$ Im = ImageCreate (200,100 );
$ Col_blk = ImageColorAllocate ($ im, 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, 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 method. Draw a rectangle directly.
// I intentionally enclose a small area with four rectangles of different colors,
// Used to describe the second coloring.

ImagePNG ($ im );
ImageDestroy ($ im );

// Check the effect.

?>

Next:

<?
Header ("Content-type: image/png ");
$ Im = ImageCreate (200,100 );
$ Col_blk = ImageColorAllocate ($ im, 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, 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 method. Draw a rectangle directly.
// I intentionally enclose a small area with four rectangles of different colors,
> // Specifies the second coloring method.

ImageFill ($ im, 70, 70, $ col_grn );
// This is the second coloring method.

ImageRectangle ($ im, 120,40, 190,90, $ col_grn );
// Draw a rectangle for the moment. In fact, any boundary can be framed.
ImageFilltoBorder ($ im, 130,50, $ col_grn, $ col_orn );
// Set the green rectangle to orange.
// As long as the specified vertex is within the range of the box, it is irrelevant to the position of the vertex in the area.
// This function actually works like this:
// Start from the specified vertex and look outward for the boundary of the specified color. If it is found, it stops,
// If it cannot be found, the path points will be painted in the desired color.

ImagePNG ($ im );
ImageDestroy ($ im );

// Check the effect.
// The picture we made is already green, but in the browser, the picture,
// Right click-> attribute: only 214 bytes!

?>

Let's talk about it this time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.