Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Engine.php
1<?php
10
11use Bitrix\Main;
13
19abstract class Engine
20{
21 protected $file;
22 protected $info;
23 protected $options;
24 protected $substituted = false;
25
31 public function __construct($file = null, array $options = [])
32 {
33 if($file !== null)
34 {
35 $this->setFile($file);
36 }
37
38 $this->options = $options;
39 }
40
45 public function setFile($file)
46 {
47 $this->file = $file;
48 $this->info = null;
49 return $this;
50 }
51
57 public function getInfo($flashEnabled = false)
58 {
59 if($this->info !== null)
60 {
61 return $this->info;
62 }
63
64 if($this->file === null)
65 {
66 return null;
67 }
68
69 if(!file_exists($this->file))
70 {
71 return null;
72 }
73
74 /*
75 This will protect us from scan the whole file in order to find out size of the xbm image
76 ext/standard/image.c php_getimagetype
77 */
78 $handler = fopen($this->file, "rb");
79 if(!is_resource($handler))
80 {
81 return null;
82 }
83 $signature = fread($handler, 12);
84 fclose($handler);
85
86 if($flashEnabled)
87 {
88 $flashPattern = "
89 |FWS # php_sig_swf
90 |CWS # php_sig_swc
91 ";
92 }
93 else
94 {
95 $flashPattern = "";
96 }
97
98 if(preg_match("/^(
99 GIF # php_sig_gif
100 |\\xff\\xd8\\xff # php_sig_jpg
101 |\\x89\\x50\\x4e # php_sig_png
102 ".$flashPattern."
103 |8BPS # php_sig_psd
104 |BM # php_sig_bmp
105 |\\xff\\x4f\\xff # php_sig_jpc
106 |II\\x2a\\x00 # php_sig_tif_ii
107 |MM\\x00\\x2a # php_sig_tif_mm
108 |FORM # php_sig_iff
109 |\\x00\\x00\\x01\\x00 # php_sig_ico
110 |\\x00\\x00\\x00\\x0c
111 \\x6a\\x50\\x20\\x20
112 \\x0d\\x0a\\x87\\x0a # php_sig_jp2
113 |RIFF.{4}WEBP # php_sig_riff php_sig_webp
114 )/xs",
115 $signature
116 ))
117 {
118 //This function does not require the GD image library.
119 $size = getimagesize($this->file);
120
121 if($size !== false)
122 {
123 $this->info = ((new Image\Info())
124 ->setWidth($size[0])
125 ->setHeight($size[1])
126 ->setFormat($size[2])
127 ->setMime($size["mime"])
128 );
129 return $this->info;
130 }
131 }
132
133 return null;
134 }
135
140 public function getExifData()
141 {
142 $result = [];
143
144 if($this->file !== null)
145 {
146 if(function_exists("exif_read_data"))
147 {
148 // exif_read_data() generates unnecessary warnings
149 if(($exif = @exif_read_data($this->file)) !== false)
150 {
151 $culture = Main\Context::getCurrent()->getCulture();
152 $result = Main\Text\Encoding::convertEncoding($exif, ini_get('exif.encode_unicode'), $culture->getCharset());
153 }
154 }
155 }
156
157 return $result;
158 }
159
164 abstract public function load();
165
172 abstract public function rotate($angle, Color $bgColor);
173
178 abstract public function flipVertical();
179
184 abstract public function flipHorizontal();
185
191 abstract public function setOrientation($orientation);
192
199 abstract public function resize(Rectangle $source, Rectangle $destination);
200
206 abstract public function filter(Mask $mask);
207
213 abstract public function drawTextWatermark(TextWatermark $watermark);
214
220 abstract public function drawImageWatermark(ImageWatermark $watermark);
221
222 protected function loadWatermark(ImageWatermark $watermark)
223 {
224 $file = $watermark->getImageFile();
225
226 if($file === null)
227 {
228 return null;
229 }
230
231 $image = new static($file);
232
233 if(!$image->load())
234 {
235 return null;
236 }
237
238 if($watermark->getMode() == ImageWatermark::MODE_RESIZE)
239 {
240 $source = $image->getDimensions();
241 $destination = $this->getDimensions();
242
243 $destination->scale($watermark->getRatio());
244
245 if ($source->resize($destination, Image::RESIZE_PROPORTIONAL))
246 {
247 $image->resize($source, $destination);
248 }
249 }
250
251 return $image;
252 }
253
261 abstract public function save($file, $quality = 95, $format = null);
262
267 abstract public function getWidth();
268
273 abstract public function getHeight();
274
279 public function getDimensions()
280 {
281 return new Rectangle($this->getWidth(), $this->getHeight());
282 }
283
287 abstract public function clear();
288
294 public function substituted(): bool
295 {
296 return $this->substituted;
297 }
298
299 protected function getMaxSize()
300 {
301 if (isset($this->options["maxSize"]) && is_array($this->options["maxSize"]))
302 {
303 return new Rectangle($this->options["maxSize"][0], $this->options["maxSize"][1]);
304 }
305
306 return null;
307 }
308
314 public function exceedsMaxSize(): bool
315 {
316 $info = $this->getInfo();
317 if (!$info)
318 {
319 return false;
320 }
321
322 $source = $info->toRectangle();
323 $maxSize = $this->getMaxSize();
324
325 if ($maxSize && $source->resize($maxSize, Image::RESIZE_PROPORTIONAL))
326 {
327 return true;
328 }
329
330 return false;
331 }
332
333 public function __destruct()
334 {
335 $this->clear();
336 }
337}
__construct($file=null, array $options=[])
Definition Engine.php:31
rotate($angle, Color $bgColor)
getInfo($flashEnabled=false)
Definition Engine.php:57
drawTextWatermark(TextWatermark $watermark)
save($file, $quality=95, $format=null)
loadWatermark(ImageWatermark $watermark)
Definition Engine.php:222
drawImageWatermark(ImageWatermark $watermark)
resize(Rectangle $source, Rectangle $destination)