Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP using imageline and XOR

I am trying to use the image GD library to draw lines using an XOR filter. I have not been able to find an easy way to do this so a line being drawn "flips" white to black and vice-versus. Any solutions?

like image 692
Talvi Watia Avatar asked Nov 06 '22 03:11

Talvi Watia


1 Answers

I'm pretty sure that it's not possible to draw the XOR line with built-in imageline PHP function. Though you can draw it yourself with imagesetpixel and custom line drawing algorithm. For example something like this can 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 ;
}

Function perfectly works for images, created from files.

like image 115
WASD42 Avatar answered Nov 12 '22 16:11

WASD42