code: public function saveToFile($path)
  	{
  		$arrGdInfo = gd_info();
  		$extension = strtolower(substr($path, strrpos($path, '.') + 1));
  
  		// Fallback to PNG if GIF ist not supported
  		if ($extension == 'gif' && !$arrGdInfo['GIF Create Support'])
  		{
  			$extension = 'png';
  		}
  
  		// Get the relative path
  		$folder = StringUtil::stripRootDir($path);
  
  		// Create the parent folder
  		if (($dirname = \dirname($folder)) != '.' && !is_dir(System::getContainer()->getParameter('kernel.project_dir') . '/' . $dirname))
  		{
  			new Folder($dirname);
  		}
  
  		// Create the new image
  		switch ($extension)
  		{
  			case 'gif':
  				$this->convertToPaletteImage();
  				imagegif($this->gdResource, $path);
  				break;
  
  			case 'jpg':
  			case 'jpeg':
  				imageinterlace($this->gdResource, 1); // see #6529
  				imagejpeg($this->gdResource, $path, (System::getContainer()->getParameter('contao.image.imagine_options')['jpeg_quality'] ?: 80));
  				break;
  
  			case 'png':
  				if ($this->countColors(256) <= 256 && !$this->isSemitransparent())
  				{
  					$this->convertToPaletteImage();
  				}
  				imagepng($this->gdResource, $path);
  				break;
  
  			default:
  				throw new \InvalidArgumentException('Image type "' . $extension . '" cannot be generated');
  				break;
  		}
  
  		return $this;
  	}
docs: Save the GD image to a file
  
  @param string $path The image path
  
  @return static
  
  @throws \InvalidArgumentException If the image type cannot be generated