background image

66

 

67

     

private

 

function

 setCaptcha()

68

    {

69

         

$this

->createCode();

70

 

71

         

for

 (

$i

 = 0; 

$i

 < 

$this

->codeNum; 

$i

++) {

72

             

$color

 = imagecolorallocate(

$this

->im, 

rand

(50, 250), 

rand

(100, 250), 

rand

(128, 250));

73

             

$size

 = 

rand

(

floor

(

$this

->height / 5), 

floor

(

$this

->height / 3));

74

             

$x

 = 

floor

(

$this

->width / 

$this

->codeNum) * 

$i

 + 5;

75

             

$y

 = 

rand

(0, 

$this

->height - 20);

76

             imagechar(

$this

->im, 

$size

$x

$y

$this

->code{

$i

}, 

$color

);

77

        }

78

    }

79

 

80

     

private

 

function

outputImg()

81

    {

82

         

if

 (imagetypes() & IMG_JPG) {

83

             

header

('Content-type:image/jpeg');

84

             imagejpeg(

$this

->im);

85

         } 

elseif

 (imagetypes() & IMG_GIF) {

86

             

header

('Content-type: image/gif');

87

             imagegif(

$this

->im);

88

         } 

elseif

 (imagetype() & IMG_PNG) {

89

             

header

('Content-type: image/png');

90

             imagepng(

$this

->im);

91

         } 

else

{

92

             

die

("Don't support image type!");

93

        }

94

    }

95

 

96

 }

?>

使用的一个例子:

<?php

require_once

 'captcha.class.php';

$captcha

 = 

new

 Captcha(80,30,4);

$captcha

->showImg();