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