[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Type / File / ImageInfo.php
1 <?php
2 namespace TYPO3\CMS\Core\Type\File;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
17 use TYPO3\CMS\Core\Log\LogManager;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * A SPL FileInfo class providing information related to an image.
22 */
23 class ImageInfo extends FileInfo
24 {
25 /**
26 * @var array
27 */
28 protected $imageSizes;
29
30 /**
31 * Returns the width of the Image.
32 *
33 * @return int
34 */
35 public function getWidth()
36 {
37 $imageSizes = $this->getImageSizes();
38 return $imageSizes[0];
39 }
40
41 /**
42 * Returns the height of the Image.
43 *
44 * @return int
45 */
46 public function getHeight()
47 {
48 $imageSizes = $this->getImageSizes();
49 return $imageSizes[1];
50 }
51
52 /**
53 * @return array
54 */
55 protected function getImageSizes()
56 {
57 if (is_null($this->imageSizes)) {
58 $this->imageSizes = getimagesize($this->getPathname());
59
60 // Fallback to IM identify
61 if ($this->imageSizes === false) {
62 $this->imageSizes = $this->getGraphicalFunctions()->imageMagickIdentify($this->getPathname());
63 }
64
65 // Extra fallback for SVG
66 if (empty($this->imageSizes) && $this->getMimeType() === 'image/svg+xml') {
67 $this->imageSizes = $this->extractSvgImageSizes();
68 }
69
70 // In case the image size could not be retrieved, log the incident as a warning.
71 if (empty($this->imageSizes)) {
72 $this->getLogger()->warning('I could not retrieve the image size for file ' . $this->getPathname());
73 $this->imageSizes = [0, 0];
74 }
75 }
76 return $this->imageSizes;
77 }
78
79 /**
80 * Try to read SVG as XML file and
81 * find width and height
82 *
83 * @return false|array
84 */
85 protected function extractSvgImageSizes()
86 {
87 $imagesSizes = [];
88
89 $fileContent = file_get_contents($this->getPathname());
90 // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
91 $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
92 $xml = simplexml_load_string($fileContent);
93 libxml_disable_entity_loader($previousValueOfEntityLoader);
94 $xmlAttributes = $xml->attributes();
95
96 // First check if width+height are set
97 if (!empty($xmlAttributes['width']) && !empty($xmlAttributes['height'])) {
98 $imagesSizes = [(int)$xmlAttributes['width'], (int)$xmlAttributes['height']];
99 } elseif (!empty($xmlAttributes['viewBox'])) {
100 // Fallback to viewBox
101 $viewBox = explode(' ', $xmlAttributes['viewBox']);
102 $imagesSizes = [(int)$viewBox[2], (int)$viewBox[3]];
103 }
104
105 return $imagesSizes !== [] ? $imagesSizes : false;
106 }
107
108 /**
109 * @return \TYPO3\CMS\Core\Log\Logger
110 */
111 protected function getLogger()
112 {
113 /** @var $loggerManager LogManager */
114 $loggerManager = GeneralUtility::makeInstance(LogManager::class);
115
116 return $loggerManager->getLogger(get_class($this));
117 }
118
119 /**
120 * @return GraphicalFunctions
121 */
122 protected function getGraphicalFunctions()
123 {
124 static $graphicalFunctions = null;
125
126 if ($graphicalFunctions === null) {
127 $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
128 }
129
130 return $graphicalFunctions;
131 }
132 }