ImageManager.php 3.4 KB

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