|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NwLaravel\FileStorage; |
|
4
|
|
|
|
|
5
|
|
|
use Imagick; |
|
6
|
|
|
|
|
7
|
|
|
class ImagineImagick implements Imagine |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Imagick |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $image; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Construct |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $path |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct($path) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->image = new Imagick($path); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Execute in Canvas |
|
26
|
|
|
* |
|
27
|
|
|
* @param \Closure $callback |
|
28
|
|
|
* |
|
29
|
|
|
* @return Imagine |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function execute($callback) |
|
32
|
|
|
{ |
|
33
|
|
|
$format = strtolower($this->image->getImageFormat()); |
|
34
|
|
|
|
|
35
|
|
|
if ($format == 'gif') { |
|
36
|
|
|
$this->image = $this->image->coalesceImages(); |
|
37
|
|
|
do { |
|
38
|
|
|
$callback($this->image); |
|
39
|
|
|
} while ($this->image->nextImage()); |
|
40
|
|
|
|
|
41
|
|
|
$this->image = $this->image->deconstructImages(); |
|
42
|
|
|
} else { |
|
43
|
|
|
$callback($this->image); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Filesize |
|
51
|
|
|
* |
|
52
|
|
|
* @return int |
|
53
|
|
|
*/ |
|
54
|
|
|
public function filesize() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->image->getImageLength(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Define Resize |
|
61
|
|
|
* |
|
62
|
|
|
* @param int $width |
|
63
|
|
|
* @param int $height |
|
64
|
|
|
* @param boolean $force |
|
65
|
|
|
* |
|
66
|
|
|
* @return Imagine |
|
67
|
|
|
*/ |
|
68
|
|
|
public function resize($width, $height, $force = false) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->execute(function ($image) use ($width, $height, $force) { |
|
71
|
|
|
$image->scaleImage($width, $height, !$force); |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Opacity |
|
77
|
|
|
* |
|
78
|
|
|
* @return Imagine |
|
79
|
|
|
*/ |
|
80
|
|
|
public function opacity($opacity) |
|
81
|
|
|
{ |
|
82
|
|
|
$opacity = intval($opacity); |
|
83
|
|
|
|
|
84
|
|
|
if ($opacity > 0 && $opacity < 100) { |
|
85
|
|
|
$this->execute(function ($image) use ($opacity) { |
|
86
|
|
|
$image->setImageOpacity($opacity/100); |
|
87
|
|
|
}); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Watermark |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $path |
|
97
|
|
|
* @param string $position |
|
98
|
|
|
* @param integer $opacity |
|
99
|
|
|
* |
|
100
|
|
|
* @return Imagine |
|
101
|
|
|
*/ |
|
102
|
|
|
public function watermark($path, $position = 'center', $opacity = null) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($this->isImage($path)) { |
|
105
|
|
|
$watermark = new \Imagick($path); |
|
106
|
|
|
|
|
107
|
|
|
$opacity = intval($opacity); |
|
108
|
|
|
if ($opacity > 0 && $opacity < 100) { |
|
109
|
|
|
$watermark->setImageOpacity($opacity/100); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$self = $this; |
|
113
|
|
|
|
|
114
|
|
|
$this->execute(function ($image) use ($watermark, $position, $self) { |
|
115
|
|
|
$self->watermarkCanvas($image, $watermark, $position); |
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
protected function watermarkCanvas(Imagick $image, Imagick $watermark, $position = 'center') |
|
123
|
|
|
{ |
|
124
|
|
|
// how big are the images? |
|
125
|
|
|
$iWidth = $image->getImageWidth(); |
|
126
|
|
|
$iHeight = $image->getImageHeight(); |
|
127
|
|
|
$wWidth = $watermark->getImageWidth(); |
|
128
|
|
|
$wHeight = $watermark->getImageHeight(); |
|
129
|
|
|
|
|
130
|
|
|
if ($iHeight < $wHeight || $iWidth < $wWidth) { |
|
131
|
|
|
// resize the watermark |
|
132
|
|
|
$watermark->scaleImage($iWidth, $iHeight, true); |
|
133
|
|
|
|
|
134
|
|
|
// get new size |
|
135
|
|
|
$wWidth = $watermark->getImageWidth(); |
|
136
|
|
|
$wHeight = $watermark->getImageHeight(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$xOffset = 0; |
|
|
|
|
|
|
140
|
|
|
$yOffset = 0; |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
switch ($position) { |
|
143
|
|
|
case 'center': |
|
144
|
|
|
default: |
|
145
|
|
|
$x = ($iWidth - $wWidth) / 2; |
|
146
|
|
|
$y = ($iHeight - $wHeight) / 2; |
|
147
|
|
|
break; |
|
148
|
|
|
case 'topLeft': |
|
|
|
|
|
|
149
|
|
|
$x = $xOffset; |
|
150
|
|
|
$y = $yOffset; |
|
151
|
|
|
break; |
|
152
|
|
|
case 'top': |
|
153
|
|
|
case 'topCenter': |
|
154
|
|
|
$x = ($iWidth - $wWidth) / 2; |
|
155
|
|
|
$y = $yOffset; |
|
156
|
|
|
break; |
|
157
|
|
|
case 'topRight': |
|
158
|
|
|
$x = $iWidth - $wWidth - $xOffset; |
|
159
|
|
|
$y = $yOffset; |
|
160
|
|
|
break; |
|
161
|
|
|
case 'right': |
|
162
|
|
|
case 'rightCenter': |
|
163
|
|
|
$x = $iWidth - $wWidth - $xOffset; |
|
164
|
|
|
$y = ($iHeight - $wHeight) / 2; |
|
165
|
|
|
break; |
|
166
|
|
|
case 'bottomRight': |
|
167
|
|
|
$x = $iWidth - $wWidth - $xOffset; |
|
168
|
|
|
$y = $iHeight - $wHeight - $yOffset; |
|
169
|
|
|
break; |
|
170
|
|
|
case 'bottom': |
|
171
|
|
|
case 'bottomCenter': |
|
172
|
|
|
$x = ($iWidth - $wWidth) / 2; |
|
173
|
|
|
$y = $iHeight - $wHeight - $yOffset; |
|
174
|
|
|
break; |
|
175
|
|
|
case 'bottomLeft': |
|
176
|
|
|
$x = $xOffset; |
|
177
|
|
|
$y = $iHeight - $wHeight - $yOffset; |
|
178
|
|
|
break; |
|
179
|
|
|
case 'left': |
|
180
|
|
|
case 'leftCenter': |
|
181
|
|
|
$x = $xOffset; |
|
182
|
|
|
$y = ($iHeight - $wHeight) / 2; |
|
183
|
|
|
break; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Is Image |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $path |
|
193
|
|
|
* |
|
194
|
|
|
* @return boolean |
|
195
|
|
|
*/ |
|
196
|
|
|
protected function isImage($path) |
|
197
|
|
|
{ |
|
198
|
|
|
return (bool) ($path && is_file($path) && strpos(mime_content_type($path), 'image/')===0); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Crop |
|
203
|
|
|
* |
|
204
|
|
|
* @param integer $width |
|
205
|
|
|
* @param integer $height |
|
206
|
|
|
* @param integer $x |
|
207
|
|
|
* @param integer $y |
|
208
|
|
|
* |
|
209
|
|
|
* @return binary |
|
210
|
|
|
*/ |
|
211
|
|
|
public function crop($width, $height, $x, $y) |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->execute(function ($image) use ($width, $height, $x, $y) { |
|
|
|
|
|
|
214
|
|
|
$image->cropImage($width, $height, $x, $y); |
|
215
|
|
|
}); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Encode |
|
220
|
|
|
* |
|
221
|
|
|
* @param string $format |
|
222
|
|
|
* @param integer $quality |
|
223
|
|
|
* |
|
224
|
|
|
* @return binary |
|
225
|
|
|
*/ |
|
226
|
|
|
public function encode($format = null, $quality = null) |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->image->getImagesBlob(); |
|
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Save |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $path |
|
235
|
|
|
* @param integer $quality |
|
236
|
|
|
* |
|
237
|
|
|
* @return binary |
|
238
|
|
|
*/ |
|
239
|
|
|
public function save($path, $quality = null) |
|
240
|
|
|
{ |
|
241
|
|
|
$quality = intval($quality); |
|
242
|
|
|
if ($quality > 0 && $quality <= 100) { |
|
243
|
|
|
$this->image->setImageCompressionQuality($quality); |
|
244
|
|
|
} |
|
245
|
|
|
$this->image->writeImage($path); |
|
246
|
|
|
|
|
247
|
|
|
return $this; |
|
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.