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