|
|
 |
imageline (PHP 3, PHP 4, PHP 5) imageline -- Draw a line Descriptionint imageline ( resource image, int x1, int y1, int x2, int y2, int color )
imageline() draws a line from
x1, y1 to
x2, y2 (top left is
0, 0) in image image of color color.
Example 1. Drawing a thick line |
<?php
function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
{
if ($thick == 1) {
return imageline($image, $x1, $y1, $x2, $y2, $color);
}
$t = $thick / 2 - 0.5;
if ($x1 == $x2 || $y1 == $y2) {
return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
}
$k = ($y2 - $y1) / ($x2 - $x1); $a = $t / sqrt(1 + pow($k, 2));
$points = array(
round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
);
imagefilledpolygon($image, $points, 4, $color);
return imagepolygon($image, $points, 4, $color);
}
?>
|
|
See also imagecreate() and
imagecolorallocate().
User Contributed Notes
imageline
Tyron
02-May-2005 01:50
// 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 "<br><img src=\"test.png\">";
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 "<br>real hgt:".$real_hgt;
echo "<br>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));
}
}
ajreading at classixshop dot com
23-Apr-2005 09:28
<?php
$img = ImageCreateTrueColor(255, 255);
$plotD = 0;
while($plotD < 256)
{
ImageLine($img, 0, $plotD , 255, $plotD, ImageColorAllocate($img, $plotD, $plotD, $plotD));
$plotD++;
}
Header("Content-type: image/png");
ImagePng($img);
ImageDestroy($img);
?>
eviloverlord at gmail dot com
27-Jan-2005 09:50
This code is used to draw a board of hexagons (for games, classes, etc.)
<?php
$maxTiles = 7; $minTiles = 4; $side = 30; $bgColor = array(0, 0, 0); $fgColor = array(255, 255, 255);$widthInTiles = range($maxTiles, $minTiles); $rowsInTiles = count($widthInTiles)*2-1; $xSide = $side*sin(deg2rad(60)); $ySide = $side*sin(deg2rad(30)); $boardWidth = $xSide*$widthInTiles[0]*2; $boardHeight = $rowsInTiles*($side + $ySide) + $ySide; $image = imagecreate($boardWidth, $boardHeight);
$bg = imagecolorallocate($image, $bgColor[0], $bgColor[1], $bgColor[2]);
$fg = imagecolorallocate($image, $fgColor[0], $fgColor[1], $fgColor[2]);
$row = 0;
foreach($widthInTiles as $tiles)
{
for ($i = 0; $i < $tiles+1; $i++)
{
$x1 = $row*$xSide + $i*$xSide*2;
$y1 = $boardHeight/2;
$y1Dif = ($side/2) + $row*($side+$ySide);
$x2 = $x1 + $xSide;
$y2 = $y1;
$y2Dif = $ySide;
$x3 = $x2 + $xSide;
if ($i < $tiles)
{
imageline($image, $x1, $y1 - $y1Dif, $x2, $y2 - $y1Dif - $y2Dif, $fg);
imageline($image, $x1, $y1 + $y1Dif, $x2, $y2 + $y1Dif + $y2Dif, $fg);
imageline($image, $x2, $y2 - $y1Dif - $y2Dif, $x3, $y1 - $y1Dif, $fg);
imageline($image, $x2, $y2 + $y1Dif + $y2Dif, $x3, $y1 + $y1Dif, $fg);
}
imageline($image, $x1, $y1 - $y1Dif, $x1, $y1 - $y1Dif + $side, $fg);
imageline($image, $x1, $y1 + $y1Dif, $x1, $y1 + $y1Dif - $side, $fg);
}
$row++;
}
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
likavcan at NOSPAN sturak nospan dot sk
05-Jun-2004 05:56
This function draws arrow.
function arrow($im, $x1, $y1, $x2, $y2, $alength, $awidth, $color) {
$distance = sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2));
$dx = $x2 + ($x1 - $x2) * $alength / $distance;
$dy = $y2 + ($y1 - $y2) * $alength / $distance;
$k = $awidth / $alength;
$x2o = $x2 - $dx;
$y2o = $dy - $y2;
$x3 = $y2o * $k + $dx;
$y3 = $x2o * $k + $dy;
$x4 = $dx - $y2o * $k;
$y4 = $dy - $x2o * $k;
imageline($im, $x1, $y1, $dx, $dy, $color);
imageline($im, $x3, $y3, $x4, $y4, $color);
imageline($im, $x3, $y3, $x2, $y2, $color);
imageline($im, $x2, $y2, $x4, $y4, $color);
}
Lionel Van Bemten
22-May-2004 05:41
here is a code to draw a "degraded" ... :
<?
header("Content-type : image/jpeg");
$image = @ImageCreate(200, 100)
or die ("Erreur de création de l'image");
$lignes_colorees = 1;
$couleur_fond = ImageColorAllocate($image, 255, 255, 255);
$rouge_depart = 100;
$vert_depart = 255;
$bleu_depart = 0;
$rouge_fin = 0;
$vert_fin = 100;
$bleu_fin = 0;
$lignes = 100; $vert_diff = $vert_fin - $vert_depart;
$vert1 = $vert_diff / $lignes;
$bleu_diff = $bleu_fin - $bleu_depart;
$bleu1 = $bleu_diff / $lignes;
$rouge_diff = $rouge_fin - $rouge_depart;
$rouge1 = $rouge_diff / $lignes;
while ($lignes_colorees <= 100)
{
$rouge2 = $lignes_colorees * $rouge1; $rouge3 = $rouge_depart + $rouge2;
$rouge = round($rouge3);
$vert2 = $lignes_colorees * $vert1;
$vert3 = $vert_depart + $vert2;
$vert = round($vert2);
$bleu2 = $lignes_colorees * $bleu1;
$bleu3 = $bleu_depart + $bleu2;
$bleu = round($bleu2);
$y1 = $lignes_colorees;
$y2 = $lignes_colorees;
$x1 = 1; $x2 = 200;
$couleur = ImageColorAllocate($image, $rouge, $vert, $bleu);
ImageLine($image, $x1, $y1, $x2, $y2, $couleur); $lignes_colorees ++;
}
ImageJpeg($image);
?>
ruturaj_v at yahoo dot com
14-May-2004 10:48
here is a function that helps you create arrows...
<?php
function get_arrowheads ($x1, $y1, $x2, $y2, $arrhead, $arrang) {
$debug = false;
define("INFINITE", 'INFINITE');
if (($x2-$x1)==0) {
if ($y1 == 0) {
$slope = 0;
} else {
$slope = INFINITE;
}
} else {
$slope = -($y2-$y1)/($x2-$x1);
}
if ($slope == 'INFINITE') {
$ang = 90;
} else {
$ang = atan ($slope);
$ang = ($ang * 180)/pi();
}
$arrang1 = ($ang - $arrang);
$arrangdeg1 = ($ang - $arrang);
$arrang1 = ($arrang1*pi())/180;
$arrang2 = ($ang + $arrang);
$arrangdeg2 = ($ang + $arrang);
$arrang2 = ($arrang2*pi())/180;
$arx1 = (floor(cos($arrang1)*$arrhead));
$ary1 = (floor(sin($arrang1)*$arrhead));
$arx2 = (floor(cos($arrang2)*$arrhead));
$ary2 = (floor(sin($arrang2)*$arrhead));
if ($debug) {
echo ("Values of arx1.. before add/sub</br>");
echo ("$arx1,$ary1 $arx2,$ary2</br>");
}
if ($ang==0) {
if ($x2 > $x1) {
$arx1 = $x2 - $arx1; $ary1 = $y2 - $ary1;
$arx2 = $x2 - $arx2; $ary2 = $y2 - $ary2;
} elseif ($x2 < $x1) {
$arx1 = $x2 + $arx1; $ary1 = $y2 - $ary1;
$arx2 = $x2 + $arx2; $ary2 = $y2 - $ary2;
}
}
if ($ang > 0 && $ang < 90) {
if (($x2 > $x1) && ($y2 < $y1)) {
$arx1 = $x2 - $arx1; $ary1 = $y2 + $ary1;
$arx2 = $x2 - $arx2; $ary2 = $y2 + $ary2;
} elseif (($x2 < $x1) && ($y2 > $y1)) {
$arx1 = $x2 + $arx1; $ary1 = $y2 - $ary1;
$arx2 = $x2 + $arx2; $ary2 = $y2 - $ary2;
}
}
if ($ang==90) {
if (($y2 > $y1)) {
$arx1 = $x2 - $arx1; $ary1 = $y2 - $ary1;
$arx2 = $x2 - $arx2; $ary2 = $y2 - $ary2;
} elseif (($y2 < $y1)) {
$arx1 = $x2 - $arx1; $ary1 = $y2 + $ary1;
$arx2 = $x2 - $arx2; $ary2 = $y2 + $ary2;
}
}
if ($ang > -90 && $ang < 0) {
if (($x2 > $x1) && ($y2 > $y1)) {
$arx1 = $x2 - $arx1; $ary1 = $y2 + $ary1;
$arx2 = $x2 - $arx2; $ary2 = $y2 + $ary2;
} elseif (($x2 < $x1) && ($y2 < $y1)) {
$arx1 = $x2 + $arx1; $ary1 = $y2 - $ary1;
$arx2 = $x2 + $arx2; $ary2 = $y2 - $ary2;
}
}
if ($debug) {
echo ("Angle of line is (".$ang*180/pi().")</br>");
echo ("Angle of line1 is $arrangdeg1</br>");
echo ("Angle of line2 is $arrangdeg2</br>");
echo ("$arx1,$ary1 $x2,$y2</br>");
echo ("$arx2,$ary2 $x2,$y2");
exit;
}
$array_arrows = array (
'x1' =>$arx1,
'y1' => $ary1,
'x2' => $arx2,
'y2' => $ary2
);
return $array_arrows;
}
$x1 = 200; $y1 = 200;
$x2 = 400; $y2 = 100;
$arrhead = 15; $arrang = 10; $ar_arrws = get_arrowheads ($x1, $y1, $x2, $y2, $arrhead, $arrang);
$im = imagecreate (400, 400);
$w = imagecolorallocate ($im, 255, 255, 255);
$red = imagecolorallocate ($im, 255, 0, 0);
imageline ($im, $x1, $y1, $x2, $y2, $green);
imageline ($im, $x1+1, $x2+1, $y1+1, $y2+1, $red);
imageline ($im, $x2, $y2, $ar_arrws['x1'], $ar_arrws['y1'], $green);
imageline ($im, $x2, $y2, $ar_arrws['x2'], $ar_arrws['y2'], $green);
?>
yl at sota dot ch
14-Apr-2004 11:44
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");
XxXoldsaltXxX at hotmail dot com
14-Feb-2004 12:50
Script to draw a grid of lines, which make the entire side of the picture look like a quarter circle.. very cool :)
<?php
$img_disp = imagecreate(1000,1000);
$backcolor = imagecolorallocate($img_disp,0,0,0);
imagefill($img_disp,0,0,$backcolor);
$textcolor = imagecolorallocate($img_disp,255,0,0);
$x1 = 0;
$y1 = 0;
$x2 = 0;
$y2 = 1000;
for(;;){
$y1 = $y1 + 20;
$x2 = $x2 + 20;
imageline($img_disp,$x1,$y1,$x2,$y2,$textcolor);
if ($y1 == 1000){
break;
}
}
header("Content-type: image/png");
imagepng($img_disp); imagedestroy($img_disp); ?>
mueller at inf dot ufsc dot br
05-Jan-2004 02:26
an algorithm to draw a bezier spline
<?php
$segmentos =30;
$x=array(0,10,80,30);
$y=array(0,10,40,50);
function bezier($p,$steps){
$t = 1 / $steps;
$temp = $t * $t;
$ret = array();
$f = $p[0];
$fd = 3 * ($p[1] - $p[0]) * $t;
$fdd_per_2=3*($p[0]-2*$p[1]+$p[2])*$temp;
$fddd_per_2=3*(3*($p[1]-$p[2])+$p[3]-$p[0])*$temp*$t;
$fddd = $fddd_per_2 + $fddd_per_2;
$fdd = $fdd_per_2 + $fdd_per_2;
$fddd_per_6 = $fddd_per_2 * (1.0 / 3);
for ($loop=0; $loop<$steps; $loop++) {
array_push($ret,$f);
$f = $f + $fd + $fdd_per_2 + $fddd_per_6;
$fd = $fd + $fdd + $fddd_per_2;
$fdd = $fdd + $fddd;
$fdd_per_2 = $fdd_per_2 + $fddd_per_2;
}
return $ret;
}
$by = bezier($y,$segmentos);
$bx = bezier($x,$segmentos);
header ("Content-type: image/jpeg");
$tam = 200;
$im = imagecreate($tam,$tam);
$background_color = imagecolorallocate ($im, 200, 200, 200);
$tc = imagecolorallocate ($im, 233, 14, 91);
for($i=0;$i<$segmentos-1;$i++)
imageline($im,$bx[$i],$tam-$by[$i],$bx[$i+1],$tam-$by[$i+1],$tc);
imagejpeg($im);
imagedestroy($im);
?>
code_couturier at graffiti dot net
18-Oct-2003 01:56
# antialiased draw_line function 1.1 (faster)
# here is a drawLine() posted by nanobot at chipx86 dot com
# on php.net and enhanced/optimized by myself :)
# here are some changes i made:
# 1. changed for true-color images (no index_var used)
# 2. changed rgb extraction to logical shift
# 3. reducing function call's
function drawQSLine ($image,$x1,$y1,$x2,$y2,$r,$g,$b) {
$icr=$r;
$icg=$g;
$icb=$b;
$dcol = ImageColorAllocate ($image,$icr,$icg,$icb);
if ($y1 == $y2) imageline ($image,$x1,$y1,$x2,$y1,$dcol);
else if ($x1 == $x2) {
imageline ($image,$x1,$y1,$x1,$y2,$dcol);
} else {
$m = ($y2 - $y1) / ($x2 - $x1);
$b = $y1 - $m * $x1;
if (abs ($m) <2) {
$x = min ($x1,$x2);
$endx = max ($x1,$x2)+1;
while ($x < $endx) {
$y=$m * $x + $b;
$y == floor ($y) ? $ya = 1 : $ya = $y - floor ($y);
$yb = ceil ($y) - $y;
$trgb = ImageColorAt($image,$x,floor($y));
$tcr = ($trgb >> 16) & 0xFF;
$tcg = ($trgb >> 8) & 0xFF;
$tcb = $trgb & 0xFF;
imagesetpixel ($image,$x,floor ($y),imagecolorallocate ($image,($tcr * $ya + $icr * $yb),
($tcg * $ya + $icg * $yb),
($tcb * $ya + $icb * $yb)));
$trgb = ImageColorAt($image,$x,ceil($y));
$tcr = ($trgb >> 16) & 0xFF;
$tcg = ($trgb >> 8) & 0xFF;
$tcb = $trgb & 0xFF;
imagesetpixel ($image,$x,ceil ($y),imagecolorallocate ($image,($tcr * $yb + $icr * $ya),
($tcg * $yb + $icg * $ya),
($tcb * $yb + $icb * $ya)));
$x ++;
} # while_x_end
} # if_end
else { # else_abs_end
$y = min ($y1,$y2);
$endy = max ($y1,$y2)+1;
while ($y < $endy) {
$x=($y - $b) / $m;
$x == floor ($x) ? $xa = 1 : $xa = $x - floor ($x);
$xb = ceil ($x) - $x;
$trgb = ImageColorAt($image,floor($x),$y);
$tcr = ($trgb >> 16) & 0xFF;
$tcg = ($trgb >> 8) & 0xFF;
$tcb = $trgb & 0xFF;
imagesetpixel ($image,floor ($x),$y,imagecolorallocate ($image,($tcr * $xa + $icr * $xb),
($tcg * $xa + $icg * $xb),
($tcb * $xa + $icb * $xb)));
$trgb = ImageColorAt($image,ceil($x),$y);
$tcr = ($trgb >> 16) & 0xFF;
$tcg = ($trgb >> 8) & 0xFF;
$tcb = $trgb & 0xFF;
imagesetpixel ($image,ceil ($x),$y,imagecolorallocate ($image, ($tcr * $xb + $icr * $xa),
($tcg * $xb + $icg * $xa),
($tcb * $xb + $icb * $xa)));
$y ++;
}# while_y_end
}# else_abs_end
}# else_y=y_x=x_end
}# drawOSLine_end
code_couturier at graffiti dot net
01-Oct-2003 11:33
# antialiased lines with true-type-fonts !?
# yip, it works - not very good - but faster
# (below 3 sec) than other routines (~7 sec)
# i saw here.
# the antialiased effect is minimal and only
# at the top of the line - but, as better as
# nothing ;)
# here is a drawLine routine from Logan Gunthorpe
# (logang at deltatee dot com) posted by nanobot at chipx86 dot com
# on php.net and enhanced/optimized by myself :)
function drawSoftLine ($image, $x1, $y1, $x2, $y2, $r,$g,$b) {
$fontfile="arial.ttf"; # the font for the dot
$str="·" ; # the point_char for the dot
$size=12; # the point for the dot_size
$color = ImageColorAllocate ($image, $r,$g,$b);
if ($x2 == $x1) {
$tmp = $x1; $x1 = $y1; $y1 = $tmp;
$tmp = $x2; $x2 = $y2; $y2 = $tmp;
$swapx = true;
}
$m = (float)(( $y2 - $y1 ) / ( $x2 - $x1 ));
$b = (float)($y1 - $m*$x1);
$strx = min ( $x1 , $x2 );
$endx = max ( $x1 , $x2 );
$x=(int)0;
# the original loop was based on one for_loop
# with the if_swap inside. to speed up this loop
# i double it, to keep off the permanent if_call's.
if (!$swapx) {
for ($x = $strx; $x <= $endx; $x++) {
$y = (int)($m*$x + $b);
imagettftext($image, $size, 0, $x, $y,
$color, $fontfile,$str);
}
} else {
for ($x = $strx; $x <= $endx; $x++) {
$y = (int)($m*$x + $b);
imagettftext($image, $size, 0, $y, $x,
$color, $fontfile,$str);
}
}
}
nanobot at chipx86 dot com
17-Jun-2003 07:28
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 ) <= 1 )
{
$x = min ( $x1 , $x2 );
$endx = max ( $x1 , $x2 );
while ( $x <= $endx )
{
$y = $m * $x + $b;
$y == floor ( $y ) ? $ya = 1 : $ya = $y - floor ( $y );
$yb = ceil ( $y ) - $y;
$tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , floor ( $y ) ) );
$tempcolors['red'] = $tempcolors['red'] * $ya + $colors['red'] * $yb;
$tempcolors['green'] = $tempcolors['green'] * $ya + $colors['green'] * $yb;
$tempcolors['blue'] = $tempcolors['blue'] * $ya + $colors['blue'] * $yb;
if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
imagesetpixel ( $image , $x , floor ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
$tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , ceil ( $y ) ) );
$tempcolors['red'] = $tempcolors['red'] * $yb + $colors['red'] * $ya;
$tempcolors['green'] = $tempcolors['green'] * $yb + $colors['green'] * $ya;
$tempcolors['blue'] = $tempcolors['blue'] * $yb + $colors['blue'] * $ya;
if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
imagesetpixel ( $image , $x , ceil ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
$x ++;
}
}
else
{
$y = min ( $y1 , $y2 );
$endy = max ( $y1 , $y2 );
while ( $y <= $endy )
{
$x = ( $y - $b ) / $m;
$x == floor ( $x ) ? $xa = 1 : $xa = $x - floor ( $x );
$xb = ceil ( $x ) - $x;
$tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , floor ( $x ) , $y ) );
$tempcolors['red'] = $tempcolors['red'] * $xa + $colors['red'] * $xb;
$tempcolors['green'] = $tempcolors['green'] * $xa + $colors['green'] * $xb;
$tempcolors['blue'] = $tempcolors['blue'] * $xa + $colors['blue'] * $xb;
if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
imagesetpixel ( $image , floor ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
$tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , ceil ( $x ) , $y ) );
$tempcolors['red'] = $tempcolors['red'] * $xb + $colors['red'] * $xa;
$tempcolors['green'] = $tempcolors['green'] * $xb + $colors['green'] * $xa;
$tempcolors['blue'] = $tempcolors['blue'] * $xb + $colors['blue'] * $xa;
if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
imagesetpixel ( $image , ceil ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
$y ++;
}
}
}
}
EDITOR: My previous code contained bugs. Please use this one instead.
kramesch_NOSPAM_ at _nospam_idsolutions dot at
26-Feb-2002 03:44
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 <= $numpixels; $curpixel++)
{
if ($iStrokeCount >= 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;
}
}
darren at php4hosting dot com
28-Oct-2000 12:48
It does work (With a bit of editing)
save the following as graph.php (You dont need any directorys)
<?php
Header("Content-type: image/png");
$picWidth=360*2;
$picHeight=200;
$pic=ImageCreate($picWidth+1,$picHeight+1);
$cWhite=ImageColorAllocate($pic,255,255,255);
ImageFilledRectangle($pic,0,0,$picWidth+1,$picHeight+1,$cWhite);
$cRed=ImageColorAllocate($pic,255,0,0);
$cBlue=ImageColorAllocate($pic,0,0,255);
$curX=0;
$curY=$picHeight;
for($pt=0;$pt<$picWidth;$pt++){
$newX=$curX+1;
$newY=($picHeight/2)+(cos(deg2rad($newX))*($picHeight/2));
ImageLine($pic,$curX,$curY,$newX,$newY,$cRed);
$curX=$newX;
$curY=$newY;
}
$curX=0;
$curY=$picHeight/2;
for($pt=0;$pt<$picWidth;$pt++){
$newX=$curX+1;
$newY=($picHeight/2)+(sin(deg2rad($newX))*($picHeight/2));
ImageLine($pic,$curX,$curY,$newX,$newY,$cBlue);
$curX=$newX;
$curY=$newY;
}
$cBlack=ImageColorAllocate($pic,0,0,0);
ImageLine($pic,0,0,0,$picHeight,$cBlack);
ImageLine($pic,0,$picHeight/2,$picWidth,$picHeight/2,$cBlack);
ImagePNG($pic);
ImageDestroy($pic);
?>
| |