public static function save($path, $img, $type = self::TYPE_JPG, $quality = null)
    {
        $dir = explode('/', $path);
        unset($dir[count($dir) - 1]);
        $dir = implode('/', $dir);
        if (!file_exists($dir) || !is_dir($dir)) {
            mkdir($dir, 0777, true);
        }

        switch ($type) {
            case self::TYPE_PNG:
                @imagepng($img, $path, $quality ?: 9);
                break;
            case self::TYPE_GIF:
                @imagegif($img, $path);
                break;
            case self::TYPE_BMP:
                @imagewbmp($img, $path);
                break;
            case self::TYPE_WEBP:
                @imagewebp($img, $path, $quality ?: 90);
                break;
            case self::TYPE_JPG:
            default:
                @imagejpeg($img, $path, $quality ?: 90);
        }
    }