views:

136

answers:

3

I have some code which is resizing images, using either Imagick or GD, depending what's available on the server.

I'm testing for availability of each using the extension_loaded() function.

if (extension_loaded('imagick')) {
    $image = new Imagick();
    ...
}

I have one user reporting that they are receiving:

Fatal error: Class 'Imagick' not found

What circumstances would result in the Imagick extension being loaded but the class not available? How should I be testing to make my code more robust?

A: 

Case sensitive?

'imagick' and Imagick.

+2  A: 

You could check if the class exists also?

class_exists("Imagick")

Danny Croft
+1  A: 

1: always do the checks in a case-insensitive manner (make the string lowercase before comparing it)

2: don't check for the library, check for features. Maybe it has a library version that's buggy or has other function names

3: in php.ini you may disable some functions explicitly by name so I think you should resort to point #2 and check with function_exists instead of extension_*

Also, take a look at /var/log/apache2/errors or the equivalent on that client's server to check for any internal error generated by the ImageMagick extension (segmentation fault or other types of low-level errors should get reported in there...)

Quamis
In this case it turns out that there's an old version of the library that is function- rather than class-based. Testing for features (in this case using `class_exists()` as Danny Croft suggested, avoids the problem. Good advice.
drewm