TYPO3 CMS  TYPO3_8-7
ImageMagickFile.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Imaging;
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
22 
28 {
34  protected $filePath;
35 
41  protected $frame;
42 
48  protected $fileExists;
49 
55  protected $fileExtension;
56 
62  protected $mimeType;
63 
71  protected $mimeExtensions = [];
72 
80  protected $asArgument;
81 
87  protected $allowedExtensions = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'tif', 'tiff', 'bmp', 'pcx', 'tga', 'ico'];
88 
94  protected $deniedExtensions = ['epi', 'eps', 'eps2', 'eps3', 'epsf', 'epsi', 'ept', 'ept2', 'ept3', 'msl', 'ps', 'ps2', 'ps3'];
95 
103  protected $mimeTypeExtensionMap = [
104  'image/png' => 'png',
105  'image/jpeg' => 'jpg',
106  'image/gif' => 'gif',
107  'image/heic' => 'heic',
108  'image/heif' => 'heif',
109  'image/webp' => 'webp',
110  'image/svg' => 'svg',
111  'image/svg+xml' => 'svg',
112  'image/tiff' => 'tif',
113  'application/pdf' => 'pdf',
114  ];
115 
121  public static function fromFilePath(string $filePath, int $frame = null): self
122  {
124  static::class,
125  $filePath,
126  $frame
127  );
128  }
129 
135  public function __construct(string $filePath, int $frame = null)
136  {
137  $this->frame = $frame;
138  $this->fileExists = file_exists($filePath);
139  $this->filePath = $filePath;
140  $this->fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
141 
142  if ($this->fileExists) {
143  $fileInfo = $this->getFileInfo($filePath);
144  $this->mimeType = $fileInfo->getMimeType();
145  $this->mimeExtensions = $fileInfo->getMimeExtensions();
146  }
147 
148  $this->asArgument = $this->escape(
149  $this->resolvePrefix() . $this->filePath
150  . ($this->frame !== null ? '[' . $this->frame . ']' : '')
151  );
152  }
153 
157  public function __toString(): string
158  {
159  return $this->asArgument;
160  }
161 
173  protected function resolvePrefix(): string
174  {
175  $prefixExtension = null;
176  $fileExtension = strtolower($this->fileExtension);
177  if ($this->mimeType !== null && !empty($this->mimeTypeExtensionMap[$this->mimeType])) {
178  $prefixExtension = $this->mimeTypeExtensionMap[$this->mimeType];
179  } elseif (!empty($this->mimeExtensions) && strpos((string)$this->mimeType, 'image/') === 0) {
180  $prefixExtension = $this->mimeExtensions[0];
181  } elseif ($this->isInAllowedExtensions($fileExtension)) {
182  $prefixExtension = $fileExtension;
183  }
184  if ($prefixExtension !== null && !in_array(strtolower($prefixExtension), $this->deniedExtensions, true)) {
185  return $prefixExtension . ':';
186  }
187  throw new Exception(
188  sprintf(
189  'Unsupported file %s (%s)',
190  basename($this->filePath),
191  $this->mimeType ?? 'unknown'
192  ),
193  1550060977
194  );
195  }
196 
201  protected function escape(string $value): string
202  {
204  }
205 
210  protected function isInAllowedExtensions(string $extension): bool
211  {
212  return in_array($extension, $this->allowedExtensions, true);
213  }
214 
219  protected function getFileInfo(string $filePath): FileInfo
220  {
221  return GeneralUtility::makeInstance(FileInfo::class, $filePath);
222  }
223 }
static makeInstance($className,... $constructorArguments)
__construct(string $filePath, int $frame=null)
static fromFilePath(string $filePath, int $frame=null)