9c42960e809209b0641ca30a45e4bf7b90708337
[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 // Try SVG first as SVG size detection with IM/GM leads to an error output
61 if ($this->imageSizes === false && $this->getMimeType() === 'image/svg+xml') {
62 $this->imageSizes = $this->extractSvgImageSizes();
63 }
64 // Fallback to IM/GM identify
65 if ($this->imageSizes === false) {
66 $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
67 $graphicalFunctions->init();
68 $this->imageSizes = $graphicalFunctions->imageMagickIdentify($this->getPathname());
69 }
70
71 // In case the image size could not be retrieved, log the incident as a warning.
72 if (empty($this->imageSizes)) {
73 $this->getLogger()->warning('I could not retrieve the image size for file ' . $this->getPathname());
74 $this->imageSizes = [0, 0];
75 }
76 }
77 return $this->imageSizes;
78 }
79
80 /**
81 * Try to read SVG as XML file and
82 * find width and height
83 *
84 * @return false|array
85 */
86 protected function extractSvgImageSizes()
87 {
88 $imagesSizes = [];
89
90 $fileContent = file_get_contents($this->getPathname());
91 // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
92 $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
93 $xml = simplexml_load_string($fileContent, 'SimpleXMLElement', LIBXML_NOERROR);
94
95 // If something went wrong with simpleXml don't try to read information
96 if ($xml === false) {
97 return false;
98 }
99
100 libxml_disable_entity_loader($previousValueOfEntityLoader);
101 $xmlAttributes = $xml->attributes();
102
103 // First check if width+height are set
104 if (!empty($xmlAttributes['width']) && !empty($xmlAttributes['height'])) {
105 $imagesSizes = [(int)$xmlAttributes['width'], (int)$xmlAttributes['height']];
106 } elseif (!empty($xmlAttributes['viewBox'])) {
107 // Fallback to viewBox
108 $viewBox = explode(' ', $xmlAttributes['viewBox']);
109 $imagesSizes = [(int)$viewBox[2], (int)$viewBox[3]];
110 }
111
112 return $imagesSizes !== [] ? $imagesSizes : false;
113 }
114
115 /**
116 * @return \TYPO3\CMS\Core\Log\Logger
117 */
118 protected function getLogger()
119 {
120 /** @var $loggerManager LogManager */
121 $loggerManager = GeneralUtility::makeInstance(LogManager::class);
122
123 return $loggerManager->getLogger(get_class($this));
124 }
125
126 /**
127 * @return GraphicalFunctions
128 */
129 protected function getGraphicalFunctions()
130 {
131 $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
132 $graphicalFunctions->init();
133
134 return $graphicalFunctions;
135 }
136 }