/** * 生成验证码图片 * * @param int $p_iWidth * @param int $p_iHeight * @param string $p_sStr * @param int $p_iFontSize * @param int $p_iPointDensity * @param int $p_iCircleDensity * @param int $p_iFontAngle */ static function createIdentifyCodeImage($p_iWidth, $p_iHeight, $p_sStr, $p_iFontSize = 0, $p_iPointDensity = 0, $p_iCircleDensity = 0, $p_iFontAngle = 0) { // 获取各种默认值 $sTextFont = Util_Common::getConfig('sImgFont'); if (0 == $p_iFontSize) { $p_iFontSize = round($p_iHeight * 3 / 5); } if (0 == $p_iPointDensity) { $p_iPointDensity = round($p_iHeight * $p_iWidth / 100); } if (0 == $p_iCircleDensity) { $p_iCircleDensity = round($p_iHeight * $p_iWidth / 200); } // 生成画布 $oImg = imagecreatetruecolor($p_iWidth, $p_iHeight); // 填充颜色 $bgc = imagecolorallocate($oImg, 255, 255, 255); imagefill($oImg, 0, 0, $bgc); // 获取字体范围大小 $aTextSize = imagettfbbox($p_iFontSize, $p_iFontAngle, $sTextFont, $p_sStr); $iTextHeight = max($aTextSize[1], $aTextSize[3]) - min($aTextSize[5], $aTextSize[7]); $iTextWidth = max($aTextSize[4], $aTextSize[2]) - min($aTextSize[0], $aTextSize[6]); // 字体起始位置 $iTextStartLeft = ($p_iWidth - $iTextWidth) / 2; $iTextStartHeight = $p_iHeight / 2 + $iTextHeight / 2; // 字体颜色 $colors = [[0, 10, 210], [24, 157, 10], [177, 70, 20]]; $colorsValue = $colors[array_rand($colors)]; $oTextColor = imagecolorallocate($oImg, $colorsValue[0], $colorsValue[1], $colorsValue[2]); // 往画布上画字符串 // imagettftext($oImg, $p_iFontSize, $p_iFontAngle, $iTextStartLeft, $iTextStartHeight, $oTextColor, $sTextFont, $p_sStr); $len = strlen($p_sStr); $_x = ($p_iWidth - 40) / $len; for ($i = 0; $i < $len; $i++) { $iTextStartLeft = $_x * $i + mt_rand(20, 25); imagettftext($oImg, $p_iFontSize, mt_rand(-10, 10), $iTextStartLeft, $iTextStartHeight, $oTextColor, $sTextFont, $p_sStr[$i]); } for ($i = 0; $i < 100; $i++) { $color = imagecolorallocate($oImg, rand(50, 220), rand(50, 220), rand(50, 220)); imagesetpixel($oImg, rand(0, $p_iWidth), rand(0, $p_iHeight), $color); } // 往画布上画线条 for ($i = 0; $i < 5; $i++) { $color = imagecolorallocate($oImg, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); imageline($oImg, mt_rand(0, $p_iWidth), mt_rand(0, $p_iHeight), mt_rand(0, $p_iWidth), mt_rand(0, $p_iHeight), $color); } ob_start(); imagegif($oImg); $blImage = ob_get_contents(); ob_end_clean(); imagedestroy($oImg); return $blImage; }