ImageManager.php 3.4 KB

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