Home >Backend Development >PHP Tutorial >PHP draws a straight line based on known points_PHP tutorial

PHP draws a straight line based on known points_PHP tutorial

不言
不言Original
2018-05-25 15:43:331617browse

Sometimes we need to draw lines for hot spots in images, then we have to use PHP’s GD library. The number of points in the hot zone is variable, and the size of the image is also variable. We can use the following method to generate the hot zone of the image.

<?php
header("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.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] / 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);	//保存图片.80为图片质量
//推荐用ImagePNG()输出,这样图片质量要好些,文件大小也小些
?>

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, with the origin (0,0) being the upper left corner of the graph. The parameter col is the color of the solid line.

Reference example:

<?php 
function imagelinethick ( $image , $x1 , $y1 , $x2 , $y2 , $color , $thick = 1 ) 
{ 
     /* 下面两行只在线段直角相交时好使 
    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 )* $a ), 
         round ( $x1 - ( 1 - $k )* $a ), round ( $y1 - ( 1 + $k )* $a ), 
         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

assigns a color to an image.

imagecolorallocate() Returns an identifier representing a color consisting of the given RGB components. The image parameter is the return value of the imagecreatetruecolor() function. red , green and blue are the red, green and blue components of the desired color respectively. 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() will fill the background color.

<?php 
$im = imagecreatetruecolor ( &#39;example.jpg&#39; ); 
// 背景设为红色 
$background = imagecolorallocate ( $im , 255 , 0 , 0 ); 
// 设定一些颜色 
$white = imagecolorallocate ( $im , 255 , 255 , 255 ); 
$black = imagecolorallocate ( $im , 0 , 0 , 0 ); 
// 十六进制方式 
$white = imagecolorallocate ( $im , 0xFF , 0xFF , 0xFF ); 
$black = imagecolorallocate ( $im , 0x00 , 0x00 , 0x00 ); 
?>

This function is used to match the color of graphics and is used by other drawing functions. The parameter im represents the handle of the graphic. The parameters red, green, and blue are the three primary colors, and their values ​​range from 0 to 255.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn