TYPO3 CMS  TYPO3_8-7
ImageInfo.php
Go to the documentation of this file.
1 <?php
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  */
19 
23 class ImageInfo extends FileInfo
24 {
28  protected $imageSizes;
29 
35  public function getWidth()
36  {
37  $imageSizes = $this->getImageSizes();
38  return $imageSizes[0];
39  }
40 
46  public function getHeight()
47  {
48  $imageSizes = $this->getImageSizes();
49  return $imageSizes[1];
50  }
51 
55  protected function getImageSizes()
56  {
57  if ($this->imageSizes === null) {
58  $this->imageSizes = false;
59  if (function_exists('getimagesize')) {
60  $this->imageSizes = @getimagesize($this->getPathname());
61  }
62  // Try SVG first as SVG size detection with IM/GM leads to an error output
63  if ($this->imageSizes === false && $this->getMimeType() === 'image/svg+xml') {
64  $this->imageSizes = $this->extractSvgImageSizes();
65  }
66  // Fallback to IM/GM identify
67  if ($this->imageSizes === false) {
68  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
69  $graphicalFunctions->init();
70  $this->imageSizes = $graphicalFunctions->imageMagickIdentify($this->getPathname());
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->getLogger()->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 
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, 'SimpleXMLElement', LIBXML_NOERROR);
96 
97  // If something went wrong with simpleXml don't try to read information
98  if ($xml === false) {
99  return false;
100  }
101 
102  libxml_disable_entity_loader($previousValueOfEntityLoader);
103  $xmlAttributes = $xml->attributes();
104 
105  // First check if width+height are set
106  if (!empty($xmlAttributes['width']) && !empty($xmlAttributes['height'])) {
107  $imagesSizes = [(int)$xmlAttributes['width'], (int)$xmlAttributes['height']];
108  } elseif (!empty($xmlAttributes['viewBox'])) {
109  // Fallback to viewBox
110  $viewBox = explode(' ', $xmlAttributes['viewBox']);
111  $imagesSizes = [(int)$viewBox[2], (int)$viewBox[3]];
112  }
113 
114  return $imagesSizes !== [] ? $imagesSizes : false;
115  }
116 
120  protected function getLogger()
121  {
123  $loggerManager = GeneralUtility::makeInstance(LogManager::class);
124 
125  return $loggerManager->getLogger(get_class($this));
126  }
127 
131  protected function getGraphicalFunctions()
132  {
133  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
134  $graphicalFunctions->init();
135 
136  return $graphicalFunctions;
137  }
138 }
static makeInstance($className,... $constructorArguments)