Swoole\Image::verifycode_imagick PHP Method

verifycode_imagick() static public method

使用imagick扩展生成验证码图片
static public verifycode_imagick ( integer $img_width = 80, integer $img_height = 30, boolean $addRandomLines = true, boolean $swirl = true ) : array
$img_width integer
$img_height integer
$addRandomLines boolean
$swirl boolean
return array
    static function verifycode_imagick($img_width = 80, $img_height = 30, $addRandomLines = true, $swirl = true)
    {
        $fontSize = 24;
        /* imagick对象 */
        $Imagick = new \Imagick();
        /* 背景对象 */
        $bg = new \ImagickPixel();
        /* Set the pixel color to white */
        $bg->setColor('rgb(235,235,235)');
        /* 画刷 */
        $ImagickDraw = new \ImagickDraw();
        if (is_file(self::$verifyCodeFontFile)) {
            $ImagickDraw->setFont(self::$verifyCodeFontFile);
        }
        $ImagickDraw->setFontSize($fontSize);
        $ImagickDraw->setFillColor('black');
        $code = strtoupper(RandomKey::string(self::$verifyCodeLength));
        /* Create new empty image */
        $Imagick->newImage($img_width, $img_height, $bg);
        /* Write the text on the image */
        $Imagick->annotateImage($ImagickDraw, 4, 20, 0, $code);
        /* 变形 */
        if ($swirl) {
            $Imagick->swirlImage(10);
        }
        /* 随即线条 */
        if ($addRandomLines) {
            $ImagickDraw->line(rand(0, 70), rand(0, 30), rand(0, 70), rand(0, 30));
            $ImagickDraw->line(rand(0, 70), rand(0, 30), rand(0, 70), rand(0, 30));
            $ImagickDraw->line(rand(0, 70), rand(0, 30), rand(0, 70), rand(0, 30));
            $ImagickDraw->line(rand(0, 70), rand(0, 30), rand(0, 70), rand(0, 30));
            $ImagickDraw->line(rand(0, 70), rand(0, 30), rand(0, 70), rand(0, 30));
        }
        /* Draw the ImagickDraw object contents to the image. */
        $Imagick->drawImage($ImagickDraw);
        /* Give the image a format */
        $Imagick->setImageFormat('png');
        /* Send headers and output the image */
        return array('image' => $Imagick->getImageBlob(), 'code' => $code);
    }