1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Glide\Api; |
4
|
|
|
|
5
|
|
|
use Intervention\Image\ImageManager; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use League\Glide\Manipulators\ManipulatorInterface; |
8
|
|
|
|
9
|
|
|
class Api implements ApiInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Intervention image manager. |
13
|
|
|
* @var ImageManager |
14
|
|
|
*/ |
15
|
|
|
protected $imageManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Collection of manipulators. |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $manipulators; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create API instance. |
25
|
|
|
* @param ImageManager $imageManager Intervention image manager. |
26
|
|
|
* @param array $manipulators Collection of manipulators. |
27
|
|
|
*/ |
28
|
|
|
public function __construct(ImageManager $imageManager, array $manipulators) |
29
|
|
|
{ |
30
|
|
|
$this->setImageManager($imageManager); |
31
|
|
|
$this->setManipulators($manipulators); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set the image manager. |
36
|
|
|
* @param ImageManager $imageManager Intervention image manager. |
37
|
|
|
*/ |
38
|
|
|
public function setImageManager(ImageManager $imageManager) |
39
|
|
|
{ |
40
|
|
|
$this->imageManager = $imageManager; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the image manager. |
45
|
|
|
* @return ImageManager Intervention image manager. |
46
|
|
|
*/ |
47
|
|
|
public function getImageManager() |
48
|
|
|
{ |
49
|
|
|
return $this->imageManager; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set the manipulators. |
54
|
|
|
* @param array $manipulators Collection of manipulators. |
55
|
|
|
*/ |
56
|
|
|
public function setManipulators(array $manipulators) |
57
|
|
|
{ |
58
|
|
|
foreach ($manipulators as $manipulator) { |
59
|
|
|
if (!($manipulator instanceof ManipulatorInterface)) { |
60
|
|
|
throw new InvalidArgumentException('Not a valid manipulator.'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->manipulators = $manipulators; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the manipulators. |
69
|
|
|
* @return array Collection of manipulators. |
70
|
|
|
*/ |
71
|
|
|
public function getManipulators() |
72
|
|
|
{ |
73
|
|
|
return $this->manipulators; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Perform image manipulations. |
78
|
|
|
* @param string $source Source image binary data. |
79
|
|
|
* @param array $params The manipulation params. |
80
|
|
|
* @return string Manipulated image binary data. |
81
|
|
|
*/ |
82
|
|
|
public function run($source, array $params) |
83
|
|
|
{ |
84
|
|
|
$image = $this->imageManager->make($source); |
85
|
|
|
|
86
|
|
|
foreach ($this->manipulators as $manipulator) { |
87
|
|
|
$manipulator->setParams($params); |
88
|
|
|
|
89
|
|
|
$image = $manipulator->run($image); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $image->getEncoded(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|