public static function imageFile($dir = null, $width = 800, $height = 600, $format = 'png', $fullPath = true, $text = null, $textColor = null, $backgroundColor = null, $fontPath = null)
    {
        $dir = is_null($dir) ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible
        // Validate directory path
        if (!is_dir($dir) || !is_writable($dir)) {
            throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir));
        }

        // Generate a random filename. Use the server address so that a file
        // generated at the same time on a different server won't have a collision.
        $name = md5(uniqid(empty($_SERVER['SERVER_ADDR'])
            ? '' : $_SERVER['SERVER_ADDR'], true));
        $filename = implode('.', [$name, $format]);
        $filepath = $dir.DIRECTORY_SEPARATOR.$filename;

        if ($text === null) {
            $text = static::DEFAULT_TEXT_FORMAT;
        }

        if ($textColor === null) {
            $textColor = static::DEFAULT_TEXT_COLOR;
        }

        if ($backgroundColor === null) {
            $backgroundColor = static::randomElement(static::$colors);
        }

        $textColor = static::hexColor($textColor);
        $backgroundColor = static::hexColor($backgroundColor);
        $maxTextWidth = floor($width - ($width * self::TEXT_PAD_MULTIPLIER));

        $formattedText = str_replace(
            ['%width%', '%height%', '%format%', '%file%', '%filepath%', '%color%', '%bgcolor%'],
            [$width, $height, $format, $filename, $filepath, $textColor, $backgroundColor],
            $text
        );

        $imanee = new \Imanee\Imanee();
        $imanee->newImage($width, $height, $backgroundColor);

        $imanee->getDrawer()
            ->setFontColor($textColor)
            ->setTextAlign(\Imanee\Drawer::TEXT_ALIGN_LEFT);

        if ($fontPath !== null) {
            $imanee->getDrawer()->setFont($fontPath);
        }

        $imanee->setFormat($format)
            ->placeText($formattedText, \Imanee\Imanee::IM_POS_MID_CENTER, $maxTextWidth)
            ->write($filepath, static::JPEG_QUALITY);

        return $fullPath ? $filepath : $filename;
    }