Hướng dẫn php imageline

[PHP 4, PHP 5, PHP 7, PHP 8]

imagelineDraw a line

Description

imageline[
    GdImage $image,
    int $x1,
    int $y1,
    int $x2,
    int $y2,
    int $color
]: bool

Parameters

image

A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor[].

x1

x-coordinate for first point.

y1

y-coordinate for first point.

x2

x-coordinate for second point.

y2

y-coordinate for second point.

color

The line color. A color identifier created with imagecolorallocate[].

Return Values

Returns true on success or false on failure.

Changelog

VersionDescription
8.0.0 image expects a GdImage instance now; previously, a resource was expected.

Examples

Example #1 Drawing a thick line

See Also

  • imagecreatetruecolor[] - Create a new true color image
  • imagecolorallocate[] - Allocate a color for an image

pb_2001 at haefft dot de

16 years ago

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.

Jonathan W.

15 years ago

I've modified the previous entry for drawing on a polar coordinate system to better represent angles based on a 360º whole circle bearing.

d [AT] sprid [DOT] de

17 years ago

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 ;]

kramesch_NOSPAM_ at _nospam_idsolutions dot at

20 years ago

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;                
    }
  }

Nils

10 years ago

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]

sbm007 at gmail dot com

12 years ago

Here is a analog clock representation of the system time along with digits for hours and little dots for minutes/seconds:

nanobot at chipx86 dot com

19 years ago

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 ]

Tyron

17 years ago

// 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++ < $wdt] {
    // Draw a line and move it down and make it lighter to get the gradient effect
    ImageLine[$img, $x1-$i*$dx, $y1+$i*$dy, $x2-$i*$dx, $y2+$i*$dy, ImageColorAllocate[$img, $i*$drgb, 0, 0]];
    ImageLine[$img, $x1-$i*$dx+1, $y1+$i*$dy, $x2-$i*$dx+1, $y2+$i*$dy, ImageColorAllocate[$img, $i*$drgb, 0, 0]];

  }
}

yl at sota dot ch

18 years ago

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"];

mueller at inf dot ufsc dot br

18 years ago

an algorithm to draw a bezier spline

v dot krypton at yandex dot ru

6 years ago

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];
}

teixeira dot cmo at gmail.com

5 years ago

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];

Anonymous

11 years ago

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:

Anonymous

13 years ago

Here's another way of modifying likavcan's code to display filled arrows. This makes use of imagefilledpolygon[...] instead of recursive function calls.

huirong dot jin at gmail dot com

14 years ago

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!

Chủ Đề