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 = str_replace(TL_ROOT . '/', '', $path);
// Create the parent folder
if (($dirname = dirname($folder)) != '.' && !is_dir(TL_ROOT . '/' . $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;
}