Home > Backend Development > PHP Tutorial > php调用图片乱码有关问题

php调用图片乱码有关问题

WBOY
Release: 2016-06-13 12:23:00
Original
871 people have browsed it

php调用图片乱码问题

<br /><meta charset="UTF-8"><br /><?php<br />	//城市名<br />    $city = '长沙';<br />    //获取json格式的数据<br />    $str = file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=KXrZyXh5wUSBEdo5TkgRQkBz");<br /> <br />    //对json格式的字符串进行编码<br />    $arr = json_decode($str,TRUE);       <br /> <br />    echo "城市:".$arr['results'][0]['currentCity']." 日期:".$arr['date']."<br /><br />";<br /><br />	$wea = "http://1.alilo.sinaapp.com/Weather/0.jpeg";<br />	$weai = imagecreatefromgif($wea);<br />	header("Content-type:image/jpeg");<br />	imagejpeg($weai);<br /> <br />    foreach($arr['results'][0]['weather_data']as $val)<br />    {<br />       echo $val['date']."<br/>";<br />       echo "天气:{$val['weather']}<br/>";<br />        <br />       $path = "{$val['dayPictureUrl']}";<br />       echo "$path";<br />       $imgInfo = getimagesize($path);<br />       $imgType = $imgInfo[2];<br />       switch ($imgType) {<br />       case 1:<br />        $img = imagecreatefromgif($path);<br />        header("Content-type:image/gif");<br />        imagegif($img);<br />        break;<br />       case 2 :<br />        $img = imagecreatefromjpeg($path);<br />        header("Content-type:image/jpeg");<br />        imagejpeg($img);<br />        break;<br />       case 3 :<br />        $img = imagecreatefrompng($path);<br />        header("Content-type:image/png");<br />        imagepng($img);<br />        break; <br />       default:<br />        exit("图片格式不支持");<br />    }  <br />        echo "<br/>";<br />        //  echo "< img src = "{$val['dayPictureUrl']}" />";<br />       echo "风向:{$val['wind']}<br/>";<br />       echo "温度:{$val['temperature']}<br/><br />";<br />     }<br />?><br />
Copy after login

代码如上所示,输出来的是一串乱码。
网站:http://1.alilo.sinaapp.com/Weather/Weather.php
------解决思路----------------------
<br /><meta charset="UTF-8"><br /><?php<br />	//城市名<br />    $city = "长沙";<br />    //获取json格式的数据<br />	 $string ="http://api.map.baidu.com/telematics/v3/weather?location=".$city."&output=json&ak=KXrZyXh5wUSBEdo5TkgRQkBz";<br />    $str = file_get_contents($string);<br /> <br /> 	//echo $str;<br />    //对json格式的字符串进行编码<br />    $arr = json_decode($str,TRUE);       <br /> <br /> <br />   foreach($arr['results'][0]['weather_data']as $val)<br />    {<br />       echo $val['date']."<br/>";<br />       echo "天气:{$val['weather']}<br/>";<br />         <br />       $path = "{$val['dayPictureUrl']}";<br />     	echo '<img  src="'.$path.'" alt="php调用图片乱码有关问题" >';<br />	   echo "<br/>";<br />	    echo "<br/>";<br />        //  echo "< img src = "{$val['dayPictureUrl']}" />";<br />       echo "风向:{$val['wind']}<br/>";<br />       echo "温度:{$val['temperature']}<br/><br />";<br />      <br />    }  <br />     <br />    <br />?><br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template