Home arrow PHP arrow Page 2 - Drawing More Complex Shapes with the GD Library in PHP

Displaying filled images and arcs - PHP

The GD library that comes bundled with PHP 4 and PHP 5 provides web developers with a robust set of intuitive functions for generating and handling dynamic graphics with minor hassles. If you're interested in mastering the numerous features integrated with this PHP extension, this series of instructive tutorials will make the whole learning experience a truly painless process.

TABLE OF CONTENTS:
  1. Drawing More Complex Shapes with the GD Library in PHP
  2. Displaying filled images and arcs
  3. Drawing filled polygons with the GD library
  4. Retrieving information about a given image stream
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
August 22, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated in the beginning of this series, the GD extension is useful for creating dynamic image streams from scratch, and offers a remarkable capacity for preprocessing existing graphics. Now we are going to see how well it fills up different types of basic shapes before displaying them on the browser.

This is the purpose of the brand new "imagefill()" and "imagefilledarc()" functions. As their names suggest, they perform a filling operation on a given image stream. The function called "imagefill()" performs a flood fill on a specified image; its use is shown in the code sample below: 

// example of 'imagefill()' function

try{
   if(!$img=imagecreatetruecolor(300,200)){
     throw new Exception('Error creating image');
   }
   // allocate blue color
   $blue=imagecolorallocate($img,0,0,255);
   imagefill($img,0,0,$blue);
   header("Content-type: image/gif");
   imagegif($img);

}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

As you can see, the "imagefill()" function is really handy for flood filling a specified image stream with a predefined foreground color. It starts this operation at the X, Y coordinates, which are naturally specified as part of the corresponding input parameters.

So far, so good, right? Now that you hopefully learned how the "imagefill()" GD function works, let me show you another example. In this case, the "imagefilledarc()" is used, not surprisingly, to fill in a basic arc with a specific foreground color.

That being explained, here's the corresponding code sample that shows a simple implementation for this function:

// example of 'imagefilledarc()' function try{
   if(!$img=imagecreatetruecolor(300,200)){
     throw new Exception('Error creating image');
   }
   // allocate blue color
   $blue=imagecolorallocate($img,0,0,255);
   // draw filled arc
   imagefilledarc($img,50,50,100,50,0,90,$blue,IMG_ARC_PIE);
   header("Content-type: image/gif");
   imagegif($img);

}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

In this case, the "imagefilledarc()" function that comes bundled with the GD library is used to display a filled arc with a predetermined foreground color on the browser. Also, the referenced function takes the starting and ending coordinates of the arc as its input parameters, along with the image stream used to display the filled shape.

Given that, the image created by the previous hands-on example is similar to the one shown below:

Certainly, after you analyze in detail the previous practical example, you'll agree with me that drawing and flood filling basic shapes by using the specific functions provided by the GD extension is truly a no-brainer process that can be performed with minor difficulties. Thus, having covered these two handy functions of this library, it's time to explore others that are also part of the library.

In the following section I'll teach you how to use a few additional functions that come packaged with the GD extension to display some other filled shapes on the browser, like basic polygons.

Want to see how this will be achieved? Click on the link that appears below and keep reading.



 
 
>>> 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: