/** * 生成验证码字符串,写入SESSION,将字符串图片返回给浏览器 * * @param $len * @param int $width * @param int $height * @param int $font_size */ public static function generate($len, $width = 110, $height = 30, $font_size = 18) { $sizes = array('18' => array('width' => 25, 'height' => 25)); $words = self::words($len); $session_key = 'captcha'; //$_SESSION[$session_key] = strtolower($words); Tools_help::setSession($session_key, strtolower($words)); $image = Files_ImageManager::createWhiteImage($width, $height); $font_config = array('spacing' => -17, 'font' => '5.ttf'); $font_path = dirname(__FILE__) . '/' . $font_config['font']; $color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150)); $rand = 0; $w = $sizes[$font_size]['width'] * $len; $h = $sizes[$font_size]['height']; $x = round(($width - $w) / 2); $y = round(($height + $h) / 2) - 6; $coors = imagettftext($image, $font_size, $rand, $x, $y, $color, $font_path, $words); if ($coors) { header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Pragma: no-cache"); header("Cache-control: private"); header('Content-Type: image/png'); imagepng($image); imagedestroy($image); } exit; }
/** * 验证上传文件的类型是否为图片及大小是否越界 * @param $file * @param int $max_file_size * @return bool|string */ public static function validateUpload($file, $max_file_size = 0) { if ((int) $max_file_size > 0 && $file['size'] > (int) $max_file_size) { return sprintf(Tools_help::displayError('Image is too large (%1$d kB). Maximum allowed: %2$d kB'), $file['size'] / 1024, $max_file_size / 1024); } if (!Files_ImageManager::isRealImage($file['tmp_name'], $file['type']) || !Files_ImageManager::isCorrectImageFileExt($file['name'])) { return 'Image format not recognized, allowed formats are: .gif, .jpg, .png'; } if ($file['error']) { return sprintf(Tools_help::displayError('Error while uploading image; please change your server\'s settings. (Error code: %s)'), $file['error']); } return true; }