Maison >développement back-end >tutoriel php >php调用图片乱码问题
<meta charset="UTF-8"><?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']."<br /><br />"; $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']."<br/>"; echo "天气:{$val['weather']}<br/>"; $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 "<br/>"; // echo "< img src = "{$val['dayPictureUrl']}" />"; echo "风向:{$val['wind']}<br/>"; echo "温度:{$val['temperature']}<br/><br />"; }?>
你在一个页面中输出了两种类型的数据,浏览器不知道怎么把他们分开。于是就 乱码了
你需要将
$wea = "http://1.alilo.sinaapp.com/Weather/0.jpeg";
$weai = imagecreatefromgif($wea);
header("Content-type:image/jpeg");
imagejpeg($weai);
改为
echo '';
如果你确实想通过抓取图片的方式了完成,那么需要将那4行单独写成文件,比如 wea.php
然后通过标记
去调用
你在一个页面中输出了两种类型的数据,浏览器不知道怎么把他们分开。于是就 乱码了
你需要将
$wea = "http://1.alilo.sinaapp.com/Weather/0.jpeg";
$weai = imagecreatefromgif($wea);
header("Content-type:image/jpeg");
imagejpeg($weai);
改为
echo '';
如果你确实想通过抓取图片的方式了完成,那么需要将那4行单独写成文件,比如 wea.php
然后通过标记
去调用
你不都是
$img = imagecreatefromgif($path);
header("Content-type:image/gif");
imagegif($img);
这样的吗?
一样的问题就不会举一反三吗?
<meta charset="UTF-8"><?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']."<br/>"; echo "天气:{$val['weather']}<br/>"; $path = "{$val['dayPictureUrl']}"; echo '<img src="'.$path.'" alt="php调用图片乱码问题" >'; echo "<br/>"; echo "<br/>"; // echo "< img src = "{$val['dayPictureUrl']}" />"; echo "风向:{$val['wind']}<br/>"; echo "温度:{$val['temperature']}<br/><br />"; } ?>
<meta charset="UTF-8"><?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']."<br/>"; echo "天气:{$val['weather']}<br/>"; $path = "{$val['dayPictureUrl']}"; echo '<img src="'.$path.'" alt="php调用图片乱码问题" >'; echo "<br/>"; echo "<br/>"; // echo "< img src = "{$val['dayPictureUrl']}" />"; echo "风向:{$val['wind']}<br/>"; echo "温度:{$val['temperature']}<br/><br />"; } ?>