Import the font by yourself. you can modify the font as needed. We recommend that you use bold fonts.
- Class Imagecode {
- Private $ width;
- Private $ height;
- Private $ counts;
- Private $ distrubcode;
- Private $ fonturl;
- Private $ fonturlbg;
- Private $ session;
- /**
- * Enter description here...
- *
- * @ Param unknown_type $ width
- * @ Param unknown_type $ height
- * @ Param unknown_type $ Count
- * @ Param unknown_type $ distrubcode random character range
- * @ Param unknown_type $ fonturl serious code body
- * @ Param unknown_type $ fonturlbg disturbing character font
- */
- Function _ construct ($ width = 120, $ height = 30, $ counts = 5, $ distrubcode = "1235467890 qwertyuipkjhgfdaszxcvbnm", $ fonturl = "msyhbd. ttf ", $ fonturlbg = 'F. ttf '){
- $ This-> width = $ width;
- $ This-> height = $ height;
- $ This-> counts = $ counts;
- $ This-> distrubcode = $ distrubcode;
- $ This-> fonturl = $ fonturl;
- $ This-> fonturlbg = $ fonturlbg? $ Fonturlbg: $ fonturl; // used when no interfering font is set
- $ This-> session = $ this-> sessioncode ();
- Session_start ();
- $ _ SESSION ['code'] = $ this-> session;
- }
- Function imageout (){
- $ Im = $ this-> createimagesource ();
- $ This-> setbackgroundcolor ($ im );
- $ This-> set_code ($ im );
- $ This-> setdistrubecode ($ im );
- ImageGIF ($ im );
- ImageDestroy ($ im );
- }
- Private function createimagesource (){
- Return imagecreate ($ this-> width, $ this-> height );
- }
- Private function setbackgroundcolor ($ im ){
- $ Bgcolor = ImageColorAllocate ($ im, rand (200,255), rand (200,255), rand (200,255); // ± 3? ° ?? ?
- Imagefill ($ im, 0, 0, $ bgcolor );
- }
- Private function setdistrubecode ($ im ){
- $ Count_h = $ this-> height;
- $ Cou = floor ($ count_h * 2 );
- For ($ I = 0; $ I <$ cou; $ I ++ ){
- $ X = rand (0, $ this-> width );
- $ Y = rand (0, $ this-> height );
- $ Jiaodu = rand (0,360 );
- $ Fontsize = rand (8, 15 );
- $ Fonturl = $ this-> fonturlbg;
- $ Originalcode = $ this-> distrubcode;
- $ Countdistrub = strlen ($ originalcode );
- $ Dscode = $ originalcode [rand (0, $ countdistrub-1)];
- $ Color = ImageColorAllocate ($ im, rand (40,140), rand (40,140), rand (40,140); // interfere with text color
- Imagettftext ($ im, $ fontsize, $ jiaodu, $ x, $ y, $ color, $ fonturl, $ dscode );
- }
- }
- Private function set_code ($ im ){
- $ Width = $ this-> width;
- $ Counts = $ this-> counts;
- $ Height = $ this-> height;
- $ Scode = $ this-> session;
- $ Y = floor ($ height/2) + floor ($ height/4 );
- $ Fontsize = rand (30, 35 );
- $ Fonturl = $ this-> fonturl;
- $ Counts = $ this-> counts;
- For ($ I = 0; $ I <$ counts; $ I ++ ){
- $ Char = $ scode [$ I];
- $ X = floor ($ width/$ counts) * $ I + 8;
- $ Jiaodu = rand (-20, 30 );
- $ Color = ImageColorAllocate ($ im, rand (0,200), rand (50,200), rand (100,240); // text color
- Imagettftext ($ im, $ fontsize, $ jiaodu, $ x, $ y, $ color, $ fonturl, $ char );
- }
- }
- Private function sessioncode (){
- $ Originalcode = $ this-> distrubcode;
- $ Countdistrub = strlen ($ originalcode );
- $ _ Dscode = "";
- $ Counts = $ this-> counts;
- For ($ j = 0; $ j <$ counts; $ j ++ ){
- $ Dscode = $ originalcode [rand (0, $ countdistrub-1)];
- $ _ Dscode. = $ dscode;
- }
- Return $ _ dscode;
- }
- }
- Header ("Content-type: image/GIF ");
- $ Imagecode = new Imagecode (160,50 );
- $ Imagecode-> imageout ();
|