‪TYPO3CMS  ‪main
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 
121  public static function ‪fromFilePath(string ‪$filePath, int ‪$frame = null): self
122  {
123  return GeneralUtility::makeInstance(
124  static::class,
125  ‪$filePath,
126  ‪$frame
127  );
128  }
129 
134  public function ‪__construct(string ‪$filePath, int ‪$frame = null)
135  {
136  $this->frame = ‪$frame;
137  $this->fileExists = file_exists(‪$filePath);
138  $this->filePath = ‪$filePath;
139  $this->fileExtension = pathinfo(‪$filePath, PATHINFO_EXTENSION);
140 
141  if ($this->fileExists) {
142  $fileInfo = $this->‪getFileInfo($filePath);
143  $this->mimeType = $fileInfo->getMimeType() ?: null;
144  $this->mimeExtensions = $fileInfo->getMimeExtensions();
145  }
146 
147  $this->asArgument = $this->‪escape(
148  $this->‪resolvePrefix() . $this->filePath
149  . ($this->frame !== null ? '[' . $this->frame . ']' : '')
150  );
151  }
152 
153  public function ‪__toString(): string
154  {
155  return ‪$this->asArgument;
156  }
157 
168  protected function ‪resolvePrefix(): string
169  {
170  $prefixExtension = null;
171  ‪$fileExtension = strtolower($this->fileExtension);
172  if ($this->mimeType !== null && !empty($this->mimeTypeExtensionMap[$this->mimeType])) {
173  $prefixExtension = $this->mimeTypeExtensionMap[‪$this->mimeType];
174  } elseif (!empty($this->mimeExtensions) && str_starts_with((string)$this->mimeType, 'image/')) {
175  $prefixExtension = $this->mimeExtensions[0];
176  } elseif ($this->‪isInAllowedExtensions(‪$fileExtension)) {
177  $prefixExtension = ‪$fileExtension;
178  }
179  if ($prefixExtension !== null && !in_array(strtolower($prefixExtension), $this->deniedExtensions, true)) {
180  return $prefixExtension . ':';
181  }
183  sprintf(
184  'Unsupported file %s (%s)',
185  basename($this->filePath),
186  $this->mimeType ?? 'unknown'
187  ),
188  1550060977
189  );
190  }
191 
192  protected function ‪escape(string $value): string
193  {
194  return CommandUtility::escapeShellArgument($value);
195  }
196 
197  protected function ‪isInAllowedExtensions(string $extension): bool
198  {
199  return in_array($extension, $this->allowedExtensions, true);
200  }
201 
202  protected function ‪getFileInfo(string ‪$filePath): FileInfo
203  {
204  return GeneralUtility::makeInstance(FileInfo::class, ‪$filePath);
205  }
206 }
‪TYPO3\CMS\Core\Imaging
Definition: Dimension.php:16
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$fileExists
‪bool $fileExists
Definition: ImageMagickFile.php:47
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\__toString
‪__toString()
Definition: ImageMagickFile.php:143
‪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\getFileInfo
‪getFileInfo(string $filePath)
Definition: ImageMagickFile.php:192
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$filePath
‪string $filePath
Definition: ImageMagickFile.php:35
‪TYPO3\CMS\Core\Imaging\Exception\UnsupportedFileException
Definition: UnsupportedFileException.php:25
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$mimeExtensions
‪string[] $mimeExtensions
Definition: ImageMagickFile.php:67
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\isInAllowedExtensions
‪isInAllowedExtensions(string $extension)
Definition: ImageMagickFile.php:187
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\fromFilePath
‪static fromFilePath(string $filePath, int $frame=null)
Definition: ImageMagickFile.php:111
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$mimeType
‪string null $mimeType
Definition: ImageMagickFile.php:59
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$allowedExtensions
‪string[] $allowedExtensions
Definition: ImageMagickFile.php:81
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\escape
‪escape(string $value)
Definition: ImageMagickFile.php:182
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\resolvePrefix
‪resolvePrefix()
Definition: ImageMagickFile.php:158
‪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:124
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\CommandUtility
Definition: CommandUtility.php:54
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$fileExtension
‪string $fileExtension
Definition: ImageMagickFile.php:53
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\$asArgument
‪string $asArgument
Definition: ImageMagickFile.php:75