Home>

1. Generate the addition verification code picture

session_start ();
/* Define header file as picture * /
header ("content-type:image/png");
/* Generate verification code * /
/* Create picture and set font color * /
$im=imagecreate ($w, $h);
$red=imagecolorallocate ($im, 255, 255, 255);
$white=imagecolorallocate ($im, 255, 255, 255);
/* Generate two numbers randomly * /
$num1=rand (1, 20);
$num2=rand (1, 20);
$_session ["administratorconfirmcode"]=$num1 + $num2;
/* Set picture background color * /
$gray=imagecolorallocate ($im, 118, 151, 199);
$black=imagecolorallocate ($im, mt_rand (0, 100), mt_rand (0, 100), mt_rand (0, 100));
/* Create picture background * /
imagefilledrectangle ($im, 0, 0, 100, 24, $black);
/* Randomly generate a large number of points on the canvas * /
for ($i=0;$i<80;$i ++) {
  imagesetpixel ($im, rand (0, $w), rand (0, $h), $gray);
}
/* Write calculated verification code to picture * /
imagestring ($im, 5, 5, 4, $num1, $red);
imagestring ($im, 5, 30, 3, "+", $red);
imagestring ($im, 5, 45, 4, $num2, $red);
imagestring ($im, 5, 70, 3, "=", $red);
imagestring ($im, 5, 80, 2, "?", $white);
/* Output picture * /
imagepng ($im);
imagedestroy ($im);

2. Generate character verification code picture [It is worth noting where is the font,Need to introduce the actual font path,Otherwise, the image may not show the verification code]

session_start ();
/* Set file header as picture output * /
header ("content-type:image/jpeg");
/* Call the generate verification code function * /
$str="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz01234565789";
$result="";
for ($i=0;$i<$length;$i ++) {
  $num [$i]=rand (0,61);
  $result.=$str [$num [$i]];
}
$text=$result;
$_session ["administratorconfirmcode"]=$text;
/* Set the width and height of the picture * /
$im_x=$w;
$im_y=$y;
/* Create picture * /
$im=imagecreatetruecolor ($im_x, $im_y);
$text_c=imagecolorallocate ($im, mt_rand (0,100), mt_rand (0,100), mt_rand (0,100));
$tmpc0=mt_rand (100,255);
$tmpc1=mt_rand (100,255);
$tmpc2=mt_rand (100,255);
$buttum_c=imagecolorallocate ($im, $tmpc0, $tmpc1, $tmpc2);
imagefill ($im, 16, 13, $buttum_c);
/* Font file * /
$font=_web_dir _. "/font/comic.ttf";
for ($i=0;$i<strlen ($text);$i ++) {
  $tmp=substr ($text, $i, 1);
  $array=array (-1,1);
  $p=array_rand ($array);
  $an=$array [$p] * mt_rand (1,10);//angle
  $size=28;
  imagettftext ($im, $size, $an, 15 + $i * $size, 35, $​​text_c, $font, $tmp);
}
/* Write characters to a file * /
$distortion_im=imagecreatetruecolor ($im_x, $im_y);
imagefill ($distortion_im, 16, 13, $buttum_c);
for ($i=0;$i<$im_x;$i ++) {
  for ($j=0;$j<$im_y;$j ++) {
    $rgb=imagecolorat ($im, $i, $j);
    if ((int) ($i + 20 + sin ($j/$im_y * 2 * m_pi) * 10)&=;imagesx ($distortion_im)&&(int) ($i + 20 + sin ($j/$im_y * 2 * m_pi) * 10)>= 0) {
      imagesetpixel ($distortion_im, (int) ($i + 10 + sin ($j/$im_y * 2 * m_pi-m_pi * 0.1) * 4), $j, $rgb);
    }
  }
}
/* Number of interfering element points * /
$count=160;
/* Create interference element points * /
for ($i=0;$i<$count;$i ++) {
  $randcolor=imagecolorallocate ($distortion_im, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255));
  imagesetpixel ($distortion_im, mt_rand ()%$im_x, mt_rand ()%$im_y, $randcolor);
}
/* Create interference lines * /
$rand=mt_rand (5,30);
$rand1=mt_rand (15,25);
$rand2=mt_rand (5,10);
for ($yy=$rand;$yy<= + $rand + 2;$yy ++) {
  for ($px=-80;$px<= 80;$px=$px + 0.1) {
    $x=$px/$rand1;
    if ($x!=0) {
      $y=sin ($x);
    }
    $py=$y * $rand2;
    imagesetpixel ($distortion_im, $px + 80, $py + $yy, $text_c);
  }
}
/* Output image to browser in png format * /
imagepng ($distortion_im);
/* Destroy image * /
imagedestroy ($distortion_im);
imagedestroy ($im);
php
  • Previous Simple usage example of DropDownList in Yii2
  • Next Yii 20 implementation to create simple widgets example