Installing GD

Just in case you missed installing the GD library when you were setting up PHP, I’ll quickly go over it again. I will also show you a great little chunk of code that you can use to load the library dynamically.

To install GD on a Linux or UNIX installation you will need to recompile PHP with the --with-gd option. You can also compile the GD library as a shared object so you can load the GD library dynamically whenever you need it. To do this you would use the --with-gd=shared option. Then, to load in the library dynamically at run time, you would use the following line of code:

dl(‘gd.so’);

To install GD on a Windows platform you need to copy the php_gd2.dll that came with the installation package into the extensions directory specified in the php.ini file. Then you will need to edit the php.ini file and uncomment the following line:

extension=php_gd2.dll

This will enable access to the GD library. If you would like to load the extension dynamically with script you may do so with the following line of code:

dl(‘php_gd2.dll’);

NOTE

In order to dynamically load an extension, the shared object file (UNIX) or the dll file (Windows) must be in the extensions directory specified in the php.ini file. If it is not, then the loading of the extension will fail and none of the functions will be available to your game.

Let’s say you don’t know if the server you are hosting your files on is a UNIX or a Windows server. You can create a dynamically loading script that will work on either platform very easily. Just take a look at the following code:

<?php
if(!extension_loaded(‘gd’))
{
    if(strtoupper(substr(PHP_OS, 3)) == “WIN”)
    {
        dl(‘php_gd2.dll’);
    }
    else
    {
        dl(‘gd.so’);
    }
}
?>

This handy little chunk of code will work for any extension—this example just happens to be for the GD library. The first step is to see if the extension is already loaded. If the extension is loaded then why would you want to do anything else? If, however, the extension is not loaded then you will need to determine what platform you are on and load the proper file. To determine what OS the PHP script is living on you will need to use the PHP_OS constant. This returns the information about the current OS. If you are on a Windows platform the first three characters of the string will be “WIN”. If you do not find the string “WIN” in the PHP_OS constant it is safe to assume that you are on a UNIX platform. Once you know what platform you are on you can dynamically load the extension that you want by using the dl() function.

Now that you have the extension properly loaded it is time to test to see if it is actually there. The easiest way to do this is to use the phpinfo() function. Remember when you used this in Chapter 2? You will need to run that same test script again and look for a section in the page that looks like Figure 8.1.

Figure 8.1. Results of the phpinfo() function.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset