/**
* Save an image
* The resulting format will be determined by the file extension.
*
* @param string $filename If omitted - original file will be overwritten
* @param null|int $quality Output image quality in percents 0-100
* @return $this
*
* @throws Exception
*/
public function saveAs($filename, $quality = null)
{
if (!$filename) {
throw new Exception('Empty filename to save image');
}
$dir = FS::dirname($filename);
if (is_dir($dir)) {
$this->_save($filename, $quality);
} else {
throw new Exception('Target directory "' . $dir . '" not exists');
}
return $this;
}