Before you start
This tutorial is for PHP programmers interested in dynamically generating 3-D graphics. Images can be created from scratch, built up from objects and light sources positioned in space according to X, Y, and Z coordinates. Programmers with experience using 3-D Studio Max will see how to import objects from 3DS files into PHP creations. Complex surfaces defined by parametric equations can easily be mapped using Image_3D. Finally, business applications and data reports can be enhanced by adding pie charts generated on the fly.
Image_3D is an object-oriented interface for creating 3-D images. Objects and lights are positioned in a 3-D space according to X, Y, and Z coordinates. Images are then rendered into 2-D space and can be stored as PNG, SVG, or output to the shell (for use on ANSI shells). The package can be used to easily generate a handful of simple 3-D objects, including cubes, cones, spheres, text, and pie graphs. In addition, there is support for importing and modifying objects created in 3-D Studio Max. Developers with a strong understanding of 3-D spaces can take advantage of the custom polygram and surface maps to create some interesting objects.
The first half of this tutorial will demonstrate how to use command-line PHP scripts to generate 3-D image files. After setting up a basic 3-D space with colors and lights, each unique 3-D object type will be investigated, as well as each of the output file formats. The second half will discuss how to turn these basic examples into practical applications. Generating 3-D images is a processor-intensive task, so you will identify a solution for this problem in order to take dynamically generated images to the Web, without crashing your Web servers. The Image_3D package supports only static image formats, so you will build a simple JavaScript solution for animating the 3-D spaces. Finally, you will write a PHP class for displaying data reports in colorful pie charts that can be integrated with business applications.
The following software and tools are required to follow along:
- PHP V5
- The Image_3D package is written using the PHP V5 object and class syntax.
- Image_3D
- The PEAR package will need to be installed. Typically, the installation of Image_3D should be easy if you have root (or administrator) access to your machine. Because Image_3D is in alpha release, the PEAR installation may complain that the package is not "stable." Use the
-foption to force the installation:
pear install -f Image_3D |
- GD
- This graphics library is required to output PNG files, though other file types can be generated from Image_3D in the absence of GD. As of PHP V4.3, a version of the GD library comes bundled with new PHP installations. So there's a good chance you won't need to worry about recompiling PHP. You can use
phpinfo()to see if your current PHP installation is GD-enabled.
- Web server
- Examples in the second half of the tutorial can be built for use on a Web site. The Apache's open source Web server and IBM HTTP Server are two options, if you don't already have a Web server available.
- SVG viewer
- SVG files can be viewed in Mozilla Firefox V1.5, which includes native SVG support. Internet Explorer users can add SVG support to the browser by downloading the Adobe SVG plug-in.
This tutorial assumes at least a base knowledge of objects and classes, as the entire interface for Image_3D is object-oriented. Access to a Linux® shell account or a Windows® command prompt is required for executing command-line PHP examples shown in the first half of the tutorial. Basic experience with JavaScript is also assumed.





