php 画直线,PHP根据已知点来画直线

有时候我们需要给图片的热区画线,这时候我们就要使用PHP的GD库了。热区的点数是不定的,图片的大小也是不定的,我们可以使用下面的方法生成图片的热区。

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()函数

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

本函数将在图形上画出一条实线。从 x1、y1 连接到 x2、y2,原点 (0,0) 为图形的左上角。参数 col 为实线的颜色。

参考例子:

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()函数

为一幅图像分配颜色。

imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。 image 参数是 imagecreatetruecolor() 函数的返回值。 red , green 和 blue 分别是所需要的颜色的红,绿,蓝成分。这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。 imagecolorallocate() 必须被调用以创建每一种用在 image 所代表的图像中的颜色。

第一次对 imagecolorallocate() 的调用会填充背景色。

$im = imagecreatetruecolor ( 'example.jpg' );

// 背景设为红色

$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 );

?>

本函数用来匹配图形的颜色,供其它绘图函数使用。参数 im 表示图形的 handle。参数 red、green、blue 是色彩三原色,其值从 0 至 255。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值