$size = $width / $len; if ($size > $height) { $size = $height; } $left = ($width - $len * ($size + $size / 10)) / $size; for ($i = 0; $i < $len; $i++) { $randtext = rand(0, 9); $code .= $randtext; $textColor = imageColorAllocate($image, rand(0, 100), rand(0, 100), rand(0, 100)); $font = rand(1, 4) . ".ttf"; $randsize = rand($size - $size / 10, $size + $size / 10); $location = $left + ($i * $size + $size / 10); imagettftext($image, $randsize, rand(-18, 18), $location, rand($size - $size / 10, $size + $size / 10), $textColor, $font, $randtext); } if ($noise == true) { setnoise(); } $_SESSION['cv_yzm'] = $code; $bordercolor = getcolor($bordercolor); if ($border == true) { imageRectangle($image, 0, 0, $width - 1, $height - 1, $bordercolor); } header("Content-type: image/png"); imagePng($image); imagedestroy($image); function getcolor($color) { global $image; $color = eregi_replace("^#", "", $color); $r = $color[0] . $color[1]; $r = hexdec($r);
$image = imageCreate($width, $height); $back = imagecolorallocate($image, mt_rand(150, 255), mt_rand(150, 255), mt_rand(150, 255)); imageFilledRectangle($image, 0, 0, $width, $height, $back); $size = $width / $len; if ($size > $height) { $size = $height; } $left = ($width - $len * ($size + $size / 10)) / $size; $textall = range('A', 'Z'); for ($i = 0; $i < $len; $i++) { $tmptext = rand(0, 25); $randtext = $textall[$tmptext]; $code .= $randtext; } if ($noise == true) { setnoise($image, $width, $height, $back, $noisenum); } for ($i = 0; $i < strlen($code); $i++) { imageString($image, 5, $i * $width / 4 + mt_rand(1, 5), $height / 4, $code[$i], imageColorAllocate($image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150))); } $_SESSION['ShopNC_app_seccode'] = $code; @header("Content-type: image/png"); imagePng($image); imagedestroy($image); function setnoise($image, $width, $height, $back, $noisenum) { for ($i = 0; $i < $noisenum; $i++) { $randColor = imageColorAllocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); imageSetPixel($image, rand(0, $width), rand(0, $height), $randColor); } }