private function generate($text)
    {
        // The implementation of generation algorithm is base on this:
        // https://github.com/yiisoft/yii/blob/master/framework/web/widgets/captcha/CCaptchaAction.php

        // First and foremost we need to create a resource
        $image = imagecreatetruecolor($this->paramBag->getWidth(), $this->paramBag->getHeight());

        // Allocate using RBG
        $backgroundColor = imagecolorallocate(
            $image,
            (int) ($this->paramBag->getBackgroundColor() % 0x1000000 / 0x10000), 
            (int) ($this->paramBag->getBackgroundColor() % 0x10000 / 0x100), 
            (int) ($this->paramBag->getBackgroundColor() % 0x100)
        );

        // Now draw a filled rectangle
        imagefilledrectangle(
            $image, 0, 0, 
            $this->paramBag->getWidth(), 
            $this->paramBag->getHeight(), 
            $backgroundColor
        );

        // Now let's free some memory
        imagecolordeallocate($image, $backgroundColor);

        if ($this->paramBag->getTransparent() == true) {
            imagecolortransparent($image, $backgroundColor);
        }

        $textColor = imagecolorallocate(
            $image, 
            (int) ($this->paramBag->getTextColor() % 0x1000000 / 0x10000), 
            (int) ($this->paramBag->getTextColor() % 0x10000 / 0x100), 
            (int) ($this->paramBag->getTextColor() % 0x100)
        );

        $length = strlen($text);
        $box = imagettfbbox(30, 0, $this->paramBag->getFontFile(), $text);

        $w = $box[4] - $box[0] + $this->paramBag->getOffset() * ($length - 1);
        $h = $box[1] - $box[5];

        $scale = min(
            ($this->paramBag->getWidth() - $this->paramBag->getPadding() * 2) / $w, 
            ($this->paramBag->getHeight() - $this->paramBag->getPadding() * 2) / $h
        );

        $x = 10; // Width
        $y = round($this->paramBag->getHeight() * 27 / 40);

        for ($i = 0; $i < $length; ++$i) {
            $fontSize = (int) (rand(26, 32) * $scale * 0.8);
            $angle = rand(-10, 10);

            $letter = $text[$i];
            $box = imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $this->paramBag->getFontFile(), $letter);
            $x = $box[2] + $this->paramBag->getOffset();
        }

        // Now free the memory
        imagecolordeallocate($image, $textColor);
        return $image;
    }