composer package updates
[openemr.git] / vendor / mpdf / mpdf / src / Tag / Img.php
blobc3cdc216e2ee213e09f7c7ae92d8c725baccdca6
1 <?php
3 namespace Mpdf\Tag;
5 use Mpdf\Mpdf;
7 class Img extends Tag
10 public function open($attr, &$ahtml, &$ihtml)
12 $this->mpdf->ignorefollowingspaces = false;
13 $objattr = [];
14 $objattr['margin_top'] = 0;
15 $objattr['margin_bottom'] = 0;
16 $objattr['margin_left'] = 0;
17 $objattr['margin_right'] = 0;
18 $objattr['padding_top'] = 0;
19 $objattr['padding_bottom'] = 0;
20 $objattr['padding_left'] = 0;
21 $objattr['padding_right'] = 0;
22 $objattr['width'] = 0;
23 $objattr['height'] = 0;
24 $objattr['border_top']['w'] = 0;
25 $objattr['border_bottom']['w'] = 0;
26 $objattr['border_left']['w'] = 0;
27 $objattr['border_right']['w'] = 0;
28 if (isset($attr['SRC'])) {
29 $srcpath = $attr['SRC'];
30 $orig_srcpath = (isset($attr['ORIG_SRC']) ? $attr['ORIG_SRC'] : '');
31 $properties = $this->cssManager->MergeCSS('', 'IMG', $attr);
32 if (isset($properties ['DISPLAY']) && strtolower($properties ['DISPLAY']) === 'none') {
33 return;
35 if (isset($properties['Z-INDEX']) && $this->mpdf->current_layer == 0) {
36 $v = (int) $properties['Z-INDEX'];
37 if ($v > 0) {
38 $objattr['z-index'] = $v;
42 $objattr['visibility'] = 'visible';
43 if (isset($properties['VISIBILITY'])) {
44 $v = strtolower($properties['VISIBILITY']);
45 if (($v === 'hidden' || $v === 'printonly' || $v === 'screenonly') && $this->mpdf->visibility === 'visible') {
46 $objattr['visibility'] = $v;
50 // VSPACE and HSPACE converted to margins in MergeCSS
51 if (isset($properties['MARGIN-TOP'])) {
52 $objattr['margin_top'] = $this->sizeConverter->convert(
53 $properties['MARGIN-TOP'],
54 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
55 $this->mpdf->FontSize,
56 false
59 if (isset($properties['MARGIN-BOTTOM'])) {
60 $objattr['margin_bottom'] = $this->sizeConverter->convert(
61 $properties['MARGIN-BOTTOM'],
62 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
63 $this->mpdf->FontSize,
64 false
67 if (isset($properties['MARGIN-LEFT'])) {
68 $objattr['margin_left'] = $this->sizeConverter->convert(
69 $properties['MARGIN-LEFT'],
70 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
71 $this->mpdf->FontSize,
72 false
75 if (isset($properties['MARGIN-RIGHT'])) {
76 $objattr['margin_right'] = $this->sizeConverter->convert(
77 $properties['MARGIN-RIGHT'],
78 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
79 $this->mpdf->FontSize,
80 false
84 if (isset($properties['PADDING-TOP'])) {
85 $objattr['padding_top'] = $this->sizeConverter->convert(
86 $properties['PADDING-TOP'],
87 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
88 $this->mpdf->FontSize,
89 false
92 if (isset($properties['PADDING-BOTTOM'])) {
93 $objattr['padding_bottom'] = $this->sizeConverter->convert(
94 $properties['PADDING-BOTTOM'],
95 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
96 $this->mpdf->FontSize,
97 false
100 if (isset($properties['PADDING-LEFT'])) {
101 $objattr['padding_left'] = $this->sizeConverter->convert(
102 $properties['PADDING-LEFT'],
103 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
104 $this->mpdf->FontSize,
105 false
108 if (isset($properties['PADDING-RIGHT'])) {
109 $objattr['padding_right'] = $this->sizeConverter->convert(
110 $properties['PADDING-RIGHT'],
111 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
112 $this->mpdf->FontSize,
113 false
117 if (isset($properties['BORDER-TOP'])) {
118 $objattr['border_top'] = $this->mpdf->border_details($properties['BORDER-TOP']);
120 if (isset($properties['BORDER-BOTTOM'])) {
121 $objattr['border_bottom'] = $this->mpdf->border_details($properties['BORDER-BOTTOM']);
123 if (isset($properties['BORDER-LEFT'])) {
124 $objattr['border_left'] = $this->mpdf->border_details($properties['BORDER-LEFT']);
126 if (isset($properties['BORDER-RIGHT'])) {
127 $objattr['border_right'] = $this->mpdf->border_details($properties['BORDER-RIGHT']);
130 if (isset($properties['VERTICAL-ALIGN'])) {
131 $objattr['vertical-align'] = self::ALIGN[strtolower($properties['VERTICAL-ALIGN'])];
133 $w = 0;
134 $h = 0;
135 if (isset($properties['WIDTH'])) {
136 $w = $this->sizeConverter->convert(
137 $properties['WIDTH'],
138 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
139 $this->mpdf->FontSize,
140 false
142 } elseif (isset($attr['WIDTH'])) {
143 $w = $this->sizeConverter->convert(
144 $attr['WIDTH'],
145 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
146 $this->mpdf->FontSize,
147 false
150 if (isset($properties['HEIGHT'])) {
151 $h = $this->sizeConverter->convert(
152 $properties['HEIGHT'],
153 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
154 $this->mpdf->FontSize,
155 false
157 } elseif (isset($attr['HEIGHT'])) {
158 $h = $this->sizeConverter->convert(
159 $attr['HEIGHT'],
160 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
161 $this->mpdf->FontSize,
162 false
165 $maxw = $maxh = $minw = $minh = false;
166 if (isset($properties['MAX-WIDTH'])) {
167 $maxw = $this->sizeConverter->convert(
168 $properties['MAX-WIDTH'],
169 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
170 $this->mpdf->FontSize,
171 false
173 } elseif (isset($attr['MAX-WIDTH'])) {
174 $maxw = $this->sizeConverter->convert(
175 $attr['MAX-WIDTH'],
176 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
177 $this->mpdf->FontSize,
178 false
181 if (isset($properties['MAX-HEIGHT'])) {
182 $maxh = $this->sizeConverter->convert(
183 $properties['MAX-HEIGHT'],
184 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
185 $this->mpdf->FontSize,
186 false
188 } elseif (isset($attr['MAX-HEIGHT'])) {
189 $maxh = $this->sizeConverter->convert(
190 $attr['MAX-HEIGHT'],
191 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
192 $this->mpdf->FontSize,
193 false
196 if (isset($properties['MIN-WIDTH'])) {
197 $minw = $this->sizeConverter->convert(
198 $properties['MIN-WIDTH'],
199 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
200 $this->mpdf->FontSize,
201 false
203 } elseif (isset($attr['MIN-WIDTH'])) {
204 $minw = $this->sizeConverter->convert(
205 $attr['MIN-WIDTH'],
206 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
207 $this->mpdf->FontSize,
208 false
211 if (isset($properties['MIN-HEIGHT'])) {
212 $minh = $this->sizeConverter->convert(
213 $properties['MIN-HEIGHT'],
214 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
215 $this->mpdf->FontSize,
216 false
218 } elseif (isset($attr['MIN-HEIGHT'])) {
219 $minh = $this->sizeConverter->convert(
220 $attr['MIN-HEIGHT'],
221 $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'],
222 $this->mpdf->FontSize,
223 false
227 if (isset($properties['OPACITY']) && $properties['OPACITY'] > 0 && $properties['OPACITY'] <= 1) {
228 $objattr['opacity'] = $properties['OPACITY'];
230 if ($this->mpdf->HREF) {
231 if (strpos($this->mpdf->HREF, '.') === false && strpos($this->mpdf->HREF, '@') !== 0) {
232 $href = $this->mpdf->HREF;
233 while (array_key_exists($href, $this->mpdf->internallink)) {
234 $href = '#' . $href;
236 $this->mpdf->internallink[$href] = $this->mpdf->AddLink();
237 $objattr['link'] = $this->mpdf->internallink[$href];
238 } else {
239 $objattr['link'] = $this->mpdf->HREF;
242 $extraheight = $objattr['padding_top'] + $objattr['padding_bottom'] + $objattr['margin_top']
243 + $objattr['margin_bottom'] + $objattr['border_top']['w'] + $objattr['border_bottom']['w'];
245 $extrawidth = $objattr['padding_left'] + $objattr['padding_right'] + $objattr['margin_left']
246 + $objattr['margin_right'] + $objattr['border_left']['w'] + $objattr['border_right']['w'];
248 // mPDF 5.7.3 TRANSFORMS
249 if (isset($properties['BACKGROUND-COLOR']) && $properties['BACKGROUND-COLOR'] != '') {
250 $objattr['bgcolor'] = $this->colorConverter->convert($properties['BACKGROUND-COLOR'], $this->mpdf->PDFAXwarnings);
253 /* -- BACKGROUNDS -- */
254 if (isset($properties['GRADIENT-MASK']) && preg_match('/(-moz-)*(repeating-)*(linear|radial)-gradient/', $properties['GRADIENT-MASK'])) {
255 $objattr['GRADIENT-MASK'] = $properties['GRADIENT-MASK'];
257 /* -- END BACKGROUNDS -- */
259 // mPDF 6
260 $interpolation = false;
261 if (!empty($properties['IMAGE-RENDERING'])) {
262 $interpolation = false;
263 if (strtolower($properties['IMAGE-RENDERING']) === 'crisp-edges') {
264 $interpolation = false;
265 } elseif (strtolower($properties['IMAGE-RENDERING']) === 'optimizequality') {
266 $interpolation = true;
267 } elseif (strtolower($properties['IMAGE-RENDERING']) === 'smooth') {
268 $interpolation = true;
269 } elseif (strtolower($properties['IMAGE-RENDERING']) === 'auto') {
270 $interpolation = $this->mpdf->interpolateImages;
272 $info['interpolation'] = $interpolation;
275 // Image file
276 $info = $this->imageProcessor->getImage($srcpath, true, true, $orig_srcpath, $interpolation); // mPDF 6
277 if (!$info) {
278 $info = $this->imageProcessor->getImage($this->mpdf->noImageFile);
279 if ($info) {
280 $srcpath = $this->mpdf->noImageFile;
281 $w = ($info['w'] * (25.4 / $this->mpdf->dpi));
282 $h = ($info['h'] * (25.4 / $this->mpdf->dpi));
285 if (!$info) {
286 return;
289 $image_orientation = 0;
290 if (isset($attr['ROTATE'])) {
291 $image_orientation = $attr['ROTATE'];
292 } elseif (isset($properties['IMAGE-ORIENTATION'])) {
293 $image_orientation = $properties['IMAGE-ORIENTATION'];
295 if ($image_orientation) {
296 if ($image_orientation == 90 || $image_orientation == -90 || $image_orientation == 270) {
297 $tmpw = $info['w'];
298 $info['w'] = $info['h'];
299 $info['h'] = $tmpw;
301 $objattr['ROTATE'] = $image_orientation;
304 $objattr['file'] = $srcpath;
305 //Default width and height calculation if needed
306 if ($w == 0 && $h == 0) {
307 /* -- IMAGES-WMF -- */
308 if ($info['type'] === 'wmf') {
309 // WMF units are twips (1/20pt)
310 // divide by 20 to get points
311 // divide by k to get user units
312 $w = abs($info['w']) / (20 * Mpdf::SCALE);
313 $h = abs($info['h']) / (20 * Mpdf::SCALE);
314 } else { /* -- END IMAGES-WMF -- */
315 if ($info['type'] === 'svg') {
316 // SVG units are pixels
317 $w = abs($info['w']) / Mpdf::SCALE;
318 $h = abs($info['h']) / Mpdf::SCALE;
319 } else {
320 //Put image at default image dpi
321 $w = ($info['w'] / Mpdf::SCALE) * (72 / $this->mpdf->img_dpi);
322 $h = ($info['h'] / Mpdf::SCALE) * (72 / $this->mpdf->img_dpi);
325 if (isset($properties['IMAGE-RESOLUTION'])) {
326 if (preg_match('/from-image/i', $properties['IMAGE-RESOLUTION']) && isset($info['set-dpi']) && $info['set-dpi'] > 0) {
327 $w *= $this->mpdf->img_dpi / $info['set-dpi'];
328 $h *= $this->mpdf->img_dpi / $info['set-dpi'];
329 } elseif (preg_match('/(\d+)dpi/i', $properties['IMAGE-RESOLUTION'], $m)) {
330 $dpi = $m[1];
331 if ($dpi > 0) {
332 $w *= $this->mpdf->img_dpi / $dpi;
333 $h *= $this->mpdf->img_dpi / $dpi;
338 // IF WIDTH OR HEIGHT SPECIFIED
339 if ($w == 0) {
340 $w = $info['h'] ? abs($h * $info['w'] / $info['h']) : INF;
343 if ($h == 0) {
344 $h = $info['w'] ? abs($w * $info['h'] / $info['w']) : INF;
347 if ($minw && $w < $minw) {
348 $w = $minw;
349 $h = $info['w'] ? abs($w * $info['h'] / $info['w']) : INF;
351 if ($maxw && $w > $maxw) {
352 $w = $maxw;
353 $h = $info['w'] ? abs($w * $info['h'] / $info['w']) : INF;
355 if ($minh && $h < $minh) {
356 $h = $minh;
357 $w = $info['h'] ? abs($h * $info['w'] / $info['h']) : INF;
359 if ($maxh && $h > $maxh) {
360 $h = $maxh;
361 $w = $info['h'] ? abs($h * $info['w'] / $info['h']) : INF;
364 // Resize to maximum dimensions of page
365 $maxWidth = $this->mpdf->blk[$this->mpdf->blklvl]['inner_width'];
366 $maxHeight = $this->mpdf->h - ($this->mpdf->tMargin + $this->mpdf->bMargin + 1);
367 if ($this->mpdf->fullImageHeight) {
368 $maxHeight = $this->mpdf->fullImageHeight;
370 if (($w + $extrawidth) > ($maxWidth + 0.0001)) { // mPDF 5.7.4 0.0001 to allow for rounding errors when w==maxWidth
371 $w = $maxWidth - $extrawidth;
372 $h = abs($w * $info['h'] / $info['w']);
375 if ($h + $extraheight > $maxHeight) {
376 $h = $maxHeight - $extraheight;
377 $w = abs($h * $info['w'] / $info['h']);
379 $objattr['type'] = 'image';
380 $objattr['itype'] = $info['type'];
382 $objattr['orig_h'] = $info['h'];
383 $objattr['orig_w'] = $info['w'];
384 /* -- IMAGES-WMF -- */
385 if ($info['type'] === 'wmf') {
386 $objattr['wmf_x'] = $info['x'];
387 $objattr['wmf_y'] = $info['y'];
388 } else { /* -- END IMAGES-WMF -- */
389 if ($info['type'] === 'svg') {
390 $objattr['wmf_x'] = $info['x'];
391 $objattr['wmf_y'] = $info['y'];
394 $objattr['height'] = $h + $extraheight;
395 $objattr['width'] = $w + $extrawidth;
396 $objattr['image_height'] = $h;
397 $objattr['image_width'] = $w;
398 /* -- CSS-IMAGE-FLOAT -- */
399 if (!$this->mpdf->ColActive && !$this->mpdf->tableLevel && !$this->mpdf->listlvl && !$this->mpdf->kwt) {
400 if (isset($properties['FLOAT']) && (strtoupper($properties['FLOAT']) === 'RIGHT' || strtoupper($properties['FLOAT']) === 'LEFT')) {
401 $objattr['float'] = strtoupper(substr($properties['FLOAT'], 0, 1));
404 /* -- END CSS-IMAGE-FLOAT -- */
405 // mPDF 5.7.3 TRANSFORMS
406 if (isset($properties['TRANSFORM']) && !$this->mpdf->ColActive && !$this->mpdf->kwt) {
407 $objattr['transform'] = $properties['TRANSFORM'];
410 $e = "\xbb\xa4\xactype=image,objattr=" . serialize($objattr) . "\xbb\xa4\xac";
412 /* -- TABLES -- */
413 // Output it to buffers
414 if ($this->mpdf->tableLevel) {
415 $this->mpdf->_saveCellTextBuffer($e, $this->mpdf->HREF);
416 $this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['s'] += $objattr['width'];
417 } else {
418 /* -- END TABLES -- */
419 $this->mpdf->_saveTextBuffer($e, $this->mpdf->HREF);
420 } // *TABLES*
421 /* -- ANNOTATIONS -- */
422 if ($this->mpdf->title2annots && isset($attr['TITLE'])) {
423 $objattr = [];
424 $objattr['margin_top'] = 0;
425 $objattr['margin_bottom'] = 0;
426 $objattr['margin_left'] = 0;
427 $objattr['margin_right'] = 0;
428 $objattr['width'] = 0;
429 $objattr['height'] = 0;
430 $objattr['border_top']['w'] = 0;
431 $objattr['border_bottom']['w'] = 0;
432 $objattr['border_left']['w'] = 0;
433 $objattr['border_right']['w'] = 0;
434 $objattr['CONTENT'] = $attr['TITLE'];
435 $objattr['type'] = 'annot';
436 $objattr['POS-X'] = 0;
437 $objattr['POS-Y'] = 0;
438 $objattr['ICON'] = 'Comment';
439 $objattr['AUTHOR'] = '';
440 $objattr['SUBJECT'] = '';
441 $objattr['OPACITY'] = $this->mpdf->annotOpacity;
442 $objattr['COLOR'] = $this->colorConverter->convert('yellow', $this->mpdf->PDFAXwarnings);
443 $e = "\xbb\xa4\xactype=annot,objattr=" . serialize($objattr) . "\xbb\xa4\xac";
444 if ($this->mpdf->tableLevel) { // *TABLES*
445 $this->mpdf->cell[$this->mpdf->row][$this->mpdf->col]['textbuffer'][] = [$e]; // *TABLES*
446 } // *TABLES*
447 else { // *TABLES*
448 $this->mpdf->textbuffer[] = [$e];
449 } // *TABLES*
451 /* -- END ANNOTATIONS -- */
455 public function close(&$ahtml, &$ihtml)