Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Image.php
1<?php
10
12
13class Image
14{
15 const
16 FORMAT_PNG = \IMAGETYPE_PNG,
17 FORMAT_JPEG = \IMAGETYPE_JPEG,
18 FORMAT_GIF = \IMAGETYPE_GIF,
19 FORMAT_BMP = \IMAGETYPE_BMP,
20 FORMAT_WEBP = \IMAGETYPE_WEBP;
21
22 const
26
27 protected $file;
28
30 protected $engine;
31
36 public function __construct($file = null)
37 {
38 $this->file = $file;
39
40 $serviceLocator = DI\ServiceLocator::getInstance();
41 if($serviceLocator->has("main.imageEngine"))
42 {
43 $this->setEngine($serviceLocator->get("main.imageEngine"));
44 }
45 else
46 {
47 $this->setEngine(new Image\Gd());
48 }
49 }
50
55 public function setEngine(Image\Engine $engine)
56 {
57 $this->engine = $engine;
58
59 if($this->file !== null)
60 {
61 $this->engine->setFile($this->file);
62 }
63 }
64
69 public function getExifData()
70 {
71 return $this->engine->getExifData();
72 }
73
79 public function getInfo($flashEnabled = false)
80 {
81 return $this->engine->getInfo($flashEnabled);
82 }
83
88 public function load()
89 {
90 return $this->engine->load();
91 }
92
99 public function rotate($angle, Image\Color $bgColor = null)
100 {
101 if($bgColor === null)
102 {
103 $bgColor = new Image\Color();
104 }
105 return $this->engine->rotate($angle, $bgColor);
106 }
107
112 public function flipVertical()
113 {
114 return $this->engine->flipVertical();
115 }
116
121 public function flipHorizontal()
122 {
123 return $this->engine->flipHorizontal();
124 }
125
131 public function autoRotate($orientation)
132 {
133 if ($this->engine->substituted())
134 {
135 // no need to rotated a stub image
136 return false;
137 }
138
139 if($orientation > 1)
140 {
141 if($orientation == 7 || $orientation == 8)
142 {
143 $this->rotate(270);
144 }
145 elseif($orientation == 3 || $orientation == 4)
146 {
147 $this->rotate(180);
148 }
149 elseif($orientation == 5 || $orientation == 6)
150 {
151 $this->rotate(90);
152 }
153
154 if ($orientation == 2 || $orientation == 7 || $orientation == 4 || $orientation == 5)
155 {
156 $this->flipHorizontal();
157 }
158
159 $this->setOrientation(1);
160
161 return true;
162 }
163 return false;
164 }
165
171 public function setOrientation($orientation)
172 {
173 return $this->engine->setOrientation($orientation);
174 }
175
182 public function resize(Image\Rectangle $source, Image\Rectangle $destination)
183 {
184 return $this->engine->resize($source, $destination);
185 }
186
192 public function filter(Image\Mask $mask)
193 {
194 return $this->engine->filter($mask);
195 }
196
202 public function drawWatermark(Image\Watermark $watermark)
203 {
204 if($watermark instanceof Image\TextWatermark)
205 {
206 return $this->engine->drawTextWatermark($watermark);
207 }
208 if($watermark instanceof Image\ImageWatermark)
209 {
210 return $this->engine->drawImageWatermark($watermark);
211 }
212 return false;
213 }
214
220 public function save($quality = 95)
221 {
222 if($quality <= 0 || $quality > 100)
223 {
224 $quality = 95;
225 }
226
227 if($this->engine->save($this->file, $quality))
228 {
229 $this->setFileAttributes($this->file);
230
231 return true;
232 }
233 return false;
234 }
235
243 public function saveAs($file, $quality = 95, $format = null)
244 {
245 if($quality <= 0 || $quality > 100)
246 {
247 $quality = 95;
248 }
249
250 if($this->engine->save($file, $quality, $format))
251 {
252 $this->setFileAttributes($file);
253
254 return true;
255 }
256 return false;
257 }
258
259 protected function setFileAttributes($file)
260 {
261 @chmod($file, BX_FILE_PERMISSIONS);
262 clearstatcache(true, $file);
263 }
264
269 public function getWidth()
270 {
271 return $this->engine->getWidth();
272 }
273
278 public function getHeight()
279 {
280 return $this->engine->getHeight();
281 }
282
287 public function getDimensions()
288 {
289 return $this->engine->getDimensions();
290 }
291
297 public function exceedsMaxSize(): bool
298 {
299 return $this->engine->exceedsMaxSize();
300 }
301
305 public function clear()
306 {
307 $this->engine->clear();
308 }
309}
autoRotate($orientation)
Definition Image.php:131
rotate($angle, Image\Color $bgColor=null)
Definition Image.php:99
filter(Image\Mask $mask)
Definition Image.php:192
__construct($file=null)
Definition Image.php:36
setEngine(Image\Engine $engine)
Definition Image.php:55
const RESIZE_PROPORTIONAL
Definition Image.php:24
drawWatermark(Image\Watermark $watermark)
Definition Image.php:202
getInfo($flashEnabled=false)
Definition Image.php:79
saveAs($file, $quality=95, $format=null)
Definition Image.php:243
resize(Image\Rectangle $source, Image\Rectangle $destination)
Definition Image.php:182
save($quality=95)
Definition Image.php:220
setOrientation($orientation)
Definition Image.php:171
const RESIZE_PROPORTIONAL_ALT
Definition Image.php:23