PHP using imageline and XOR

I am trying to use the GD image library to draw lines using the XOR filter. I could not find a simple way to do this, so that the line was drawn "flipped" from white to black and vice versa. Any solutions?

+4
source share
1 answer

I am sure it is not possible to build an XOR string with the PHP imageline built-in function. Although you can do it yourself using imagesetpixel and a custom line drawing algorithm. For example, something like this might work (Bresenham Line Algorythm for PHP):

 function line($im,$x1,$y1,$x2,$y2) { $deltax=abs($x2-$x1); $deltay=abs($y2-$y1); if ($deltax>$deltay) { $numpixels=$deltax+1; $d=(2*$deltay)-$deltax; $dinc1=$deltay << 1; $dinc2=($deltay-$deltax) << 1; $xinc1=1; $xinc2=1; $yinc1=0; $yinc2=1; } else { $numpixels=$deltay+1; $d=(2*$deltax)-$deltay; $dinc1=$deltax << 1; $dinc2=($deltax-$deltay)<<1; $xinc1=0; $xinc2=1; $yinc1=1; $yinc2=1; } if ($x1>$x2) { $xinc1=-$xinc1; $xinc2=-$xinc2; } if ($y1>$y2) { $yinc1=-$yinc1; $yinc2=-$yinc2; } $x=$x1; $y=$y1; for ($i=0;$i<$numpixels;$i++) { $color_current = imagecolorat ( $im, $x, $y ); $r = ($color_current >> 16) & 0xFF; $g = ($color_current >> 8) & 0xFF; $b = $color_current & 0xFF; $color = imagecolorallocate($im, 255 - $r, 255 - $g, 255 - $b); imagesetpixel($im,$x,$y,$color); if ($d<0) { $d+=$dinc1; $x+=$xinc1; $y+=$yinc1; } else { $d+=$dinc2; $x+=$xinc2; $y+=$yinc2; } } return ; } 

The function works great for images created from files.

+1
source

Source: https://habr.com/ru/post/1337699/


All Articles