13

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.

random
  • 9,774
  • 10
  • 66
  • 83
Bluemagica
  • 5,000
  • 12
  • 47
  • 73

6 Answers6

17

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.

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
Jon
  • 428,835
  • 81
  • 738
  • 806
  • that's the easiest most effective way to check for the extension! – Thariama Jan 04 '11 at 14:20
  • 1
    @downvoter: I would so love to hear what's wrong with this answer. Thanks. – Jon Jun 08 '12 at 14:24
  • You can check for `gd` extension, but you cannot check for `freetype` simply because it's not PHP extension (see output of `php -m`). `freetype` is just linked to gd during compilation time. – rr- Sep 13 '13 at 09:56
12

Use function_exists:

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

Hope that helps.

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
Norm
  • 583
  • 5
  • 15
  • 2
    +1 this does not a check if freetype is installed, but a check if a function imagettftext is available - but this should work in the given context – Thariama Jan 04 '11 at 13:44
  • :) I was just hoping to point this person in a correct direction. – Norm Jan 04 '11 at 13:45
  • Yes it does check for both GD and FreeType. [imagettftext manual page](http://php.net/manual/en/function.imagettftext.php) – Bob Ray Jul 27 '16 at 05:13
5

Assuming the GD library is installed, you can detect Freetype support using the gd_info() function.

$gdinfo = gd_info();
if($gdinfo['FreeType Support']) echo 'FreeType Support Enabled';

If you need to check whether or not GD library is installed first, use extension_loaded('gd');

BenTheDesigner
  • 1,954
  • 3
  • 17
  • 21
3

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.

Anthony
  • 1,776
  • 17
  • 29
1

A first somewhat complicated approach:

call php_info() and search/parse the result for freetype

Thariama
  • 50,002
  • 13
  • 138
  • 166
  • this might be complicated, but it will work (so why the downvote?) – Thariama Jan 04 '11 at 13:44
  • 3
    Downvote wasn't mine, but 1) it will take effort to get it to work, 2) it's more brittle than any other solution (suppose the output of `php_info` changes in the future) and 3) it's the most computationally expensive way to do it by far. – Jon Jan 04 '11 at 14:10
0

Try function_exists(), i.e.

if (!function_exists('imagettftext')) {
  // No freetype library
}

You'd probably be best asking for alternatives to imagettftext as a separate question.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128