Monday 11 June 2012

Working with graphics in php


we can also draw basic shapes like rectagle,square,circle...etc..and can do garphics much like all programming languages also in php. There exist a library for helping users in doing graphics.
Here iam giving you a sample of graphics work. here iam going to draw acircle and small circles just like what given in above image. like the atoms arranged in shell of an element.
so for doing this code is like this:-
<?php

header('Content-type:image/jpeg');//setting that the output is an i,age

$image = imagecreatetruecolor(500,500);// generating canvas for the image
$col1 = imagecolorallocate($image,255,255,255);//generating a colour
$col2 = imagecolorallocate($image,125,100,20);//generating a colour
imagefilledrectangle($image, 0,0, 500,500,$col1);//creatinga canvas rectangle
imagefilledrectangle($image, 50,50, 450,450,$col2);
imagearc( $image,220,220, 300,300, 0, 360, $col1);//creating the major circle
$f=6;//number of circles to generate
//$f=$_GET['number'];
if($f!=0){
$degree=(360/$f);// finding the unit degree so that circle comes in equal distance over the main circle
$d=ceil($degree);
$r=ceil($degree);//removing decimal places

for($i=0;$i<=360;$i++){// looping 360 degree

if($i==$d){// checking between current degree and calculated degree
$s=($i*3.14)/180;//converting degree to radian
 $x[$i]=220+(150*cos($s));//finding x cordiante of the point in circle

 $y[$i]=220+(150*sin($s));//finding y cordinate of point in circle

 imagearc($image,$x[$i],$y[$i], 50,50, 0, 360, $col1);//dawing the circle
 $d=$d+$r;//incrementing the angle
}

 }
}
imagejpeg($image);//outputting the image




?>

No comments:

Post a Comment