note 37523 modified in function.imagecreatetruecolor by didou

From: Date: Tue, 18 Nov 2003 23:10:19 +0000
Subject: note 37523 modified in function.imagecreatetruecolor by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-60591@lists.php.net to get a copy of this message
Unfortunately the @imagecreatetruecolor() method doesn't even work because php dies with a fatal error noting that GD 2 is required. You can't even capture this error with a custom error handler. I have come up with a function to get the GD version number that seems to work pretty well on every version of PHP and GD I've thrown at it (even CLI versions.) It's obviously not the most efficient thing in the world, but it does cache the result in a static variable so calling it multiple times doesn't slow down further. function gd_version() { static $gd_version_number = null; if ($gd_version_number === null) { // Use output buffering to get results from phpinfo() // without disturbing the page we're in. Output // buffering is "stackable" so we don't even have to // worry about previous or encompassing buffering. ob_start(); phpinfo(8); $module_info = ob_get_contents(); ob_end_clean(); if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info,$matches)) { $gd_version_number = $matches[1]; } else { $gd_version_number = 0; } } return $gd_version_number; } Then you can simply use it something like this: if (gd_version() >= 2) { $image = ImageCreateTrueColor($width, $height); } else { $image = ImageCreate($width, $height); } --was-- Unfortunately the @imagecreatetruecolor() method doesn't even work because php dies with a fatal error noting that GD 2 is required. You can't even capture this error with a custom error handler. I have come up with a function to get the GD version number that seems to work pretty well on every version of PHP and GD I've thrown at it (even CLI versions.) It's obviously not the most efficient thing in the world, but it does cache the result in a static variable so calling it multiple times doesn't slow down further. function gd_version() { static $gd_version_number = null; if ($gd_version_number === null) { // Use output buffering to get results from phpinfo() // without disturbing the page we're in. Output // buffering is "stackable" so we don't even have to // worry about previous or encompassing buffering. ob_start(); phpinfo(8); $module_info = ob_get_contents(); ob_end_clean(); if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info,$matches)) { $gd_version_number = $matches[1]; } else { $gd_version_number = 0; } } return $gd_version_number; } Then you can simply use it something like this: if (gd_version() > 2) { $image = ImageCreateTrueColor($width, $height); } else { $image = ImageCreate($width, $height); } http://www.php.net/manual/en/function.imagecreatetruecolor.php

« previous php.notes (#60591) next »