‪TYPO3CMS  9.5
ImageMagickFile.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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  {
123  return GeneralUtility::makeInstance(
124  static::class,
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 }
‪TYPO3\CMS\Core\Imaging
Definition: Dimension.php:2
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\escape
‪string escape(string $value)
Definition: ImageMagickFile.php:191
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$fileExists
‪bool $fileExists
Definition: ImageMagickFile.php:45
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$deniedExtensions
‪string[] $deniedExtensions
Definition: ImageMagickFile.php:85
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$frame
‪int null $frame
Definition: ImageMagickFile.php:39
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$filePath
‪string $filePath
Definition: ImageMagickFile.php:33
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$mimeExtensions
‪string[] $mimeExtensions
Definition: ImageMagickFile.php:65
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\getFileInfo
‪FileInfo getFileInfo(string $filePath)
Definition: ImageMagickFile.php:209
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$mimeType
‪string $mimeType
Definition: ImageMagickFile.php:57
‪TYPO3\CMS\Core\Utility\CommandUtility\escapeShellArgument
‪static string escapeShellArgument($input)
Definition: CommandUtility.php:503
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\isInAllowedExtensions
‪bool isInAllowedExtensions(string $extension)
Definition: ImageMagickFile.php:200
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\__toString
‪string __toString()
Definition: ImageMagickFile.php:147
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\resolvePrefix
‪string resolvePrefix()
Definition: ImageMagickFile.php:163
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$allowedExtensions
‪string[] $allowedExtensions
Definition: ImageMagickFile.php:79
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\fromFilePath
‪static ImageMagickFile fromFilePath(string $filePath, int $frame=null)
Definition: ImageMagickFile.php:111
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$mimeTypeExtensionMap
‪string[] $mimeTypeExtensionMap
Definition: ImageMagickFile.php:93
‪TYPO3\CMS\Core\Imaging\ImageMagickFile
Definition: ImageMagickFile.php:28
‪TYPO3\CMS\Core\Type\File\FileInfo
Definition: FileInfo.php:24
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\__construct
‪__construct(string $filePath, int $frame=null)
Definition: ImageMagickFile.php:125
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Utility\CommandUtility
Definition: CommandUtility.php:48
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$fileExtension
‪string $fileExtension
Definition: ImageMagickFile.php:51
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$asArgument
‪string $asArgument
Definition: ImageMagickFile.php:73