[BUGFIX] Add missing initialization of GraphicalFunctions
[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 Psr\Log\LoggerAwareInterface;
17 use Psr\Log\LoggerAwareTrait;
18 use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * A SPL FileInfo class providing information related to an image.
23 */
24 class ImageInfo extends FileInfo implements LoggerAwareInterface
25 {
26 use LoggerAwareTrait;
27
28 /**
29 * @var array
30 */
31 protected $imageSizes;
32
33 /**
34 * Returns the width of the Image.
35 *
36 * @return int
37 */
38 public function getWidth()
39 {
40 $imageSizes = $this->getImageSizes();
41 return $imageSizes[0];
42 }
43
44 /**
45 * Returns the height of the Image.
46 *
47 * @return int
48 */
49 public function getHeight()
50 {
51 $imageSizes = $this->getImageSizes();
52 return $imageSizes[1];
53 }
54
55 /**
56 * @return array
57 */
58 protected function getImageSizes()
59 {
60 if (is_null($this->imageSizes)) {
61 $this->imageSizes = getimagesize($this->getPathname());
62
63 // Fallback to IM identify
64 if ($this->imageSizes === false) {
65 $this->imageSizes = $this->getGraphicalFunctions()->imageMagickIdentify($this->getPathname());
66 }
67
68 // Extra fallback for SVG
69 if (empty($this->imageSizes) && $this->getMimeType() === 'image/svg+xml') {
70 $this->imageSizes = $this->extractSvgImageSizes();
71 }
72
73 // In case the image size could not be retrieved, log the incident as a warning.
74 if (empty($this->imageSizes)) {
75 $this->logger->warning('I could not retrieve the image size for file ' . $this->getPathname());
76 $this->imageSizes = [0, 0];
77 }
78 }
79 return $this->imageSizes;
80 }
81
82 /**
83 * Try to read SVG as XML file and
84 * find width and height
85 *
86 * @return FALSE|array
87 */
88 protected function extractSvgImageSizes()
89 {
90 $imagesSizes = [];
91
92 $fileContent = file_get_contents($this->getPathname());
93 // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
94 $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
95 $xml = simplexml_load_string($fileContent);
96 libxml_disable_entity_loader($previousValueOfEntityLoader);
97 $xmlAttributes = $xml->attributes();
98
99 // First check if width+height are set
100 if (!empty($xmlAttributes['width']) && !empty($xmlAttributes['height'])) {
101 $imagesSizes = [(int)$xmlAttributes['width'], (int)$xmlAttributes['height']];
102 } elseif (!empty($xmlAttributes['viewBox'])) {
103 // Fallback to viewBox
104 $viewBox = explode(' ', $xmlAttributes['viewBox']);
105 $imagesSizes = [(int)$viewBox[2], (int)$viewBox[3]];
106 }
107
108 return $imagesSizes !== [] ? $imagesSizes : false;
109 }
110
111 /**
112 * @return GraphicalFunctions
113 */
114 protected function getGraphicalFunctions()
115 {
116 static $graphicalFunctions = null;
117
118 if ($graphicalFunctions === null) {
119 $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
120 $graphicalFunctions->init();
121 }
122
123 return $graphicalFunctions;
124 }
125 }