‪TYPO3CMS  ‪main
ScalableVectorGraphicsContentObject.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 
25 {
31  public function ‪render($conf = []): string
32  {
33  $renderMode = $this->cObj->stdWrapValue('renderMode', $conf);
34 
35  if ($renderMode === 'inline') {
36  return $this->‪renderInline($conf);
37  }
38 
39  return $this->‪renderObject($conf);
40  }
41 
42  protected function ‪renderInline(array $conf): string
43  {
44  $src = $this->‪resolveAbsoluteSourcePath($conf);
45  [$width, $height, $isDefaultWidth, $isDefaultHeight] = $this->‪getDimensions($conf);
46 
47  $content = '';
48  if (file_exists($src)) {
49  $svgContent = (string)file_get_contents($src);
50  $svgContent = preg_replace('/<script[\s\S]*?>[\s\S]*?<\/script>/i', '', $svgContent) ?? '';
51  $svgElement = simplexml_load_string($svgContent);
52 
53  $domXml = dom_import_simplexml($svgElement);
54  if (!$isDefaultWidth) {
55  $domXml->setAttribute('width', $width);
56  }
57  if (!$isDefaultHeight) {
58  $domXml->setAttribute('height', $height);
59  }
60  // remove xml version tag
61  $content = $domXml->ownerDocument->saveXML($domXml->ownerDocument->documentElement);
62  } else {
63  $value = $this->cObj->stdWrapValue('value', $conf);
64  if (!empty($value)) {
65  $content = [];
66  $content[] = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' . (int)$width . '" height="' . (int)$height . '">';
67  $content[] = $value;
68  $content[] = '</svg>';
69  $content = implode(LF, $content);
70  }
71  }
72  if (isset($conf['stdWrap.'])) {
73  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
74  }
75  return $content;
76  }
77 
81  protected function ‪renderObject(array $conf): string
82  {
83  $src = $this->‪resolveAbsoluteSourcePath($conf);
84  [$width, $height] = $this->‪getDimensions($conf);
85 
86  $src = $src === '' ? null : ‪PathUtility::getAbsoluteWebPath($src);
87 
88  $content = [];
89  if ($src) {
90  $content[] = '<!--[if IE]>';
91  $content[] = ' <object src="' . htmlspecialchars($src) . '" classid="image/svg+xml" width="' . (int)$width . '" height="' . (int)$height . '">';
92  $content[] = '<![endif]-->';
93  $content[] = '<!--[if !IE]>-->';
94  $content[] = ' <object data="' . htmlspecialchars($src) . '" type="image/svg+xml" width="' . (int)$width . '" height="' . (int)$height . '">';
95  $content[] = '<!--<![endif]-->';
96  $content[] = '</object>';
97  }
98  $content = implode(LF, $content);
99  if (isset($conf['stdWrap.'])) {
100  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
101  }
102  return $content;
103  }
104 
105  protected function ‪resolveAbsoluteSourcePath(array $conf): string
106  {
107  $src = (string)$this->cObj->stdWrapValue('src', $conf);
108  return GeneralUtility::getFileAbsFileName($src);
109  }
110 
111  protected function ‪getDimensions(array $conf): array
112  {
113  $isDefaultWidth = false;
114  $isDefaultHeight = false;
115  $width = $this->cObj->stdWrapValue('width', $conf);
116  $height = $this->cObj->stdWrapValue('height', $conf);
117 
118  if (empty($width)) {
119  $isDefaultWidth = true;
120  $width = 600;
121  }
122  if (empty($height)) {
123  $isDefaultHeight = true;
124  $height = 400;
125  }
126 
127  return [$width, $height, $isDefaultWidth, $isDefaultHeight];
128  }
129 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\renderObject
‪renderObject(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:81
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:18
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\getDimensions
‪getDimensions(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:111
‪TYPO3\CMS\Core\Utility\PathUtility\getAbsoluteWebPath
‪static string getAbsoluteWebPath(string $targetPath, bool $prefixWithSitePath=true)
Definition: PathUtility.php:52
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject
Definition: ScalableVectorGraphicsContentObject.php:25
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\render
‪render($conf=[])
Definition: ScalableVectorGraphicsContentObject.php:31
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:31
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\renderInline
‪renderInline(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Frontend\ContentObject\ScalableVectorGraphicsContentObject\resolveAbsoluteSourcePath
‪resolveAbsoluteSourcePath(array $conf)
Definition: ScalableVectorGraphicsContentObject.php:105