imagefilledarc(): arc using PHP GD

<?Php
header ("Content-type: image/jpeg");
$width=300;$height=300;
$im = @ImageCreate ($width,$height) // added one pixel for border
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 255, 255, 255);
$filled_color = ImageColorAllocate ($im, 255, 0,0);
imagefilledarc($im,150,150,290,290,0,160,$filled_color,IMG_ARC_PIE);
Imagejpeg($im);
imagedestroy($im); //memory cleared. 
?>
filled are using imagefilledarc()
Syntax
imagefilledarc($image,$center_x,$center_y,$width,$height,
	$start_angle,$end_angle,$color,$style)
Returns true or false based on success of failure.
$imageImage created using ImageCreate() or imagecreatetruecolor()
$center_xx coordinate of center ( X : Horizontal )
$center_yy coordinate of center ( Y : Vertical )
$widthWidth of the arc
$heightHeight of the arc
$start_angle Angle of the start in degree. 3 Oclock position is 0 degree.
$end_angle Angle of the end of the arc in degree.
$colorColor to fill the arc by using imagecolorallocate().
$styleTake values : IMG_ARC_PIE,IMG_ARC_CHORD,IMG_ARC_NOFILL,IMG_ARC_EDGED.


Drawing filled arc, circles & patterns using colours and angles by imagefilledarc() in PHP GD


Creating full circle using imagefilledarc()

We need to maintain $width and $height equal , $start_angle should be 0 (degree ) and $end_angle should be 360 ( degree ). By using imagearc() we created one face, to this image now we will be adding eye balls by using imagefilledard().

Example

Face with eye balls using imagefilledarac
<?Php
header ("Content-type: image/jpeg");
$width=300;$height=300;
$im = @ImageCreate ($width,$height) // added one pixel for border
or die ("Cannot Initialize new GD image stream");

$background_color = ImageColorAllocate ($im, 255, 255, 255);
$text_color = ImageColorAllocate ($im, 255, 0,0);
imagearc($im,150,150,290,290,0,360,$text_color); // outer face
imagearc($im,80,100,50,50,0,360,$text_color); // left eye 
imagearc($im,220,100,50,50,0,360,$text_color); // right eye
imagearc($im,150,150,150,150,45,135,$text_color); // mouth

$text_color = ImageColorAllocate ($im, 128, 128,0);
imagefilledarc($im,80,100,20,20,0,360,$text_color,IMG_ARC_PIE); // left eye ball 
imagefilledarc($im,220,100,20,20,0,360,$text_color,IMG_ARC_PIE); // right eye ball 

Imagejpeg($im);
imagedestroy($im); //memory cleared. 
?>
By changing the position of eye ball we can create different type of face ( emoji )
Face with eye balls in UP direction
$move_left=0;
$move_top=15;
Face with eye balls in LEFT direction
$move_left=15;
$move_top=0;
$move_left=0; // x position is not changed 
$move_top=15; // Y position is moved up 
imagefilledarc($im,80-$move_left,100-$move_top,20,20,0,360,$text_color,IMG_ARC_PIE); // left eye ball 
imagefilledarc($im,220-$move_left,100-$move_top,20,20,0,360,$text_color,IMG_ARC_PIE); // right eye ball 
Try to create different movement of eye balls and mouth arc representing different face expressions ( emoji )

Concentric circle using imagefilledarc()

Random numbers are gnerated between 1 and 255 for adding differnt colors. Common center for all the circles.
Filled concentric Circles using imagefilledarc
<?Php
header ("Content-type: image/jpeg");
$width=301;$height=301;
$im = @ImageCreate ($width,$height) // added one pixel for border
or die ("Cannot Initialize new GD image stream");
$gap=20; // change this value to increase the lines 
$background_color = ImageColorAllocate ($im, 255, 255, 255);
imagesetthickness($im,1);
$text_color = ImageColorAllocate ($im, 255, 0,0);
for($i=0;$i<=$width;$i +=$gap){
$r=rand(1,255); // random numbers 
$g=rand(1,255);  
$b=rand(1,255);	
$text_color = ImageColorAllocate ($im, $r, $g,$b);		
imagefilledarc($im,$width/2,$height/2,($width)-$i,($height)-$i,0,360,$text_color,IMG_ARC_PIE);
}

Imagejpeg ($im);
imagedestroy($im); //memory is removed. 
?>
We can keep height and width of the arcs constant and change the starting and ending angles of the arcs. The $gap we will reduce here.
 Change in angles of Filled concentric Circles
imagefilledarc($im,$width/2,$height/2,$width,$height,$i,$i+$gap,$text_color,IMG_ARC_PIE);

How to create a Pie chart by using GD Library imagefilledarc()

Pie chart using GD in PHP

Creating dynamic pie charts in PHP is possible using the GD library. By defining color-coded sections and calculating the proportional angles, we can generate a pie chart image. This example demonstrates how to use `imagefilledarc()` to create a simple pie chart with different colors for each segment.


📊 How to Create a Dynamic Pie Chart in PHP Using GD Library | imagefilledarc() Explained

<?php
header ("Content-type: image/jpeg"); // Set content type to display image

$width = 300;
$height = 300;
$image = imagecreate($width, $height);

// Define colors
$white = imagecolorallocate($image, 255, 255, 255);
$colors = [
    imagecolorallocate($image, 255, 0, 0),   // Red
    imagecolorallocate($image, 0, 255, 0),   // Green
    imagecolorallocate($image, 0, 0, 255),   // Blue
    imagecolorallocate($image, 255, 255, 0)    // Yellow
];

// Sample data for the pie chart
$data = [10, 10, 25, 25]; 

$my_sum = array_sum($data); // Calculate sum of data values

$start_angle = 0; // Start angle for first slice
$center_x = $width / 2;
$center_y = $height / 2;
$radius = 100; // Radius of pie chart

// Loop through data and draw pie chart slices
foreach ($data as $index => $value) {
    $end_angle = $start_angle + ($value * 360 / $my_sum);
    
    imagefilledarc($image, $center_x, $center_y, 
        $radius * 2, $radius * 2, 
        $start_angle, $end_angle, $colors[$index], IMG_ARC_PIE);
    
    $start_angle = $end_angle; // Update start angle for next slice
}

// Output the image as PNG format
imagepng($image);
imagedestroy($image); // Free up memory
?>
DEMO : GD Pie chat by using imagefilledarc()

Code Explanation

  • Initializing the Canvas: We create an image with a specified width and height using imagecreate().
  • Defining Colors: Colors are assigned using imagecolorallocate(), with an array to store multiple segment colors.
  • Data Handling: The script calculates the total sum of values and determines the proportion of each slice.
  • Drawing the Pie Chart: Using a loop, each segment is drawn using imagefilledarc(), ensuring the correct start and end angles.
  • Outputting the Image: The generated pie chart is displayed using imagepng(), and memory is freed with imagedestroy().

This method is lightweight and efficient, making it ideal for generating reports, statistics, and visual data representations in PHP applications.

Inserting GD Pie chat in PDF documents by using FPDF

Including this graph in a web page

We can keep the above code in a page test1.php and use this page like this.
<img src="test1.php?time=<?php echo time(); ?>" alt="Dynamic Image">
More on displaying image on browser
GD imagefilledrectangle() to create Bar Chat
Adding Pie chat to PDF
GD functions GD imagearc() GD imagerectangle() imageellipse()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com











    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer