PHP获取城市及天气预报 生成图片
PHP获取城市及天气预报 生成图片
图片的大小为 500 x 200 px
代码如下:
<?php
/**
* 获取天气生成图片 By 简爱 http://www.gouji.org
*
* 所需文件:
* 字体
* ./font/simsun.ttc
* ./font/msyh.ttf
* ./font/hyzy.ttf
*
* 城市 ID 数据
* ./city.php
*
* 图片
* ./tq.gif
**/
$ip = get_ip(); // 获取 IP
$city_cn = get_city($ip); // 获取 城市
//获取城市 ID $city_id
$file ='./city.php'; // 保存城市ID数组的文件(城市名为 key 值为对应 城市ID)
$arr = require $file;
if(array_key_exists($city_cn, $arr)){ // 判断是否存在指定城市
$city_id = $arr[$city_cn];
}else{
$city_id = "101010100"; // 不存在 设置为 北京 ID
}
$tq_j = get_tq($city_id);
$tq_s = json_decode($tq_j, true);
$text1 = $tq_s['weatherinfo']['date_y']." ".$tq_s['weatherinfo']['week']."\r\n";
$text1 .= "城市:".$tq_s['weatherinfo']['city']."\r\n";
$text1 .= "今天:".$tq_s['weatherinfo']['weather1']." ".get_wd($tq_s['weatherinfo']['temp1']).$tq_s['weatherinfo']['wind1']."\r\n";
$text1 .= "明天:".$tq_s['weatherinfo']['weather2']." ".get_wd($tq_s['weatherinfo']['temp2']).$tq_s['weatherinfo']['wind2']."\r\n";
$text1 .= "后天:".$tq_s['weatherinfo']['weather3']." ".get_wd($tq_s['weatherinfo']['temp3']).$tq_s['weatherinfo']['wind3']."\r\n";
//获取时间日期
$time_now = gmdate("G:i:s",time()+8*3600);
$date_now = gmdate("Y年m月d日",time()+8*3600);
$day_now = gmdate("w",time()+8*3600);
$mweek=array(0=>"星期日",1=>"星期一",2=>"星期二",3=>"星期三",
4=>"星期四",5=>"星期五",6=>"星期六");
$week=$mweek[$day_now];
//设置字体
$font = './font/simsun.ttc';
$font2 = './font/msyh.ttf';
$font_n = './font/hyzy.ttf';
$font_size = '13'; //文字大小
$font_size_2 = '11';
$fontx1 = '15'; //text1 位置 x
$fonty1 = '50'; //text1 位置 y
$fontx2 = '30'; //text2 位置 x
$fonty2 = '21'; //text2 位置 y
$text2 = "天气 by 简爱 -- ".$date_now." ".$week." ".$time_now;
//设置 文件头
header("Content-type: image/png");
$im = imagecreatefromgif("./tq.gif");
//定义颜色
$grey = imagecolorallocate($im, 0, 255, 0);
$black = ImageColorAllocate($im, 0,0,0);//定义黑色的值
$yellow = ImageColorAllocate($im, 255,255,128);//定义黄色的值
$white = ImageColorAllocate($im, 255,255,255);//定义白色的值
$red = ImageColorAllocate($im, 255,0,0);//定义红色的值
$blue = ImageColorAllocate($im, 0x33,0x99,0xff);//定义蓝色色的值
$green = ImageColorAllocate($im, 0x66,0x99,0x00);//定义绿色的值
$text1s = $grey;
$text2s = $black;
//写字 参数:(图像,字号, 透明度,字体文件,X坐标, Y坐标, 颜色, 字体,文字)
imagettftext($im, $font_size, 0, $fontx1, $fonty1, $text1s, $font, $text1);
imagettftext($im, $font_size_2, 0, $fontx2, $fonty2, $text2s, $font2, $text2);
imagettftext($im, 16, 0, 115, 180, $red, $font_n, "Powered by GouJi.org");
imagegif($im);
imagedestroy($im);
//获取IP
function get_ip(){
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
if (!ip2long($ip)) {
$ip = '';
}
return $ip;
}
//通过IP 获取城市
function get_city($ip){
$url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=".$ip;
$dz = file_get_contents($url);
$di = split(' ',$dz);
//print_r($di);
return $di[5];
}
//通过ID 获取天气
function get_tq($city_id){
$url = "http://m.weather.com.cn/data/".$city_id.".html";
$txt = file_get_contents($url);
return $txt;
}
//分割温度 (℃ 在生成图片时不显示)
function get_wd($wd){
$wd_z = strlen($wd);
$wd_s = strrpos($wd,"~");
$wd_1 = substr($wd,0,$wd_s-3);
$wd_2 = substr($wd,$wd_s+1,$wd_z-$wd_s-4);
return $wd_1."~".$wd_2." 度 ";
}
本文出自简爱博客,转载时请注明出处及相应链接。
评论
看看
好东西,看看
求下载
@cxw:下载试试吧 希望能用
谢谢您发给我
代码放在哪里?
@东莞临在:这个不是 EMLOG 插件
只是一段 PHP 代码
保存为 PHP 就行 目录下 需要放置 tq.gif 与 city.php
待会儿 上传
谢谢了
收了。谢了