代码如下:
根据IP获取地址是免费的接口
<?php
//设置时区
date_default_timezone_set('PRC');
//获取访问者IP
$ip=$_SERVER['REMOTE_ADDR'];
//根据IP获取地址API
$url = 'http://ip-api.com/json/'.$ip.'?lang=zh-CN';
$str=getData($url);
function getData($url){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,3);
$handles = curl_exec($ch);
curl_close($ch);
return json_decode($handles,true);
}
//拼接地址
$city=$str['country'].$str['regionName'].$str['city'];
//获得时间
$time=date('Y-m-d H:i:s',time());
$weekarray=array("日","一","二","三","四","五","六");
$xq="星期".$weekarray[date("w")];
//我们需要一张图片当背景
$image = imagecreatefromjpeg("./1.jpg");
//在这张图片上写文字
$blue = imagecolorallocate($image,0,0,255);
imagettftext($image,30,0,100,100,$blue,"./SIMYOU.TTF",'欢迎来自');
imagettftext($image,30,0,58,160,$blue,"./SIMYOU.TTF",$city.'的小伙伴');
imagettftext($image,30,0,100,220,$blue,"./SIMYOU.TTF",'你的IP是');
imagettftext($image,30,0,100,280,$blue,"./SIMYOU.TTF",$ip);
imagettftext($image,30,0,50,400,$blue,"./SIMYOU.TTF",'现在时间是'.$time.$xq);
//输出
header("content-type:image/jpeg");
//输出到浏览器
imagejpeg($image);
imagedestroy($image);
?>