my-project-zf /library/Common/Plugin/ImgCode.php

Language PHP Lines 132
MD5 Hash 351cb72ac973dd864652a9a8b0a69215 Estimated Cost $2,055 (why?)
Repository http://my-project-zf.googlecode.com/svn/trunk/ View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* ???????
*
*/

class Common_Plugin_ImgCode extends Zend_Controller_Plugin_Abstract {

    /**
     * ???????
     *
     * @static
     * @access public
     *
     * @param string $length ??
     * @param string $mode ?? 0 ?? 1:???2????? 3:???? 4:??????
     * @param string $type ????
     * @param string $width ??
     * @param string $height ??
     *
     * @return string
     * @throws ThinkExecption
     */
    function image($length = 4, $mode = 4, $type = 'png', $width = 50, $height = 25) {
        $randval = $this->rand_string($length, $mode); //?????
        $authCode = new Zend_Session_Namespace('Auth_Code'); //???????
        $authCode->imagecode = $randval; //??session????????session?

        //$_SESSION['IMGCODE']= $randval; //??????$_SESSION['IMGCODE']?

        $width = ($length * 9 + 10) > $width ? $length * 9 + 10 : $width;
        if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
            $im = @ imagecreatetruecolor($width, $height);
        } else {
            $im = @ imagecreate($width, $height);
        }
        $r = Array (
            225,
            255,
            255,
            223
        );
        $g = Array (
            225,
            236,
            237,
            255
        );
        $b = Array (
            225,
            236,
            166,
            125
        );
        $key = mt_rand(0, 3);

        $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //???????
        $borderColor = imagecolorallocate($im, 100, 100, 100); //???
        $pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //???

        @ imagefilledrectangle($im, 0, 0, $width -1, $height -1, $backColor);
        @ imagerectangle($im, 0, 0, $width -1, $height -1, $borderColor);
        $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
        // ??
        for ($i = 0; $i < 10; $i++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
        }
        for ($i = 0; $i < 25; $i++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
        }

        @ imagestring($im, 5, 5, 3, $randval, $stringColor);
        $this->output($im, $type);
    }

    function output($im, $type = 'png') {
        header("Content-type: image/" . $type);
        $ImageFun = 'Image' . $type;
        $ImageFun ($im);
        imagedestroy($im);
    }

    /**
     * ???????????????? ????6? ???????
     *
     * @param string $len ??
     * @param string $type ????
     * 0 ?? 1 ?? ?? ??
     * @param string $addChars ????
     * @return string
     */
    function rand_string($len = 6, $type = '', $addChars = '') {
        $str = '';
        switch ($type) {
            case 0 :
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;
                break;
            case 1 :
                $chars = str_repeat('0123456789', 3);
                break;
            case 2 :
                $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
                break;
            case 3 :
                $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
                break;
            default :
                // ????????????oOLl???01???????addChars??
                $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;
                break;
        }
        if ($len > 10) { //?????????????
            $chars = $type == 1 ? str_repeat($chars, $len) : str_repeat($chars, 5);
        }
        if ($type != 4) {
            $chars = str_shuffle($chars);
            $str = substr($chars, 0, $len);
        } else {
            // ?????
            for ($i = 0; $i < $len; $i++) {
                $str .= substr($chars, floor(mt_rand(0, mb_strlen($chars, 'utf-8') - 1)), 1);
            }
        }
        return $str;
    }

}


?>
Back to Top