百科狗-知识改变命运!
--

imageline() - gd函数(图像处理)

乐乐12个月前 (11-21)阅读数 18#技术干货
文章标签线段

imageline()

imageline() - gd函数(图像处理)

(PHP 4, PHP 5, PHP 7)

画一条线段

说明

imageline (resource $image,int $x1,int $y1,int $x2,int $y2,int $color) : bool

imageline() 用$color颜色在图像$image中从坐标$x1,$y1到$x2,$y2(图像左上角为 0, 0)画一条线段。

画一条粗线

参见 imagecreatetruecolor() 和 imagecolorallocate()。

This is a function to make a dotted line. It accepts (it actually requires) 7 parameters and returns 1 if everything went OK and 0 if there was a problem.
int imagelinedotted ( resource im, int x1, int y1, int x2, int y2, int dist, int col )
imagelinedotted() draws a line from x1, y1 to x2, y2 (top left is 0, 0) in image im of colour col where dist defines the distance (measured in pixels) between one dot and another. 
I've modified the previous entry for drawing on a polar coordinate system to better represent angles based on a 360º whole circle bearing. 
Here my function do clear all problems. With this, you can draw firstly smooth lines (basic code adapted from code_couturier at graffiti dot net, with some performance changes). The special is, you can define the alpha-value of the line (0 = normal smooth line, 127 = fully transparent). Change whatever you want to make it better, but post your results ;) 
Here is a simple code to draw a line with an arbitrary stroke. The parameter aStroke is treated as a cyclic boolean array where true equals "set a point"
e.g. $aDotStroke = array(true,false);
function ImageStrokeLine($im,$x1,$y1,$x2,$y2,$farbe, $aStroke)
 {
  $deltax = abs($x2 - $x1);    
  $deltay = abs($y2 - $y1);    
  $x = $x1;            
  $y = $y1;            
  
  if ($x2 >= $x1)         
  {
   $xinc1 = 1;
   $xinc2 = 1;
  }
  else             
  {
   $xinc1 = -1;
   $xinc2 = -1;
  }
  
  if ($y2 >= $y1)         
  {
   $yinc1 = 1;
   $yinc2 = 1;
  }
  else             
  {
   $yinc1 = -1;
   $yinc2 = -1;
  }
  
  if ($deltax >= $deltay) 
  {
   $xinc1 = 0;         
   $yinc2 = 0;         
   $den = $deltax;
   $num = $deltax / 2;
   $numadd = $deltay;
   $numpixels = $deltax; 
  }
  else             
  {
   $xinc2 = 0;       
   $yinc1 = 0;       
   $den = $deltay;
   $num = $deltay / 2;
   $numadd = $deltax;
   $numpixels = $deltay; 
  }
  
  for ($curpixel = 0; $curpixel = count($aStroke))
   {
    $iStrokeCount = 0;
   }
 
   if ($aStroke[$iStrokeCount++]) 
   {
    ImageSetPixel($im,$x, $y,$farbe);      
   }      
   $num += $numadd;      
   if ($num >= $den)       
   {
    $num -= $den;        
    $x += $xinc1;        
    $y += $yinc1;        
   }
   $x += $xinc2;         
   $y += $yinc2;         
  }
 }
A quick function using imageline that I wrote so i could specify a starting point, angle and length of vector.
Thought other people might find this useful.

For this script angles work in a anti-clockwise direction (modify + and - in function to change start of 0 degrees and also direction of angle calculated)
Here is a analog clock representation of the system time along with digits for hours and little dots for minutes/seconds: 
Here is a function for making antialiased lines:
 function imagesmoothline ( $image , $x1 , $y1 , $x2 , $y2 , $color )
 {
 $colors = imagecolorsforindex ( $image , $color );
 if ( $x1 == $x2 )
 {
  imageline ( $image , $x1 , $y1 , $x2 , $y2 , $color ); // Vertical line
 }
 else
 {
  $m = ( $y2 - $y1 ) / ( $x2 - $x1 );
  $b = $y1 - $m * $x1;
  if ( abs ( $m ) 
// Here's a function for drawing a rotated gradient Rectangle (based on a previous note)
// Create An Image 255x255
$img = ImageCreateTrueColor(255, 255);
GradientRect($img,50,50,80,80,30);
ImagePng($img,"test.png");
ImageDestroy($img);
echo "
"; function GradientRect($img, $x1, $y1, $x2, $y2, $wdt) { $alpha = atan2($y2-$y1,$x2-$x1); $real_wdt = $wdt*sin($alpha); $real_hgt = $wdt*cos($alpha); echo "real wdt:".$real_wdt; echo "
real hgt:".$real_hgt; echo "
angle: ".($angle*180/pi()); $plotD = 0; $i=0; $dy = $real_hgt/$wdt; $dx = $real_wdt/$wdt; $drgb= 256/$wdt; while($i++
Simple function to create border for jpg-images:
function createImageBorder($imgName){
   $img   = substr($imgName, 0, -4); // remove fileExtension
   $ext   = ".jpg";
   $quality = 95;
   $borderColor = 255; // 255 = white
   
  /*
   a             b
   +-------------------------+
   |             
   |     IMAGE      
   |             
   +-------------------------+
   c             d  
  */
  
  $scr_img = imagecreatefromjpeg($img.$ext);
  $width  = imagesx($scr_img);
  $height = imagesy($scr_img);
       
    // line a - b
    $abX = 0;
    $abY = 0;
    $abX1 = $width;
    $abY1 = 0;
    
    // line a - c
    $acX = 0;
    $acY = 0;
    $acX1 = 0;
    $acY1 = $height;
    
    // line b - d
    $bdX = $width-1;
    $bdY = 0;
    $bdX1 = $width-1;
    $bdY1 = $height;
    
    // line c - d
    $cdX = 0;
    $cdY = $height-1;
    $cdX1 = $width;
    $cdY1 = $height-1;
      
    // DRAW LINES  
    imageline($scr_img,$abX,$abY,$abX1,$abY1,$borderColor);
    imageline($scr_img,$acX,$acY,$acX1,$acY1,$borderColor);
    imageline($scr_img,$bdX,$bdY,$bdX1,$bdY1,$borderColor);
    imageline($scr_img,$cdX,$cdY,$cdX1,$cdY1,$borderColor);
    
   // create copy from image  
    imagejpeg($scr_img, $img."_border.jpg", $quality);
    imagedestroy($scr_img); 
 }
  
  createImageBorder("myfile.jpg");
an algorithm to draw a bezier spline 
This function draw border to image.
function imageborder($img,$color) {
  $width = imagesx($img);
  $height = imagesy($img);
  ImageLine($img, 0, 0, $width, 0, $color);
  ImageLine($img, 0, 0, 0, $height, $color);
  ImageLine($img, $width-1, 0, $height-1, $height, $color);
  ImageLine($img, 0, $height-1, $width, $height-1, $color);
}
If you draw on a canvas, using js ctx.lineJoin = "round"; and then try to do the same on a GD $img the result will not be the same. 
Here is a simple code to draw a line similar to one made using lineJoin round.
$w=5; //set your line thickness;
imagesetthickness($img, $w);
imageline($img, $x1, $y1, $x2, $y2, $color);
imagesetthickness($img, 1);
imagefilledellipse ( $img , $x2 , $y2 , $w ,$w, $color);
The most bold-line-functions i found have problems with lines in a certian direction (they draw smaller lines with some angles). To do a real bold line just use this function: 
An example to draw Amplitude Modulation curve: y = c * sin (x/a) * sin (x/b) . You can easily modify the codes to create your own oscilloscope application!

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)