‪TYPO3CMS  10.4
ImageInfo.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
22 
26 class ‪ImageInfo extends ‪FileInfo implements LoggerAwareInterface
27 {
28  use LoggerAwareTrait;
29 
33  protected ‪$imageSizes;
34 
40  public function ‪getWidth()
41  {
43  return ‪$imageSizes[0];
44  }
45 
51  public function ‪getHeight()
52  {
54  return ‪$imageSizes[1];
55  }
56 
63  protected function ‪getExifAwareImageSize(string $imageFile)
64  {
65  $size = false;
66  if (function_exists('getimagesize')) {
67  $size = @getimagesize($imageFile);
68  }
69  if ($size === false) {
70  return false;
71  }
72  [$width, $height] = $size;
73 
74  if (function_exists('exif_read_data')) {
75  $exif = @exif_read_data($imageFile);
76  // see: http://sylvana.net/jpegcrop/exif_orientation.html
77  if (isset($exif['Orientation']) && $exif['Orientation'] >= 5 && $exif['Orientation'] <= 8) {
78  return [$height, $width];
79  }
80  }
81 
82  return [$width, $height];
83  }
84 
88  protected function ‪getImageSizes()
89  {
90  if ($this->imageSizes === null) {
91  $this->imageSizes = $this->‪getExifAwareImageSize($this->getPathname());
92 
93  // Try SVG first as SVG size detection with IM/GM leads to an error output
94  if ($this->imageSizes === false && $this->‪getMimeType() === 'image/svg+xml') {
95  $this->imageSizes = $this->‪extractSvgImageSizes();
96  }
97  // Fallback to IM/GM identify
98  if ($this->imageSizes === false) {
99  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
100  $this->imageSizes = $graphicalFunctions->imageMagickIdentify($this->getPathname());
101  }
102 
103  // In case the image size could not be retrieved, log the incident as a warning.
104  if (empty($this->imageSizes)) {
105  $this->logger->warning('I could not retrieve the image size for file ' . $this->getPathname());
106  $this->imageSizes = [0, 0];
107  }
108  }
109  return ‪$this->imageSizes;
110  }
111 
118  protected function ‪extractSvgImageSizes()
119  {
120  $imagesSizes = [];
121 
122  $fileContent = file_get_contents($this->getPathname());
123  if ($fileContent === false) {
124  return false;
125  }
126  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
127  $previousValueOfEntityLoader = null;
128  if (PHP_MAJOR_VERSION < 8) {
129  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
130  }
131  $xml = simplexml_load_string($fileContent, \SimpleXMLElement::class, LIBXML_NOERROR | LIBXML_NOWARNING);
132  if (PHP_MAJOR_VERSION < 8) {
133  libxml_disable_entity_loader($previousValueOfEntityLoader);
134  }
135  // If something went wrong with simpleXml don't try to read information
136  if ($xml === false) {
137  return false;
138  }
139 
140  $xmlAttributes = $xml->attributes();
141 
142  // First check if width+height are set
143  if (!empty($xmlAttributes['width']) && !empty($xmlAttributes['height'])) {
144  $imagesSizes = [(int)$xmlAttributes['width'], (int)$xmlAttributes['height']];
145  } elseif (!empty($xmlAttributes['viewBox'])) {
146  // Fallback to viewBox
147  $viewBox = explode(' ', $xmlAttributes['viewBox']);
148  $imagesSizes = [(int)$viewBox[2], (int)$viewBox[3]];
149  } else {
150  // To not fail image processing, we just assume an SVG image dimension here
151  $imagesSizes = [64, 64];
152  }
153 
154  return $imagesSizes !== [] ? $imagesSizes : false;
155  }
156 }
‪TYPO3\CMS\Core\Type\File
Definition: FileInfo.php:16
‪TYPO3\CMS\Core\Type\File\ImageInfo\getImageSizes
‪array getImageSizes()
Definition: ImageInfo.php:87
‪TYPO3\CMS\Core\Type\File\ImageInfo\$imageSizes
‪array $imageSizes
Definition: ImageInfo.php:32
‪TYPO3\CMS\Core\Type\File\ImageInfo\extractSvgImageSizes
‪false array extractSvgImageSizes()
Definition: ImageInfo.php:117
‪TYPO3\CMS\Core\Type\File\ImageInfo\getExifAwareImageSize
‪array false getExifAwareImageSize(string $imageFile)
Definition: ImageInfo.php:62
‪TYPO3\CMS\Core\Type\File\FileInfo\getMimeType
‪string bool getMimeType()
Definition: FileInfo.php:34
‪TYPO3\CMS\Core\Type\File\ImageInfo\getWidth
‪int getWidth()
Definition: ImageInfo.php:39
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions
Definition: GraphicalFunctions.php:37
‪TYPO3\CMS\Core\Type\File\ImageInfo
Definition: ImageInfo.php:27
‪TYPO3\CMS\Core\Type\File\FileInfo
Definition: FileInfo.php:25
‪TYPO3\CMS\Core\Type\File\ImageInfo\getHeight
‪int getHeight()
Definition: ImageInfo.php:50
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46