ImageManager.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Intervention\Image;
  3. use Closure;
  4. use Intervention\Image\Exception\MissingDependencyException;
  5. use Intervention\Image\Exception\NotSupportedException;
  6. class ImageManager
  7. {
  8. /**
  9. * Config
  10. *
  11. * @var array
  12. */
  13. public $config = [
  14. 'driver' => 'gd'
  15. ];
  16. /**
  17. * Creates new instance of Image Manager
  18. *
  19. * @param array $config
  20. */
  21. public function __construct(array $config = [])
  22. {
  23. $this->checkRequirements();
  24. $this->configure($config);
  25. }
  26. /**
  27. * Overrides configuration settings
  28. *
  29. * @param array $config
  30. *
  31. * @return self
  32. */
  33. public function configure(array $config = [])
  34. {
  35. $this->config = array_replace($this->config, $config);
  36. return $this;
  37. }
  38. /**
  39. * Initiates an Image instance from different input types
  40. *
  41. * @param mixed $data
  42. *
  43. * @return \Intervention\Image\Image
  44. */
  45. public function make($data)
  46. {
  47. return $this->createDriver()->init($data);
  48. }
  49. /**
  50. * Creates an empty image canvas
  51. *
  52. * @param int $width
  53. * @param int $height
  54. * @param mixed $background
  55. *
  56. * @return \Intervention\Image\Image
  57. */
  58. public function canvas($width, $height, $background = null)
  59. {
  60. return $this->createDriver()->newImage($width, $height, $background);
  61. }
  62. /**
  63. * Create new cached image and run callback
  64. * (requires additional package intervention/imagecache)
  65. *
  66. * @param Closure $callback
  67. * @param int $lifetime
  68. * @param boolean $returnObj
  69. *
  70. * @return Image
  71. */
  72. public function cache(Closure $callback, $lifetime = null, $returnObj = false)
  73. {
  74. if (class_exists('Intervention\\Image\\ImageCache')) {
  75. // create imagecache
  76. $imagecache = new ImageCache($this);
  77. // run callback
  78. if (is_callable($callback)) {
  79. $callback($imagecache);
  80. }
  81. return $imagecache->get($lifetime, $returnObj);
  82. }
  83. throw new MissingDependencyException(
  84. "Please install package intervention/imagecache before running this function."
  85. );
  86. }
  87. /**
  88. * Creates a driver instance according to config settings
  89. *
  90. * @return \Intervention\Image\AbstractDriver
  91. */
  92. private function createDriver()
  93. {
  94. if (is_string($this->config['driver'])) {
  95. $drivername = ucfirst($this->config['driver']);
  96. $driverclass = sprintf('Intervention\\Image\\%s\\Driver', $drivername);
  97. if (class_exists($driverclass)) {
  98. return new $driverclass;
  99. }
  100. throw new NotSupportedException(
  101. "Driver ({$drivername}) could not be instantiated."
  102. );
  103. }
  104. if ($this->config['driver'] instanceof AbstractDriver) {
  105. return $this->config['driver'];
  106. }
  107. throw new NotSupportedException(
  108. "Unknown driver type."
  109. );
  110. }
  111. /**
  112. * Check if all requirements are available
  113. *
  114. * @return void
  115. */
  116. private function checkRequirements()
  117. {
  118. if ( ! function_exists('finfo_buffer')) {
  119. throw new MissingDependencyException(
  120. "PHP Fileinfo extension must be installed/enabled to use Intervention Image."
  121. );
  122. }
  123. }
  124. }