Home arrow PHP arrow Page 2 - Drawing Functions and the GD Library in PHP

Taking a look at the imagearc() function - PHP

If you're a PHP developer looking for an approachable guide on how to use the popular GD extension available with PHP 4 and PHP 5, then look no further, because this series might be what you need. Welcome to the third article of the series that began with "A Close Look at the GD Library in PHP." In this group of tutorials, you'll find distilled material on using the most important functions that come integrated with the GD library.

TABLE OF CONTENTS:
  1. Drawing Functions and the GD Library in PHP
  2. Taking a look at the imagearc() function
  3. Using the imagechar() and imagecharup() functions
  4. Using the imageellipse() and imagefilledellipse() functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
August 21, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In consonance with the concepts that I explained at the introduction of this article, the GD library comes well-equipped with a neat number of drawing functions. These functions are useful when utilized within a specific PHP application that requires the display of different types of basic shapes to users.

The GD extension can also help you create complex images, like 2D and 3D graphics, flow charts, and pie drawings, by using the appropriate set of functions provided by the library in question.

Please take a look at the following code sample, which demonstrates how to use the "imagearc()" function to display obviously different arcs. Here are the corresponding examples, so take some time and study them:

// example of 'imagearc()' function - draws a white arc

try{
   if(!$img=imagecreatetruecolor(300,200)){
     throw new Exception('Error creating image');
   }
   // allocate a basic color
   if(!$whiteColor=imagecolorallocate($img,255,255,255)){
     throw new Exception('Error allocating color');
   }
   // draw a white arc
   imagearc($img,100,100,100,100,0,360, $whiteColor);
   // display image to the browser
   header("Content-type: image/png");
   imagepng($img);
   // free memory
   imagedestroy($img);
           
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

As you can see, the above "imagearc()" GD function is tasked with displaying several types of arcs on a given image stream. It takes up a few intuitive input parameters, such the image stream resource to draw on, the respective start and end coordinates, and finally the arc inclination and its corresponding color.

The previous script will display on the browser a white-colored arc, similar to the one shown below:

Due to the great versatility exposed by this function, it's possible to create several types of arcs, with different colors. The following examples illustrate this concept very clearly, so have a look at them, please:

// example of 'imagearc()' function - draws a red arc

try{
   if(!$img=imagecreatetruecolor(300,200)){
     throw new Exception('Error creating image');
   }
   // allocate a basic color
   if(!$redColor=imagecolorallocate($img,255,0,0)){
     throw new Exception('Error allocating color');
   }
   // draw a red arc
   imagearc($img,100,100,100,100,0,360,$redColor);
   // display image to the browser
   header("Content-type: image/png");
   imagepng($img);
   // free memory
   imagedestroy($img);
          
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

// example of 'imagearc()' function - draws a green arc

try{
   if(!$img=imagecreatetruecolor(300,200)){
     throw new Exception('Error creating image');
   }
   // allocate some basic colors
   if(!$greenColor=imagecolorallocate($img,255,255,255)){
     throw new Exception('Error allocating color');
   }
   // draw a green arc
   imagearc($img,100,100,100,100,0,360,$greenColor);
   // display image to the browser
   header("Content-type: image/png");
   imagepng($img);
   // free memory
   imagedestroy($img);
          
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

Okay, at this point I believe that you now understand how to use the GB extension to draw a few basics arcs on a given image stream, thus it's time to move forward and continue exploring more functions that come integrated with this library.

And speaking of analyzing some additional functions bundled with the GD library, in the section to come I'll show you how to use two more functions to display some basic characters on a specified image stream.

To see how this will be achieved, jump ahead and read the next few lines. I'll be there, waiting for you.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: