Driver.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Intervention\Image\Imagick;
  3. use Intervention\Image\AbstractDriver;
  4. use Intervention\Image\Exception\NotSupportedException;
  5. use Intervention\Image\Image;
  6. class Driver extends AbstractDriver
  7. {
  8. /**
  9. * Creates new instance of driver
  10. *
  11. * @param Decoder $decoder
  12. * @param Encoder $encoder
  13. */
  14. public function __construct(Decoder $decoder = null, Encoder $encoder = null)
  15. {
  16. if ( ! $this->coreAvailable()) {
  17. throw new NotSupportedException(
  18. "ImageMagick module not available with this PHP installation."
  19. );
  20. }
  21. $this->decoder = $decoder ? $decoder : new Decoder;
  22. $this->encoder = $encoder ? $encoder : new Encoder;
  23. }
  24. /**
  25. * Creates new image instance
  26. *
  27. * @param int $width
  28. * @param int $height
  29. * @param mixed $background
  30. * @return \Intervention\Image\Image
  31. */
  32. public function newImage($width, $height, $background = null)
  33. {
  34. $background = new Color($background);
  35. // create empty core
  36. $core = new \Imagick;
  37. $core->newImage($width, $height, $background->getPixel(), 'png');
  38. $core->setType(\Imagick::IMGTYPE_UNDEFINED);
  39. $core->setImageType(\Imagick::IMGTYPE_UNDEFINED);
  40. $core->setColorspace(\Imagick::COLORSPACE_UNDEFINED);
  41. // build image
  42. $image = new Image(new static, $core);
  43. return $image;
  44. }
  45. /**
  46. * Reads given string into color object
  47. *
  48. * @param string $value
  49. * @return AbstractColor
  50. */
  51. public function parseColor($value)
  52. {
  53. return new Color($value);
  54. }
  55. /**
  56. * Checks if core module installation is available
  57. *
  58. * @return boolean
  59. */
  60. protected function coreAvailable()
  61. {
  62. return (extension_loaded('imagick') && class_exists('Imagick'));
  63. }
  64. }