<?php



//防盗链技术
if(isset($_SERVER['HTTP_REFERER'])){
  if(strpos($_SERVER['HTTP_REFERER'],"http://localhost")==0){
	pic();
  }
  else{
	pic();
  }
}
elseif(checkrobot()){
	pic();
}else{
	pic();
}

function pic(){
	
	$img_array = glob("./image/*.{jpg,png}",GLOB_BRACE);
	
	$dst_path = $img_array[array_rand($img_array)]; //随机调用文件夹图片，如需固定，换成固定图片路径即可
	
	$font = './Alibaba-PuHuiTi-Bold.ttf';
	$fontsize = 25;
	$dst_nr = mb_substr($_GET["k"],0,20,'utf-8');//文字内容(默认截取10个汉字)
	$dst = imagecreatefromstring(file_get_contents($dst_path));
	$color= imagecolorallocatealpha($dst,255,255,0,0);
	$imgHeight = imagesy($dst);
	$imgWidth = imagesx($dst);
	
	$fontWidth = imagefontwidth($fontsize);
	$fontHeight = imagefontheight($fontsize);
	$x = ($imgWidth-$fontWidth*strlen($dst_nr))/3;
	$y = ($imgHeight-$fontHeight)/1.85;
	
	imagefttext($dst, $fontsize, 0, $x, $y, $color, $font, $dst_nr);
	header('Content-Type: image/jpg');
	imagejpeg($dst);
	imagedestroy($dst);	
	
}

//判断是不是蜘蛛
function checkrobot($useragent=''){
	static $kw_spiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
	static $kw_browsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');

	$useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
	if(strpos($useragent, 'http') === false && dstrpos($useragent, $kw_browsers)) return false;
	if(dstrpos($useragent, $kw_spiders)) return true;
	return false;
}

function dstrpos($string, $arr, $returnvalue = false) {
	if(empty($string)) return false;
	foreach((array)$arr as $v) {
		if(strpos($string, $v) !== false) {
			$return = $returnvalue ? $v : true;
			return $return;
		}
	}
	return false;
}



?>

