php调用图片乱码有关问题
程序员文章站
2022-05-12 22:11:23
...
php调用图片乱码问题
代码如上所示,输出来的是一串乱码。
网站:http://1.alilo.sinaapp.com/Weather/Weather.php
------解决思路----------------------
//城市名
$city = '长沙';
//获取json格式的数据
$str = file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=KXrZyXh5wUSBEdo5TkgRQkBz");
//对json格式的字符串进行编码
$arr = json_decode($str,TRUE);
echo "城市:".$arr['results'][0]['currentCity']." 日期:".$arr['date']."
";
$wea = "http://1.alilo.sinaapp.com/Weather/0.jpeg";
$weai = imagecreatefromgif($wea);
header("Content-type:image/jpeg");
imagejpeg($weai);
foreach($arr['results'][0]['weather_data']as $val)
{
echo $val['date']."
";
echo "天气:{$val['weather']}
";
$path = "{$val['dayPictureUrl']}";
echo "$path";
$imgInfo = getimagesize($path);
$imgType = $imgInfo[2];
switch ($imgType) {
case 1:
$img = imagecreatefromgif($path);
header("Content-type:image/gif");
imagegif($img);
break;
case 2 :
$img = imagecreatefromjpeg($path);
header("Content-type:image/jpeg");
imagejpeg($img);
break;
case 3 :
$img = imagecreatefrompng($path);
header("Content-type:image/png");
imagepng($img);
break;
default:
exit("图片格式不支持");
}
echo "
";
// echo "";
echo "风向:{$val['wind']}
";
echo "温度:{$val['temperature']}
";
}
?>
代码如上所示,输出来的是一串乱码。
网站:http://1.alilo.sinaapp.com/Weather/Weather.php
------解决思路----------------------
//城市名
$city = "长沙";
//获取json格式的数据
$string ="http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=KXrZyXh5wUSBEdo5TkgRQkBz";
$str = file_get_contents($string);
//echo $str;
//对json格式的字符串进行编码
$arr = json_decode($str,TRUE);
foreach($arr['results'][0]['weather_data']as $val)
{
echo $val['date']."
";
echo "天气:{$val['weather']}
";
$path = "{$val['dayPictureUrl']}";
echo '';
echo "
";
echo "
";
// echo "";
echo "风向:{$val['wind']}
";
echo "温度:{$val['temperature']}
";
}
?>
相关文章
相关视频