‪TYPO3CMS  9.5
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  */
16 use Psr\Log\LoggerAwareInterface;
17 use Psr\Log\LoggerAwareTrait;
20 
24 class ‪ImageInfo extends ‪FileInfo implements LoggerAwareInterface
25 {
26  use LoggerAwareTrait;
27 
31  protected ‪$imageSizes;
32 
38  public function ‪getWidth()
39  {
41  return ‪$imageSizes[0];
42  }
43 
49  public function ‪getHeight()
50  {
52  return ‪$imageSizes[1];
53  }
54 
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 
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 
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 | LIBXML_NOWARNING);
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 }
‪TYPO3\CMS\Core\Type\File
Definition: FileInfo.php:2
‪TYPO3\CMS\Core\Type\File\ImageInfo\getImageSizes
‪array getImageSizes()
Definition: ImageInfo.php:85
‪TYPO3\CMS\Core\Type\File\ImageInfo\$imageSizes
‪array $imageSizes
Definition: ImageInfo.php:30
‪TYPO3\CMS\Core\Type\File\ImageInfo\extractSvgImageSizes
‪false array extractSvgImageSizes()
Definition: ImageInfo.php:115
‪TYPO3\CMS\Core\Type\File\ImageInfo\getExifAwareImageSize
‪array false getExifAwareImageSize(string $imageFile)
Definition: ImageInfo.php:60
‪TYPO3\CMS\Core\Type\File\FileInfo\getMimeType
‪string bool getMimeType()
Definition: FileInfo.php:33
‪TYPO3\CMS\Core\Type\File\ImageInfo\getWidth
‪int getWidth()
Definition: ImageInfo.php:37
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions
Definition: GraphicalFunctions.php:35
‪TYPO3\CMS\Core\Type\File\ImageInfo
Definition: ImageInfo.php:25
‪TYPO3\CMS\Core\Type\File\FileInfo
Definition: FileInfo.php:24
‪TYPO3\CMS\Core\Type\File\ImageInfo\getHeight
‪int getHeight()
Definition: ImageInfo.php:48
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45