1. Home
  2. Computing & Technology
  3. PHP / MySQL

GD Library - The Basics of Drawing with PHP

By , About.com Guide

6 of 7

Arcs & Pies

Image made with GD Library

Our code creates this image

<?
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);
}

Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP Functions
  5. GD Library - PHP GD Library - GD Image Library

©2009 About.com, a part of The New York Times Company.

All rights reserved.