Ars Informatica
August 19, 2017
Home
Health Care Informatics
Web-based MySQL/PHP Databasing
Programming
Web Development
Favourite Software
Hardware for the Frugal Fanatic
Graphic Design and Image Processing
Free Scripts and Software
About Us
Contact Us
Links
 
 

 Article Feed for this site

Checking for and installing the gd Graphics Library

February 12, 2008

As mentioned in our article Image creation and manipulation using the gd Graphics Library, the gd library is an open source code library (yes, it's free) for the dynamic creation and manipulation of images, i.e. the real-time generation of charts, graphics, thumbnails, etc. Note, though, that it's not as good as the equally free ImageMagick suite, which handles over 95 image formats, has many powerful image handling functions, and produces a much better, high-quality output.

So why use the gd library? Simply put, it may be all you have. It comes with PHP, and is often either enabled or can be enabled with minimal effort. If you cannot or aren't allowed to run ImageMagick (even with its PHP or PERL wrappers, MagickWand and PERLMagick), then gd may well give you what you need.

To check if gd library is installed, invoke:

print_r(gd_info());

If installed, you'll get the somewhat cryptic:

Array ( [GD Version] => bundled (2.0.28 compatible) [FreeType Support] => 1
[FreeType Linkage] => with freetype [T1Lib Support] => [GIF Read Support]
=> 1 [GIF Create Support] => 1 [JPG Support] => 1 [PNG Support] => 1
[WBMP Support] => 1 [XBM Support] => 1 [JIS-mapped Japanese Font Support] => )

More on what info is returned, and a very simple function for formatting this more legibly, below.

If not installed, this function call returns the following error message:

Call to undefined function: gd_info()

If you have PHP version 4.3 or greater, this simply means that you haven't got this library enabled. You'll need to uncomment line

;extension=php_gd2.dll

by removing the semi-colon at the start of this line in file php.ini. If this file doesn't already exist in the PHP directory, create it by copying php.ini-dist to php.ini and then making this change. Finally, make sure that the pointer to the directory that contains the gd2.dll extersion, extension_dir, is set correctly, i.e.

extension_dir = "./extensions"

Finally, if you're installing the gd library with older versions of PHP (though I do wonder why you'd want to do that), find it at the GD Library Main Page at http://www.libgd.org/.  

A nicer/neater gd_info function

The Ars Informatica version of the gd_info() function, function AI_gd_info():

function AI_gd_info() {
	if (function_exists('gd_info')) {
		$info = gd_info();
		foreach ($info as $k => $v) {
			if ($v == true AND $v == 1) $v = 'YES';
			else if ($v == false) $v = 'NO';
			echo $k.': '.$v.'<br>';
		}
	}
	else echo 'GD not supported';
}

produces the following output:

GD Version: bundled (2.0.28 compatible)
FreeType Support: YES
FreeType Linkage: with freetype
T1Lib Support: NO
GIF Read Support: YES
GIF Create Support: YES
JPG Support: YES
PNG Support: YES
WBMP Support: YES
XBM Support: YES
JIS-mapped Japanese Font Support: NO

Which is a little more legible, ain't it?

As with all the code I've published on this site, feel free to copy this code and use it to your heart's content. Though a link back to my site would be much appreciated.
 

See Also

Image creation and manipulation using the gd Graphics Library
Custom grayscale function for dynamic image conversion using PHP and the gd Graphics Library
 
Copyright © 2017 Ars Informatica. All Rights Reserved.