PHP根据已知点来画直线_PHP教程

不言
发布: 2018-05-25 15:43:33
原创
1831人浏览过

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

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

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

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

参考例子:

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

为一幅图像分配颜色。

立即学习PHP免费学习笔记(深入)”;

来画数字人直播
来画数字人直播

来画数字人自动化直播,无需请真人主播,即可实现24小时直播,无缝衔接各大直播平台。

来画数字人直播 0
查看详情 来画数字人直播

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

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

<?php 
$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。

相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号