TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
ImageInfo.php
Go to the documentation of this file.
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  */
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 (is_null($this->imageSizes)) {
58  $this->imageSizes = getimagesize($this->getPathname());
59 
60  // Fallback to IM identify
61  if ($this->imageSizes === false) {
62  $this->imageSizes = $this->getGraphicalFunctions()->imageMagickIdentify($this->getPathname());
63  }
64 
65  // Extra fallback for SVG
66  if (empty($this->imageSizes) && $this->getMimeType() === 'image/svg+xml') {
67  $this->imageSizes = $this->extractSvgImageSizes();
68  }
69 
70  // In case the image size could not be retrieved, log the incident as a warning.
71  if (empty($this->imageSizes)) {
72  $this->getLogger()->warning('I could not retrieve the image size for file ' . $this->getPathname());
73  $this->imageSizes = [0, 0];
74  }
75  }
76  return $this->imageSizes;
77  }
78 
85  protected function extractSvgImageSizes()
86  {
87  $imagesSizes = [];
88 
89  $fileContent = file_get_contents($this->getPathname());
90  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
91  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
92  $xml = simplexml_load_string($fileContent);
93  libxml_disable_entity_loader($previousValueOfEntityLoader);
94  $xmlAttributes = $xml->attributes();
95 
96  // First check if width+height are set
97  if (!empty($xmlAttributes['width']) && !empty($xmlAttributes['height'])) {
98  $imagesSizes = [(int)$xmlAttributes['width'], (int)$xmlAttributes['height']];
99 
100  // Fallback to viewBox
101  } elseif (!empty($xmlAttributes['viewBox'])) {
102  $viewBox = explode(' ', $xmlAttributes['viewBox']);
103  $imagesSizes = [(int)$viewBox[2], (int)$viewBox[3]];
104  }
105 
106  return $imagesSizes !== [] ? $imagesSizes : false;
107  }
108 
112  protected function getLogger()
113  {
115  $loggerManager = GeneralUtility::makeInstance(LogManager::class);
116 
117  return $loggerManager->getLogger(get_class($this));
118  }
119 
123  protected function getGraphicalFunctions()
124  {
125  static $graphicalFunctions = null;
126 
127  if ($graphicalFunctions === null) {
128  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
129  }
130 
131  return $graphicalFunctions;
132  }
133 }
static makeInstance($className,...$constructorArguments)