note 52481 added to function.gd-info

From: Date: Tue, 03 May 2005 11:35:23 +0000
Subject: note 52481 added to function.gd-info
Groups: php.notes 
Request: Send a blank email to php-notes+get-88896@lists.php.net to get a copy of this message
This function safely determines the GD version, even on PHP versions earlier than 4.3 that lack the gd_info() function. Use it to avoid having your script halt with a fatal error if you try to use a TrueColor function and the GD version isn't 2.0 or later. You can optionally specify a version, but if you specify version 2 it might be overridden. Once the version number is determined it's retained to speed future calls. <?php /** * Get which version of GD is installed, if any. * * Returns the version (1 or 2) of the GD extension. */ function gdVersion($user_ver = 0) { if (! extension_loaded('gd')) { return; } static $gd_ver = 0; // Just accept the specified setting if it's 1. if ($user_ver == 1) { $gd_ver = 1; return 1; } // Use the static variable if function was called previously. if ($user_ver !=2 && $gd_ver > 0 ) { return $gd_ver; } // Use the gd_info() function if possible. if (function_exists('gd_info')) { $ver_info = gd_info(); preg_match('/\d/', $ver_info['GD Version'], $match); $gd_ver = $match[0]; return $match[0]; } // If phpinfo() is disabled use a specified / fail-safe choice... if (preg_match('/phpinfo/', ini_get('disable_functions'))) { if ($user_ver == 2) { $gd_ver = 2; return 2; } else { $gd_ver = 1; return 1; } } // ...otherwise use phpinfo(). ob_start(); phpinfo(8); $info = ob_get_contents(); ob_end_clean(); $info = stristr($info, 'gd version'); preg_match('/\d/', $info, $match); $gd_ver = $match[0]; return $match[0]; } // End gdVersion() // Usage: if ($gdv = gdVersion()) { if ($gdv >=2) { echo 'TrueColor functions may be used.'; } else { echo 'GD version is 1. Avoid the TrueColor functions.'; } } else { echo "The GD extension isn't loaded."; } ?> The function only detects the GD version, but you could determine GD capabilities in a similar manner. ---- Manual Page -- http://www.php.net/manual/en/function.gd-info.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52481 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52481&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52481&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52481&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52481&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52481&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52481&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88896) next »