PHP draws a straight line based on known points _php tutorial

Source: Internet
Author: User
Tags imagejpeg
Sometimes we need to draw a line to the hot area of the image, and we are going to use the PHP GD library. The number of hot areas is variable, the size of the picture is also variable, we can use the following method to generate a picture of the hot zone.

<?phpheader ("Content-type:image/jpeg"); $width = n; $height = +; $image = imagecreate (+); $white = Imagecolorallocate ($image, 0xf5, 0xf5, 0xf5); $red = Imagecolorallocate ($image, 0xFF, 0x00, 0x00); $blue = Imagecolorallocate ($image, 204, 255, 0); $blue 2 = imagecolorallocate ($image, 0, 0, 0); $line _string = "65.616350%,6.142129 %,50.894733%,6.142129%,49.632880%,12.764112%,17.665940%,12.764112%,16.544293%,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]/$width, $line _array[$i +1]/100 * $ Height, $line _array[$i +2]/$width, $line _array[$i +3]/$height, $blue);} Else{imageline ($image, $line _array[$i]/$width, $line _array[$i +1]/$height, $line _array[0]/$ * $width, $ LINE_ARRAY[1]/+ * $height, $blue);}} Imagejpeg ($image);//imagejpeg ($image, "test.jpg", 80);//Save the picture. 80 for picture qualityVolume//Recommended with imagepng () output, so the picture quality is better, the file size is also smaller?> 

Imageline () function

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

This function will draw a solid line on the graph. Connect from x1, y1 to X2, Y2, and origin (0,0) to the upper-left corner of the graph. Parameter col is the color of the solid line.

Reference Example:

<?php function Imagelinethick ($image, $x 1, $y 1, $x 2, $y 2, $color, $thick = 1) {/* The following two lines only intersect at right angles of the segment so IM     Agesetthickness ($image, $thick);     Return Imageline ($image, $x 1, $y 1, $x 2, $y 2, $color);     */if ($thick = = 1) {return imageline ($image, $x 1, $y 1, $x 2, $y 2, $color);     } $t = $thick/2-0.5;  if ($x 1 = = $x 2 | | $y 1 = = $y 2) {return Imagefilledrectangle ($image, round (min ($x 1, $x 2)-$t), round (     Min ($y 1, $y 2)-$t), round (Max ($x 1, $x 2) + $t), round (Max ($y 1, $y 2) + $t), $color); } $k = ($y 2-$y 1)/($x 2-$x 1);      y = kx + Q $a = $t/sqrt (1 + POW ($k, 2)); $points = Array (round ($x 1-(1 + $k) * $a), round ($y 1 + (1-$k) * $a), round ($x 1-(1-$k          ) * $a), round ($y 1-(1 + $k) * $a), round ($x 2 + (1 + $k) * $a), round ($y 2-(1-$k) * $a),  Round ($x 2 + (1-$k) * $a), round ($y 2 + (1 + $k) * $a),   );     Imagefilledpolygon ($image, $points, 4, $color); Return Imagepolygon ($image, $points, 4, $color); }?>

Imagecolorallocate () function

Assigns a color to an image.

Imagecolorallocate () returns an identifier that represents a color made up of a given RGB component. The image parameter is the return value of the Imagecreatetruecolor () function. Red, green, and blue are the colors they need to be the color of the Reds, greens, and blue components. These parameters are integers from 0 to 255 or hexadecimal 0x00 to 0xFF. Imagecolorallocate () must be called to create each color used in the image represented by image.

The first call to Imagecolorallocate () fills the background color.

<?php $im = Imagecreatetruecolor (' example.jpg '); The background is set to red $background = Imagecolorallocate ($im, 255, 0, 0); Set some color $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 graphic for use by other drawing functions. The parameter IM represents the handle of the graph. Parameters red, green, blue are the primary colors of the color, with values 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.