1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Engine.php
См. документацию.
1<?php
8
9namespace Bitrix\Main\File\Image;
10
11use Bitrix\Main;
12use Bitrix\Main\File\Image;
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
208 abstract public function blur(int $sigma): bool;
209
215 abstract public function filter(Mask $mask);
216
222 abstract public function drawTextWatermark(TextWatermark $watermark);
223
229 abstract public function drawImageWatermark(ImageWatermark $watermark);
230
231 protected function loadWatermark(ImageWatermark $watermark)
232 {
233 $file = $watermark->getImageFile();
234
235 if($file === null)
236 {
237 return null;
238 }
239
240 $image = new static($file);
241
242 if(!$image->load())
243 {
244 return null;
245 }
246
247 if($watermark->getMode() == ImageWatermark::MODE_RESIZE)
248 {
249 $source = $image->getDimensions();
250 $destination = $this->getDimensions();
251
252 $destination->scale($watermark->getRatio());
253
254 if ($source->resize($destination, Image::RESIZE_PROPORTIONAL))
255 {
256 $image->resize($source, $destination);
257 }
258 }
259
260 return $image;
261 }
262
270 abstract public function save($file, $quality = 95, $format = null);
271
276 abstract public function getWidth();
277
282 abstract public function getHeight();
283
288 public function getDimensions()
289 {
290 return new Rectangle($this->getWidth(), $this->getHeight());
291 }
292
296 abstract public function clear();
297
303 public function substituted(): bool
304 {
305 return $this->substituted;
306 }
307
308 protected function getMaxSize()
309 {
310 if (isset($this->options["maxSize"]) && is_array($this->options["maxSize"]))
311 {
312 return new Rectangle($this->options["maxSize"][0], $this->options["maxSize"][1]);
313 }
314
315 return null;
316 }
317
323 public function exceedsMaxSize(): bool
324 {
325 $info = $this->getInfo();
326 if (!$info)
327 {
328 return false;
329 }
330
331 $source = $info->toRectangle();
332 $maxSize = $this->getMaxSize();
333
334 if ($maxSize && $source->resize($maxSize, Image::RESIZE_PROPORTIONAL))
335 {
336 return true;
337 }
338
339 return false;
340 }
341
342 public function __destruct()
343 {
344 $this->clear();
345 }
346}
__construct($file=null, array $options=[])
Определения Engine.php:31
rotate($angle, Color $bgColor)
setFile($file)
Определения Engine.php:45
exceedsMaxSize()
Определения Engine.php:323
getInfo($flashEnabled=false)
Определения Engine.php:57
drawTextWatermark(TextWatermark $watermark)
save($file, $quality=95, $format=null)
loadWatermark(ImageWatermark $watermark)
Определения Engine.php:231
getDimensions()
Определения Engine.php:288
drawImageWatermark(ImageWatermark $watermark)
resize(Rectangle $source, Rectangle $destination)
static convertEncoding($data, $charsetFrom, $charsetTo)
Определения encoding.php:17
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$culture
Определения include.php:61
if($NS['step']==6) if( $NS[ 'step']==7) if(COption::GetOptionInt('main', 'disk_space', 0) > 0) $info
Определения backup.php:924
Определения action.php:3