SQL PHP HTML ASP JavaScript articles and free scripts to download

Drawing graphic lines using PHP GD Support

If you have installed GD library then let us try to draw some lines in an image using gd support. We will open our file in a new window as we have to tell browser ( through header ) that we are sending image.

Using gd we will first learn how to draw line. We will learn some basic functions used in creating and managing images.

$im = @ImageCreate (100, 200)

The above line creates and image identifier of width 100 and height 200. The top left coordinate is set as 0, 0

$background_color = ImageColorAllocate ($im, 234, 234, 234);

Background color is set here for the image with the identifier $im. Here we have set it to a light gray background. The color of the background can be changed by changing the values of red, green, blue ( now all are set to 234 to create light gray background)

Same way let us set a text color for the image
$text_color = ImageColorAllocate ($im, 233, 14, 91);
 imageline ($im,$x1,$y1,$x2,$y2,$text_color);
The above line will show the line from where to where it is to be drawn. Staring from $x1,$x2 to $x2, $y2. Note that top left is at the coordinate 0,0. The lines below will give how the line will appear with different values of the coordinates used in imageline function.

Now with this image, let us draw

ImagePng ($im);

With the above knowledge, you can see that the command imageline ($im,$x1,$y1,$x2,$y2,$text_color) draws lines between two points defined with two coordinates. To learn the different images we get ( or different lines ) with the change in value of coordinates ( x and y ) we will list a set of values and corresponding image link. Click the link right of the coordinates to check what image will be drawn with the value given at left. All images will be opened in a new window.

imageline ($im,0,0,50,100,$text_color)Display
imageline ($im,0,0,100,100,$text_color) Display
imageline ($im,100,0,0,200,$text_color) Display
imageline ($im,0,0,100,200,$text_color) Display

With above example you can see how the coordinates affect the line inside the image. Now let us dray two lines in the same image one after the other.

imageline ($im,0,0,100,200,$text_color);
imageline ($im,100,0,0,200,$text_color);
Display


Above examples must have given you some idea on how to draw lines using gd support in PHP. You can use the forum if you have any doubt. Now here you can see the code we used to draw the lines in small window. Note that this code has two imageline functions to draw two lines. Change the data and see how the line changes.

<?php
header ("Content-type: image/png");
$x1=$_GET['x1'];
$x2=$_GET['x2'];
$y1=$_GET['y1'];
$y2=$_GET['y2'];

$im = @ImageCreate (100, 200)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 224, 234, 234);
$text_color = ImageColorAllocate ($im, 233, 14, 91);

// imageline ($im,$x1,$y1,$x2,$y2,$text_color); imageline ($im,0,0,100,200,$text_color); imageline ($im,100,0,0,200,$text_color);
ImagePng ($im); ?>
Save the above code in a file and open it without adding any html code or data to it. With this learning on how to draw lines we will move to our next learning on how to draw graphs by taking data from MySQL table.
Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer