Draw a line using GD library

Draw a line using GD library

To use GD image functions first you need to check whether the
GD library is installed in you server , you can test it using phpinfo()
where GD support is enabled or not, If not you can enable it by changing
the php.ini value by removing the “;” before “extension=php_gd2.dll”.

To draw a line first we need to create a an image.
image can be created using the syntax imagecreate()

$im = @ImageCreate (100, 100)

this will create an image with width 100px and height 100px
from the top left.

the now we can draw the line using
the following syntax

bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )

as

imageline ($im,$x1,$y1,$x2,$y2,$color);

where $im is image resource created by imagecreate(),
$x1 ,$y1,$x2 ,$y2 are co-ordinates of the line

and
$color is the color identifier.

It can be

$color = ImageColorAllocate ($im, 255, 255, 255); /// for white
$color = ImageColorAllocate ($im, 0, 0, 0); //// for black

The Next step is outputting the image
if it should be outputted as PNG image we can use imagePng()

imagePNG($im);

noting should be outputted before using imagePng

Now we can write the whole process as

<?php

$im = @ImageCreate (100, 100)
$color = ImageColorAllocate ($im, 0, 0, 0); //// for black
$x1= 2;
$y1 = 2;
$x2 = 50;
$y2 = 50;
imageline ($im,$x1,$y1,$x2,$y2,$color);
imagePNG($im);

?>

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.