TYPO3  7.6
ImageInfo.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Type\File;
3 
20 
24 class ImageInfo extends FileInfo
25 {
29  protected $imageSizes;
30 
36  public function getWidth()
37  {
38  $imageSizes = $this->getImageSizes();
39  return $imageSizes[0];
40  }
41 
47  public function getHeight()
48  {
49  $imageSizes = $this->getImageSizes();
50  return $imageSizes[1];
51  }
52 
56  protected function getImageSizes()
57  {
58  if (is_null($this->imageSizes)) {
59  $this->imageSizes = getimagesize($this->getPathname());
60 
61  // Fallback to IM identify
62  if ($this->imageSizes === false) {
63  $this->imageSizes = $this->getGraphicalFunctions()->imageMagickIdentify($this->getPathname());
64  }
65 
66  // Extra fallback for SVG
67  if (empty($this->imageSizes) && $this->getMimeType() === 'image/svg+xml') {
68  $this->imageSizes = $this->extractSvgImageSizes();
69  }
70 
71  // In case the image size could not be retrieved, log the incident as a warning.
72  if (empty($this->imageSizes)) {
73  $this->getLogger()->warning('I could not retrieve the image size for file ' . $this->getPathname());
74  $this->imageSizes = array(0, 0);
75  }
76  }
77  return $this->imageSizes;
78  }
79 
86  protected function extractSvgImageSizes()
87  {
88  $imagesSizes = array();
89 
90  $xml = simplexml_load_file($this->getPathname());
91  $xmlAttributes = $xml->attributes();
92 
93  // First check if width+height are set
94  if (!empty($xmlAttributes['width']) && !empty($xmlAttributes['height'])) {
95  $imagesSizes = array((int)$xmlAttributes['width'], (int)$xmlAttributes['height']);
96 
97  // Fallback to viewBox
98  } elseif (!empty($xmlAttributes['viewBox'])) {
99  $viewBox = explode(' ', $xmlAttributes['viewBox']);
100  $imagesSizes = array((int)$viewBox[2], (int)$viewBox[3]);
101  }
102 
103  return $imagesSizes !== array() ? $imagesSizes : false;
104  }
105 
109  protected function getLogger()
110  {
112  $loggerManager = GeneralUtility::makeInstance(LogManager::class);
113 
114  return $loggerManager->getLogger(get_class($this));
115  }
116 
120  protected function getGraphicalFunctions()
121  {
122  static $graphicalFunctions = null;
123 
124  if ($graphicalFunctions === null) {
125  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
126  }
127 
128  return $graphicalFunctions;
129  }
130 }