public function save($fileName, $format = null)
{
$validFormats = array('GIF', 'JPG', 'PNG');
$format = $format !== null ? strtoupper($format) : $this->format;
if (!in_array($format, $validFormats)) {
throw new InvalidArgumentException('Invalid format type specified in save function: ' . $format);
}
// make sure the directory is writeable
if (!is_writable(dirname($fileName))) {
// try to correct the permissions
if ($this->options['correctPermissions'] === true) {
@chmod(dirname($fileName), 0777);
// throw an exception if not writeable
if (!is_writable(dirname($fileName))) {
throw new RuntimeException('File is not writeable, and could not correct permissions: ' . $fileName);
}
} else {
throw new RuntimeException('File not writeable: ' . $fileName);
}
}
switch ($format) {
case 'GIF':
imagegif($this->oldImage, $fileName);
break;
case 'JPG':
imagejpeg($this->oldImage, $fileName, $this->options['jpegQuality']);
break;
case 'PNG':
imagepng($this->oldImage, $fileName);
break;
}
return $this;
}