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_writeable(dirname($fileName))) {
            // try to correct the permissions
            if ($this->options['correctPermissions'] === true) {
                @chmod(dirname($fileName), 0777);

                // throw an exception if not writeable
                if (!is_writeable(dirname($fileName))) {
                    throw new \RuntimeException("File is not writeable, and could not correct permissions: {$fileName}");
                }
            } else { // throw an exception if not writeable
                throw new \RuntimeException("File not writeable: {$fileName}");
            }
        }

        // When the interlace option equals true or false call imageinterlace else leave it to default
        if ($this->options['interlace'] === true) {
            imageinterlace($this->oldImage, 1);
        } elseif ($this->options['interlace'] === false) {
            imageinterlace($this->oldImage, 0);
        }

        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;
    }