Here’s a fast, effective way of drawing a Bezier Curve in PHP with no cumbersome libraries or extensions.
<?php
$im = imagecreate(500, 500);
function quadBezier($im, $x1, $y1, $x2, $y2, $x3, $y3, $color) {
$b = $pre1 = $pre2 = $pre3 = 0;
$prevx = 0;
$prevy = 0;
$d = sqrt(($x1 - $x2) * ($x1 - $x2) + ($y1 - $y2) * ($y1 - $y2)) +
sqrt(($x2 - $x3) * ($x2 - $x3) + ($y2 - $y3) * ($y2 - $y3));
$resolution = (1/$d) * 10;
for ($a = 1; $a >0; $a-=$resolution) {
$b=1-$a;
$pre1=($a*$a);
$pre2=2*$a*$b;
$pre3=($b*$b);
$x = $pre1*$x1 + $pre2*$x2 + $pre3*$x3;
$y = $pre1*$y1 + $pre2*$y2 + $pre3*$y3;
if ($prevx != 0 && $prevy != 0)
imageline ($im, $x, $y, $prevx,$prevy, $color);
$prevx = $x;
$prevy = $y;
}
imageline ($im, $prevx, $prevy, $x3, $y3, $color);
}
$bg = imagecolorallocate($im, 255, 255, 255);
$color = imagecolorallocate($im, 0, 0, 0);
for ($i = 0; $i<20; $i++){
quadBezier($im, 40,100, 150 , 20 + $i * 10 , 500, 100,$color);
}
header("Content-type: image/jpeg");
imagejpeg($im,"",100);
?>
Tags: PHP