1. Computing

GD Library - The Basics of Drawing with PHP

By , About.com Guide

6 of 7

Arcs & Pies
 <?
 header('Content-type: image/png');
 $handle = imagecreate(100, 100);
 $background = imagecolorallocate($handle, 255, 255, 255);
 $red = imagecolorallocate($handle, 255, 0, 0);
 $green = imagecolorallocate($handle, 0, 255, 0);
 $blue = imagecolorallocate($handle, 0, 0, 255);
 imagefilledarc($handle, 50, 50, 100, 50, 0, 90, $red, IMG_ARC_PIE);
 imagefilledarc($handle, 50, 50, 100, 50, 90, 225 , $blue, IMG_ARC_PIE);
 imagefilledarc($handle, 50, 50, 100, 50, 225, 360 , $green, IMG_ARC_PIE);
 imagepng($handle);
 ?> 
Using imagefilledarc we can create a pie, or a slice. The parameters are: handle, center X & Y, width, height, start, end, color, and type. The start and end points are in degrees, starting from the 3 o'clock position.

The types are:

  1. IMG_ARC_PIE- Filled arch
  2. IMG_ARC_CHORD- filled with straight edge
  3. IMG_ARC_NOFILL- when added as a parameter, makes it unfilled
  4. IMG_ARC_EDGED- Connects to center. You will use this with nofill to make an unfilled pie.

We can lay a second arc underneath to create a 3D effect like shown in our example above. We just need to add this code in under the colors and before the first filled arc.

 $darkred = imagecolorallocate($handle, 0x90, 0x00, 0x00);
 $darkblue = imagecolorallocate($handle, 0, 0, 150);

// 3D look for ($i = 60; $i > 50; $i--) { imagefilledarc($handle, 50, $i, 100, 50, 0, 90, $darkred, IMG_ARC_PIE); imagefilledarc($handle, 50, $i, 100, 50, 90, 360 , $darkblue, IMG_ARC_PIE); }

  1. About.com
  2. Computing
  3. PHP / MySQL
  4. PHP Functions
  5. GD Library - PHP GD Library - GD Image Library

©2013 About.com. All rights reserved.