PHP draws a straight line based on known points

Source: Internet
Author: User

Sometimes we need to draw a line for the hot area of the image. At this time, we need to use the GD library of PHP. The number of points in the hot zone is variable, and the image size is also variable. We can use the following method to generate the hot zone of the image.

<? Phpheader ("Content-type: image/jpeg"); $ width = 400; $ height = 300; $ image = imagecreate (400,300); $ white = imagecolorallocate ($ image, 0xf5, 0xf5, 0xf5); $ red = imagecolorallocate ($ image, 0xff, 0x00, 0x00); $ blue = imagecolorallocate ($ image, 204,255, 0 ); $ blue2 = imagecolorallocate ($ image, 0, 0, 0); $ line_string = "65.616350%, 6.142129%, 50.894733%, 6.142129%, 49.632880%, 12.764112%, 17.665940%, 12.764112%, 16.54 4293%, 6.142129%, 2.663912%, 6.142129%, 2.663912%, 29.558995%, 65.616350%, 29.558995% "; $ line_array = explode (", ", $ line_string); for ($ I = 0; $ I <count ($ line_array); $ I = $ I + 2) {if ($ I <= count ($ line_array)-4) {imageline ($ image, $ line_array [$ I]/100 * $ width, $ line_array [$ I + 1]/100 * $ height, $ line_array [$ I + 2]/100 * $ width, $ line_array [$ I + 3]/100 * $ height, $ blue);} else {imageline ($ image, $ line_array [$ I] /100 * $ width, $ line_array [$ I + 1]/100 * $ height, $ line_array [0]/100 * $ width, $ line_array [1]/100 * $ height, $ blue) ;}} imagejpeg ($ image); // imagejpeg ($ image, "test.jpg", 80 ); // Save the image. 80 is the image quality. // It is recommended to use ImagePNG () for output. In this way, the image quality is better and the file size is smaller.>
Imageline () function

Syntax: int imageline (int im, int x1, int y1, int x2, int y2, int col );

This function draws a solid line on the graph. Connect to x2 and y2 from x1 and y1, and the origin (0, 0) is the upper left corner of the graph. The col parameter is the solid color.

Example:

<? Php function imagelinethick ($ image, $ x1, $ y1, $ x2, $ y2, $ color, $ thick = 1) {/* When the following two rows only have the right-angle intersection of online segments, imagesetthickness ($ image, $ thick); return imageline ($ image, $ x1, $ y1, $ x2, $ y2, $ color); */if ($ thick = 1) {return imageline ($ image, $ x1, $ y1, $ x2, $ y2, $ color );} $ t = $ thick/2-0.5; if ($ x1 = $ x2 | $ y1 = $ y2) {return imagefilledrectangle ($ image, round (min ($ x1, $ X2)-$ t), round (min ($ y1, $ y2)-$ t), round (max ($ x1, $ x2) + $ t ), round (max ($ y1, $ y2) + $ t), $ color) ;}$ k = ($ y2-$ y1)/($ x2-$ x1 ); // y = kx + q $ a = $ t/sqrt (1 + pow ($ k, 2 )); $ points = array (round ($ x1-(1 + $ k) * $ a), round ($ y1 + (1-$ k) * $ ), round ($ x1-(1-$ k) * $ a), round ($ y1-(1 + $ k) * $ ), round ($ x2 + (1 + $ k) * $ a), round ($ Y2-(1-$ k) * $ a), round ($ x2 + (1-$ k) * $ a), round ($ y2 + (1 + $ k) * $ a),); imagefilledpolygon ($ image, $ points, 4, $ color); return imagepolygon ($ image, $ points, 4, $ color);}?>
Imagecolorallocate () function

Assign color to an image.

Imagecolorallocate () returns an identifier representing a color consisting of a given RGB component. The image parameter is the return value of the imagecreatetruecolor () function. Red, green, and blue are the required colors respectively. These parameters are integers from 0 to 255 or hexadecimal values 0x00 to 0xFF. Imagecolorallocate () must be called to create the color of each image used in the image.

The first call to imagecolorallocate () will fill in the background color.

<? Php $ im = imagecreatetruecolor ('example.jpg '); // set the background to Red $ background = imagecolorallocate ($ im, 255, 0, 0 ); // set some colors $ white = imagecolorallocate ($ im, 255,255,255); $ black = imagecolorallocate ($ im, 0, 0, 0 ); // hexadecimal mode $ white = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF); $ black = imagecolorallocate ($ im, 0x00, 0x00, 0x00);?>

This function is used to match the color of the image for other drawing functions. The parameter im represents the handle of the image. The red, green, and blue parameters are the three primary colors of color. The value ranges from 0 to 255.

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.