|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Image; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** @mixin \Spatie\Image\Manipulations */ |
|
8
|
|
|
class Image |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var string */ |
|
11
|
|
|
protected $pathToImage; |
|
12
|
|
|
|
|
13
|
|
|
/** @var \Spatie\Image\Manipulations */ |
|
14
|
|
|
protected $manipulations; |
|
15
|
|
|
|
|
16
|
|
|
/** @var */ |
|
17
|
|
|
protected $imageDriver = 'gd'; |
|
18
|
|
|
|
|
19
|
|
|
public static function load($pathToImage) |
|
20
|
|
|
{ |
|
21
|
|
|
return new static($pathToImage); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(string $pathToImage) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->pathToImage = $pathToImage; |
|
27
|
|
|
|
|
28
|
|
|
$this->manipulations = new Manipulations(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $imageDriver |
|
33
|
|
|
* |
|
34
|
|
|
* @return $this |
|
35
|
|
|
*/ |
|
36
|
|
|
public function useImageDriver(string $imageDriver) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->imageDriver = $imageDriver; |
|
39
|
|
|
|
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param callable|$manipulations |
|
45
|
|
|
* @return $this |
|
46
|
|
|
*/ |
|
47
|
|
|
public function manipulate($manipulations) |
|
48
|
|
|
{ |
|
49
|
|
|
if (is_callable($manipulations)) { |
|
50
|
|
|
$manipulations($this->manipulations); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($manipulations instanceof Manipulations) { |
|
54
|
|
|
$this->manipulations->mergeManipulations($manipulations); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function __call($name, $arguments) |
|
61
|
|
|
{ |
|
62
|
|
|
if (! method_exists($this->manipulations, $name)) { |
|
63
|
|
|
throw new Exception("Manipulation `{$name}` does not exist"); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->manipulations->$name(...$arguments); |
|
67
|
|
|
|
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getManipulationSequence(): ManipulationSequence |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->manipulations->getManipulationSequence(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function save($outputPath = '') |
|
77
|
|
|
{ |
|
78
|
|
|
if ($outputPath == '') { |
|
79
|
|
|
$outputPath = $this->pathToImage; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
GlideConversion::create($this->pathToImage) |
|
83
|
|
|
->useImageDriver($this->imageDriver) |
|
84
|
|
|
->performManipulations($this->manipulations) |
|
85
|
|
|
->save($outputPath); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|