Talking about PHP drawing (ii.)

Source: Internet
Author: User
Tags array header range
Last said a simple way to avoid the GD, and then use GD to make the simplest of a "picture"-Straight line.
This time I'm going to draw straight down and say. The part that was explained in detail in the last code, this time no longer repeat.

?
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);
So we drew a dotted line.

Now let's do a test. Used to illustrate a problem.
$col _yel = imagecolorallocate ($im, 255,255,0);
Yellow.
Imageline ($im, 0,99,199,99, $col _yel);
At the bottom of the image, a yellow line was drawn along the way.
Imageline ($im, 200,0,200,100, $col _orn);
Try to draw a clear line along the right side of the image, and there is nothing.
This indicates that the width of the 200, 100 of the image, its coordinate range is (0,0) to (199,99).

Imagepng ($im);
Imagedestroy ($im);
Let's end this section first.
?>


The next effect is cool! I am also now learning to sell. PHP4.0.6 above adds to this usage--you can use alternating
Color Painting line! Examples are 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, 199, img_color_styled);

Imagepng ($im);
Imagedestroy ($im);
?>

Let's see the effect.

One of the three lines that I split with the empty line explains. Defines an array of $style whose members are a series of colors;
Then execute a function, and then use img_color_styled "color" to draw out is such a magical "line"--
The effect of alternating red, black, and orange. Take a closer look and you'll find that the sequence of alternating red, black and orange is what we define
$style sequence of members of the array: red, red, black, orange, orange, orange, black, and then cycle ...
Do you understand me? Note that this function is supported after PHP4.0.6.



With the basics of the lines that I've explained in detail, I want to make a generation of the functions that draw other geometries. Need to be prompted by everyone is that whatever
What kind of geometry you draw is nothing more than a few elements of grasping this figure. Not the color, the elements of various graphics are as follows:

Point, two elements: horizontal axis, ordinate

Rectangle, four elements: the upper left corner, the lower right corner of the horizontal, vertical

Arcs, so understood: arcs can include arcs, elliptical arcs; he can make a circle with a 360-degree arc, draw an elliptical arc and draw him 360 degrees.
into an ellipse; so the element of this arc is six: the center point horizontal, ordinate, the horizontal axis is long, the longitudinal axis is long, the arc's beginning, the end point.

Look at the example below.

?
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);
A small point, do not know whether can see?
Imagerectangle ($im, 25,20,95,55, $col _blu);
a blue rectangle.
Imagearc ($im, 20,85,50,40,225,360, $col _grn);
The green elliptical arc, the center in (20,85), the horizontal axis 50, the longitudinal axis 40,225 degrees to 360 degrees.
This shows that the beginning and end of the arc here is measured by the angle,
is 0 degrees in the horizontal direction to the right, and is measured clockwise.
Imagearc ($im, 160,60,40,40,0,360, $col _orn);
The whole round of orange. As long as the axis length and longitudinal axis looks, is a positive circle.
We have learned in high school: Circle is a special case of ellipse!
Finally, draw a circular arc. Can the center of the circle be outside the image?
Imagearc ($im, 160,140,240,240,0,360, $col _red);
OK!

Imagepng ($im);
Imagedestroy ($im);
?>

Drawing of course, it is inevitable to paint an area into a certain color. GD has three coloring ways, one is rectangular area coloring,
One is the enclosing area coloring of the specified point, and the other is the area shaded 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. Draws a rectangle directly.
I deliberately surround a small area with four different colors of rectangles,
Used to illustrate the second coloring.

Imagepng ($im);
Imagedestroy ($im);

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. Draws a rectangle directly.
I deliberately surround a small area with four different colors of rectangles,
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 a box. In fact, any boundary can be framed.
Imagefilltoborder ($im, 130,50, $col _grn, $col _orn);
Colour the green rectangular frame in orange.
As long as the specified point is within the range of this "box", it has nothing to do with the point's position within the area.
This function actually works like this:
Starts from the specified point, outward, looks for the boundary of the specified color, and if found, stops,
If you can't find it, apply the dots to the desired color.

Imagepng ($im);
Imagedestroy ($im);

Look at the effect.
Now we have made the picture is colorful, but in the browser, the picture,
Right--> property: only 214 bytes!

?>

This time, let's talk about it.



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.