1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
4
|
|
|
* you may not use this file except in compliance with the License. |
5
|
|
|
* You may obtain a copy of the License at |
6
|
|
|
* |
7
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
* |
9
|
|
|
* Unless required by applicable law or agreed to in writing, software |
10
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
11
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12
|
|
|
* See the License for the specific language governing permissions and |
13
|
|
|
* limitations under the License. |
14
|
|
|
* |
15
|
|
|
* @author Alexander Zakharov <[email protected]> |
16
|
|
|
* @link http://rootlocal.ru |
17
|
|
|
* @copyright Copyright © 2018 rootlocal.ru |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace image\components\drivers; |
21
|
|
|
|
22
|
|
|
use yii\base\ErrorException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Driver |
26
|
|
|
* @package image\components\drivers |
27
|
|
|
*/ |
28
|
|
|
class Driver implements DriverInterface |
29
|
|
|
{ |
30
|
|
|
// Resizing constraints |
31
|
|
|
const NONE = 0x01; |
32
|
|
|
const WIDTH = 0x02; |
33
|
|
|
const HEIGHT = 0x03; |
34
|
|
|
const AUTO = 0x04; |
35
|
|
|
const INVERSE = 0x05; |
36
|
|
|
const PRECISE = 0x06; |
37
|
|
|
const ADAPT = 0x07; |
38
|
|
|
const CROP = 0x08; |
39
|
|
|
|
40
|
|
|
// Flipping directions |
41
|
|
|
const HORIZONTAL = 0x11; |
42
|
|
|
const VERTICAL = 0x12; |
43
|
|
|
|
44
|
|
|
// Status of the driver check |
45
|
|
|
protected static $_checked = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string image file path |
49
|
|
|
*/ |
50
|
|
|
public $file; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var integer image width |
54
|
|
|
*/ |
55
|
|
|
public $width; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var integer image height |
59
|
|
|
*/ |
60
|
|
|
public $height; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var integer one of the IMAGETYPE_* constants |
64
|
|
|
*/ |
65
|
|
|
public $type; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string mime type of the image |
69
|
|
|
*/ |
70
|
|
|
public $mime; |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Loads information about the image. Will throw an exception if the image |
75
|
|
|
* does not exist or is not an image. |
76
|
|
|
* |
77
|
|
|
* @param string $file image file path |
78
|
|
|
* @return void |
79
|
|
|
* @throws ErrorException |
80
|
|
|
*/ |
81
|
|
|
public function __construct($file) |
82
|
|
|
{ |
83
|
|
|
$info = []; |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
// Get the real path to the file |
87
|
|
|
$file = realpath($file); |
88
|
|
|
|
89
|
|
|
// Get the image information |
90
|
|
|
$info = getimagesize($file); |
91
|
|
|
// Store the image information |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
} catch (\Exception $e) { |
95
|
|
|
// Ignore all errors while reading the image |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (empty($file) || empty($info)) { |
99
|
|
|
throw new ErrorException(sprintf('Not an image or invalid image: %s', $file)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->file = $file; |
103
|
|
|
$this->width = $info[0]; |
104
|
|
|
$this->height = $info[1]; |
105
|
|
|
$this->type = $info[2]; |
106
|
|
|
$this->mime = image_type_to_mime_type($this->type); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Execute a resize. |
111
|
|
|
* |
112
|
|
|
* @param integer $width new width |
113
|
|
|
* @param integer $height new height |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function _do_resize($width, $height) |
117
|
|
|
{ |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Adaptation the image. |
122
|
|
|
* |
123
|
|
|
* @param integer $width image width |
124
|
|
|
* @param integer $height image height |
125
|
|
|
* @param integer $bg_width background width |
126
|
|
|
* @param integer $bg_height background height |
127
|
|
|
* @param integer $offset_x offset from the left |
128
|
|
|
* @param integer $offset_y offset from the top |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function _do_adapt($width, $height, $bg_width, $bg_height, $offset_x, $offset_y) |
132
|
|
|
{ |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Execute a crop. |
137
|
|
|
* |
138
|
|
|
* @param integer $width new width |
139
|
|
|
* @param integer $height new height |
140
|
|
|
* @param integer $offset_x offset from the left |
141
|
|
|
* @param integer $offset_y offset from the top |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function _do_crop($width, $height, $offset_x, $offset_y) |
145
|
|
|
{ |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Execute a rotation. |
150
|
|
|
* |
151
|
|
|
* @param integer $degrees degrees to rotate |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function _do_rotate($degrees) |
155
|
|
|
{ |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Execute a flip. |
160
|
|
|
* |
161
|
|
|
* @param integer $direction direction to flip |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function _do_flip($direction) |
165
|
|
|
{ |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Execute a sharpen. |
170
|
|
|
* |
171
|
|
|
* @param integer $amount amount to sharpen |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public function _do_sharpen($amount) |
175
|
|
|
{ |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Execute a reflection. |
180
|
|
|
* |
181
|
|
|
* @param integer $height reflection height |
182
|
|
|
* @param integer $opacity reflection opacity |
183
|
|
|
* @param boolean $fade_in true to fade out, false to fade in |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
|
|
public function _do_reflection($height, $opacity, $fade_in) |
187
|
|
|
{ |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Execute a watermarking. |
192
|
|
|
* |
193
|
|
|
* @param \image\components\Kohana\Image $image watermarking |
194
|
|
|
* @param integer $offset_x offset from the left |
195
|
|
|
* @param integer $offset_y offset from the top |
196
|
|
|
* @param integer $opacity opacity of watermark |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
public function _do_watermark(\image\components\Kohana\Image $image, $offset_x, $offset_y, $opacity) |
200
|
|
|
{ |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Execute a background. |
205
|
|
|
* |
206
|
|
|
* @param integer $r red |
207
|
|
|
* @param integer $g green |
208
|
|
|
* @param integer $b blue |
209
|
|
|
* @param integer $opacity opacity |
210
|
|
|
* @return void |
211
|
|
|
*/ |
212
|
|
|
public function _do_background($r, $g, $b, $opacity) |
213
|
|
|
{ |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Execute a save. |
218
|
|
|
* |
219
|
|
|
* @param string $file new image filename |
220
|
|
|
* @param integer $quality quality |
221
|
|
|
* @return boolean |
222
|
|
|
*/ |
223
|
|
|
public function _do_save($file, $quality) |
224
|
|
|
{ |
225
|
|
|
return false; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Execute a render. |
230
|
|
|
* |
231
|
|
|
* @param string $type image type: png, jpg, gif, etc |
232
|
|
|
* @param integer $quality quality |
233
|
|
|
* @return string |
234
|
|
|
*/ |
235
|
|
|
public function _do_render($type, $quality) |
236
|
|
|
{ |
237
|
|
|
return ''; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|