‪TYPO3CMS  10.4
ImageMagickFile.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
24 
30 {
36  protected ‪$filePath;
37 
43  protected ‪$frame;
44 
50  protected ‪$fileExists;
51 
57  protected ‪$fileExtension;
58 
64  protected ‪$mimeType;
65 
73  protected ‪$mimeExtensions = [];
74 
82  protected ‪$asArgument;
83 
89  protected ‪$allowedExtensions = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'tif', 'tiff', 'bmp', 'pcx', 'tga', 'ico'];
90 
96  protected ‪$deniedExtensions = ['epi', 'eps', 'eps2', 'eps3', 'epsf', 'epsi', 'ept', 'ept2', 'ept3', 'msl', 'ps', 'ps2', 'ps3'];
97 
105  protected ‪$mimeTypeExtensionMap = [
106  'image/png' => 'png',
107  'image/jpeg' => 'jpg',
108  'image/gif' => 'gif',
109  'image/heic' => 'heic',
110  'image/heif' => 'heif',
111  'image/webp' => 'webp',
112  'image/svg' => 'svg',
113  'image/svg+xml' => 'svg',
114  'image/tiff' => 'tif',
115  'application/pdf' => 'pdf',
116  ];
117 
123  public static function ‪fromFilePath(string ‪$filePath, int ‪$frame = null): self
124  {
125  return GeneralUtility::makeInstance(
126  static::class,
128  ‪$frame
129  );
130  }
131 
137  public function ‪__construct(string ‪$filePath, int ‪$frame = null)
138  {
139  $this->frame = ‪$frame;
140  $this->fileExists = file_exists(‪$filePath);
141  $this->filePath = ‪$filePath;
142  $this->fileExtension = pathinfo(‪$filePath, PATHINFO_EXTENSION);
143 
144  if ($this->fileExists) {
145  $fileInfo = $this->‪getFileInfo($filePath);
146  $this->mimeType = $fileInfo->getMimeType();
147  $this->mimeExtensions = $fileInfo->getMimeExtensions();
148  }
149 
150  $this->asArgument = $this->‪escape(
151  $this->‪resolvePrefix() . $this->filePath
152  . ($this->frame !== null ? '[' . $this->frame . ']' : '')
153  );
154  }
155 
159  public function ‪__toString(): string
160  {
161  return ‪$this->asArgument;
162  }
163 
175  protected function ‪resolvePrefix(): string
176  {
177  $prefixExtension = null;
178  ‪$fileExtension = strtolower($this->fileExtension);
179  if ($this->mimeType !== null && !empty($this->mimeTypeExtensionMap[$this->mimeType])) {
180  $prefixExtension = $this->mimeTypeExtensionMap[‪$this->mimeType];
181  } elseif (!empty($this->mimeExtensions) && strpos((string)$this->mimeType, 'image/') === 0) {
182  $prefixExtension = $this->mimeExtensions[0];
183  } elseif ($this->‪isInAllowedExtensions(‪$fileExtension)) {
184  $prefixExtension = ‪$fileExtension;
185  }
186  if ($prefixExtension !== null && !in_array(strtolower($prefixExtension), $this->deniedExtensions, true)) {
187  return $prefixExtension . ':';
188  }
189  throw new Exception(
190  sprintf(
191  'Unsupported file %s (%s)',
192  basename($this->filePath),
193  $this->mimeType ?? 'unknown'
194  ),
195  1550060977
196  );
197  }
198 
203  protected function ‪escape(string $value): string
204  {
206  }
207 
212  protected function ‪isInAllowedExtensions(string $extension): bool
213  {
214  return in_array($extension, $this->allowedExtensions, true);
215  }
216 
221  protected function ‪getFileInfo(string ‪$filePath): FileInfo
222  {
223  return GeneralUtility::makeInstance(FileInfo::class, ‪$filePath);
224  }
225 }
‪TYPO3\CMS\Core\Imaging
Definition: Dimension.php:16
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\escape
‪string escape(string $value)
Definition: ImageMagickFile.php:193
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$fileExists
‪bool $fileExists
Definition: ImageMagickFile.php:47
‪TYPO3\CMS\Core\Exception
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$deniedExtensions
‪string[] $deniedExtensions
Definition: ImageMagickFile.php:87
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$frame
‪int null $frame
Definition: ImageMagickFile.php:41
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$filePath
‪string $filePath
Definition: ImageMagickFile.php:35
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$mimeExtensions
‪string[] $mimeExtensions
Definition: ImageMagickFile.php:67
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\getFileInfo
‪FileInfo getFileInfo(string $filePath)
Definition: ImageMagickFile.php:211
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$mimeType
‪string $mimeType
Definition: ImageMagickFile.php:59
‪TYPO3\CMS\Core\Utility\CommandUtility\escapeShellArgument
‪static string escapeShellArgument($input)
Definition: CommandUtility.php:504
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\isInAllowedExtensions
‪bool isInAllowedExtensions(string $extension)
Definition: ImageMagickFile.php:202
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\__toString
‪string __toString()
Definition: ImageMagickFile.php:149
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\resolvePrefix
‪string resolvePrefix()
Definition: ImageMagickFile.php:165
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$allowedExtensions
‪string[] $allowedExtensions
Definition: ImageMagickFile.php:81
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\fromFilePath
‪static ImageMagickFile fromFilePath(string $filePath, int $frame=null)
Definition: ImageMagickFile.php:113
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$mimeTypeExtensionMap
‪string[] $mimeTypeExtensionMap
Definition: ImageMagickFile.php:95
‪TYPO3\CMS\Core\Imaging\ImageMagickFile
Definition: ImageMagickFile.php:30
‪TYPO3\CMS\Core\Type\File\FileInfo
Definition: FileInfo.php:25
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\__construct
‪__construct(string $filePath, int $frame=null)
Definition: ImageMagickFile.php:127
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\CommandUtility
Definition: CommandUtility.php:49
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$fileExtension
‪string $fileExtension
Definition: ImageMagickFile.php:53
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$asArgument
‪string $asArgument
Definition: ImageMagickFile.php:75