diff --git a/src/Drivers/AbstractDriver.php b/src/Drivers/AbstractDriver.php index d21252a2..723d6e05 100644 --- a/src/Drivers/AbstractDriver.php +++ b/src/Drivers/AbstractDriver.php @@ -11,6 +11,11 @@ use ReflectionClass; abstract class AbstractDriver implements DriverInterface { + public function __construct() + { + $this->checkHealth(); + } + /** * Return a specialized version for the current driver of the given object * diff --git a/src/Drivers/Gd/Driver.php b/src/Drivers/Gd/Driver.php index 7c38b8f3..8c53957c 100644 --- a/src/Drivers/Gd/Driver.php +++ b/src/Drivers/Gd/Driver.php @@ -3,6 +3,7 @@ namespace Intervention\Image\Drivers\Gd; use Intervention\Image\Drivers\AbstractDriver; +use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Image; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorProcessorInterface; @@ -22,6 +23,20 @@ class Driver extends AbstractDriver return 'GD'; } + /** + * {@inheritdoc} + * + * @see DriverInterface::checkHealth() + */ + public function checkHealth(): void + { + if (!extension_loaded('gd') || !function_exists('gd_info')) { + throw new RuntimeException( + 'GD Library extension not available with this PHP installation.' + ); + } + } + /** * {@inheritdoc} * diff --git a/src/Drivers/Imagick/Driver.php b/src/Drivers/Imagick/Driver.php index 823007ab..99ea0449 100644 --- a/src/Drivers/Imagick/Driver.php +++ b/src/Drivers/Imagick/Driver.php @@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Imagick; use Imagick; use ImagickPixel; use Intervention\Image\Drivers\AbstractDriver; +use Intervention\Image\Exceptions\RuntimeException; use Intervention\Image\Image; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorProcessorInterface; @@ -24,6 +25,20 @@ class Driver extends AbstractDriver return 'Imagick'; } + /** + * {@inheritdoc} + * + * @see DriverInterface::checkHealth() + */ + public function checkHealth(): void + { + if (!extension_loaded('imagick') || !class_exists('Imagick')) { + throw new RuntimeException( + 'ImageMagick extension not available with this PHP installation.' + ); + } + } + /** * {@inheritdoc} * diff --git a/src/Interfaces/DriverInterface.php b/src/Interfaces/DriverInterface.php index 3443f2ef..1f7a4490 100644 --- a/src/Interfaces/DriverInterface.php +++ b/src/Interfaces/DriverInterface.php @@ -51,4 +51,11 @@ interface DriverInterface * @return ColorProcessorInterface */ public function colorProcessor(ColorspaceInterface $colorspace): ColorProcessorInterface; + + /** + * Check whether all requirements for operating the driver are met + * + * @return void + */ + public function checkHealth(): void; }