In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to define color, draw points, lines and rectangles in PHP. It is very detailed and has certain reference value. Friends who are interested must read it!
Drawing an image in PHP everything is based on the canvas from the previous article, create the canvas, and then draw the image on the canvas. When we think of images, we think of colors, so first of all, let's take a look at how we should define colors for images in PHP.
Image definition color
When we use PHP for image operation, what must be inseparable is the color setting, different colors outline this beautiful image. So how should we provide color to the image in PHP? At this point, we are going to use the functions imagecolorallocate () and imagecolorallocatealpha (). Next, let's take a look at how these two functions should be used.
Imagecolorallocate () function
The imagecolorallocate () function can assign colors to images. If you want to set multiple colors, you need to call this function multiple times. The syntax format of the function:
Imagecolorallocate (resource $image, int $red, int $green, int $blue)
Where $image represents the image to which the color needs to be set, this function returns an identifier indicating the color of the given RGB component. $red,$green and $blue are the red, green and blue components of the desired color, respectively. Values range from 0 to 255. or hexadecimal 0x00 to 0xFF.
Examples are as follows:
Output result:
Imagecolorallocatealpha () function
The imagecolorallocatealpha () function does the same thing as the imagecolorallocate () function, but there is an additional alpha for setting transparent parameters, and its syntax format is as follows:
Imagecolorallocatealpha (resource $image, int $red, int $green, int $blue, int $alpha)
Where the previous parameter is consistent with the parameter of the imagecolorallocate () function, $alphab represents the parameter of transparency, with values ranging from 0 to 127,0 for complete opacity, and 127 for complete transparency.
Examples are as follows:
Output result:
Therefore, it is possible to define colors on the image through the functions imagecolorallocate () and imagecolorallocatealpha (). At the same time, the image is not only made up of colors, but also needs to have points, lines and different shapes. Next, let's take a look at how to solve these problems.
Draw points and Lin
Drawing points and lines can be said to be the most basic operation of drawing images in PHP. Although they are very basic, they can be flexibly used to draw more complex images. We can draw a point in the canvas through the imagesetpixel () function, or we can set the color of the points. The syntax format of its function is as follows:
Imagesetpixel (resource $image, int $x, int $y, int $color)
Where $image represents the created canvas, $x and $y represent the coordinate point at ($x _ color), the color of this coordinate point is $canvas.
To draw a line segment, you can use the imageline () function, whose syntax format is as follows:
Imageline (resource $image, int $x1, int $y1, int $x2, int $y2, int $color)
Where, represents a line segment with the color $color from coordinates ($x1) to coordinates ($x2).
Next, we can give an example through a combination of loops and random numbers:
Output result:
Draw a rectangle
In PHP, if we want to draw a rectangle, we need to use the imagerectangle () or imagefilledrectangle () function. The imagefilledrectangle () function fills the rectangle after it has been drawn, but the imagerectangle () function does not. Their grammatical format is as follows:
Imagerectangle (resource $image, int $x1, int $y1, int $x2, int $y2, int $color) imagefilledrectangle (resource $image, int $x1, int $y1, int $x2, int $y2, int $color)
Of which, both represent the drawing of a rectangle with upper left corner coordinates ($x1dy1) and lower right corner coordinates ($x2jiny2). The difference between the two is: the color after the imagerectangle () function represents the color of the rectangle edges, and the color after the imagefilledrectangle () function represents the filling color inside the rectangle.
Next, through the example, draw a rectangle through the imagerectangle () or imagefilledrectangle () function, as shown below:
Output result:
These are all the contents of the article "how to define colors, draw points, lines and rectangles in PHP". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.