[TASK] Deprecate GraphicalFunctions->init method
[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 * Gets the image size, considering the exif-rotation present in the file
57 *
58 * @param string $imageFile The image filepath
59 * @return array|false Returns an array where [0]/[1] is w/h.
60 */
61 protected function getExifAwareImageSize(string $imageFile)
62 {
63 $size = false;
64 if (function_exists('getimagesize')) {
65 $size = @getimagesize($imageFile);
66 }
67 if ($size === false) {
68 return false;
69 }
70 [$width, $height] = $size;
71
72 if (function_exists('exif_read_data')) {
73 $exif = @exif_read_data($imageFile);
74 // see: http://sylvana.net/jpegcrop/exif_orientation.html
75 if (isset($exif['Orientation']) && $exif['Orientation'] >= 5 && $exif['Orientation'] <= 8) {
76 return [$height, $width];
77 }
78 }
79
80 return [$width, $height];
81 }
82
83 /**
84 * @return array
85 */
86 protected function getImageSizes()
87 {
88 if ($this->imageSizes === null) {
89 $this->imageSizes = $this->getExifAwareImageSize($this->getPathname());
90
91 // Try SVG first as SVG size detection with IM/GM leads to an error output
92 if ($this->imageSizes === false && $this->getMimeType() === 'image/svg+xml') {
93 $this->imageSizes = $this->extractSvgImageSizes();
94 }
95 // Fallback to IM/GM identify
96 if ($this->imageSizes === false) {
97 $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
98 $this->imageSizes = $graphicalFunctions->imageMagickIdentify($this->getPathname());
99 }
100
101 // In case the image size could not be retrieved, log the incident as a warning.
102 if (empty($this->imageSizes)) {
103 $this->logger->warning('I could not retrieve the image size for file ' . $this->getPathname());
104 $this->imageSizes = [0, 0];
105 }
106 }
107 return $this->imageSizes;
108 }
109
110 /**
111 * Try to read SVG as XML file and
112 * find width and height
113 *
114 * @return false|array
115 */
116 protected function extractSvgImageSizes()
117 {
118 $imagesSizes = [];
119
120 $fileContent = file_get_contents($this->getPathname());
121 // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
122 $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
123 $xml = simplexml_load_string($fileContent, 'SimpleXMLElement', LIBXML_NOERROR);
124
125 // If something went wrong with simpleXml don't try to read information
126 if ($xml === false) {
127 return false;
128 }
129
130 libxml_disable_entity_loader($previousValueOfEntityLoader);
131 $xmlAttributes = $xml->attributes();
132
133 // First check if width+height are set
134 if (!empty($xmlAttributes['width']) && !empty($xmlAttributes['height'])) {
135 $imagesSizes = [(int)$xmlAttributes['width'], (int)$xmlAttributes['height']];
136 } elseif (!empty($xmlAttributes['viewBox'])) {
137 // Fallback to viewBox
138 $viewBox = explode(' ', $xmlAttributes['viewBox']);
139 $imagesSizes = [(int)$viewBox[2], (int)$viewBox[3]];
140 }
141
142 return $imagesSizes !== [] ? $imagesSizes : false;
143 }
144 }