Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if the FreeType PHP extension is installed on the server

How do I test to see if the FreeType extension is installed on a server running PHP?

I wanted to make a simple CAPTCHA system on my site, so I used imagettftext() and it worked fine. But what if the server didn't have the FreeType library installed?

So is there a way to somehow detect FreeType library through code, and if it is not present, fall back to something like imagestring()?

If I can't use imagettftext() I may have to look at alternatives to draw big font text as the imagestring max size isn't good for something like a CAPTCHA.

like image 874
Bluemagica Avatar asked Jan 04 '11 13:01

Bluemagica


3 Answers

This won't work for dynamic code (so not a true answer to the question) but for anyone who just want's to know if it's installed, from the command line on Linux:

php -i | grep -E "GD|FreeType"

Outputs:

GD Support => enabled
GD headers Version => 2.2.5
GD library Version => 2.2.5
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.4.11

NOTE: On a system without it installed you'll get no output.

like image 186
Anthony Avatar answered Oct 24 '22 12:10

Anthony


This will not be better in practice than the function_exists solutions already posted, but the technically correct way to check is by using extension_loaded.

like image 37
Jon Avatar answered Oct 24 '22 12:10

Jon


Use function_exists:

if (function_exists('imagettftext')) {
     imagettftext();
} else {
     // do other function
}

Hope that helps.

like image 27
Norm Avatar answered Oct 24 '22 12:10

Norm