code: public function preFileAdd(&$targetFileName, \TYPO3\CMS\Core\Resource\Folder $folder, $sourceFile)
      {
          $storageConfiguration = $folder->getStorage()->getConfiguration();
          $storageRecord = $folder->getStorage()->getStorageRecord();
          if ($storageRecord['driver'] !== 'Local') {
              // Unfortunately unsupported yet
              return;
          }
  
          if (static::$originalFileName) {
              // Temporarily change back the file name to ensure original format is used
              // when converting from one format to another with IM/GM
              $targetFileName = static::$originalFileName;
              static::$originalFileName = null;
          }
  
          $targetDirectory = $storageConfiguration['pathType'] === 'relative' ? PATH_site : '';
          $targetDirectory .= rtrim(rtrim($storageConfiguration['basePath'], '/') . $folder->getIdentifier(), '/');
  
          $extension = strtolower(substr($targetFileName, strrpos($targetFileName, '.') + 1));
  
          // Various operation (including IM/GM) relies on a file WITH an extension
          $originalSourceFile = $sourceFile;
          $sourceFile .= '.' . $extension;
  
          if (rename($originalSourceFile, $sourceFile)) {
              $newSourceFile = $this->processFile($sourceFile, $targetFileName, $targetDirectory);
              $newExtension = strtolower(substr($newSourceFile, strrpos($newSourceFile, '.') + 1));
  
              // We must go back to original (temporary) file name
              rename($newSourceFile, $originalSourceFile);
  
              if ($newExtension !== $extension) {
                  $targetFileName = substr($targetFileName, 0, -strlen($extension)) . $newExtension;
              }
          }
      }
docs: Auto-resizes a given source file (possibly converting it as well).
  
  @param string $targetFileName
  @param \TYPO3\CMS\Core\Resource\Folder $folder
  @param string $sourceFile
  @return void