PHP图像未使用img元素显示在HTML中


PHP Image not showing in HTML using img element

你好,我有一个php文件,其中包含:

当我访问PHP文件时,图像显示正确,但是当我试图在HTML模板中显示它时,它显示为带有裂缝的小img,所以基本上说"找不到图像"

<img src="http://konvictgaming.com/status.php?channel=blindsniper47">

是我用来在HTML模板中显示它的,但它似乎不想显示,我已经尝试过搜索我的特定问题,但几乎没有结果,尽管我确信我可能搜索错了标题

在下方添加来自OP的代码

$clientId = '';             // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png';     // Set online image here
$offline = 'offline.png';   // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
if ($json_array['stream'] != NULL) {
    $channelTitle = $json_array['stream']['channel']['display_name'];
    $streamTitle = $json_array['stream']['channel']['status'];
    $currentGame = $json_array['stream']['channel']['game'];
    echo "<img src='$online' />";
} else {
    echo "<img src='$offline' />";
}

url不是图像,而是具有以下内容的网页

<img src='offline.png' alt='Offline' />

网页不能显示为图像。您需要编辑页面,以便只传输具有正确http标头的实际图像。

您可能可以通过谷歌搜索"php动态图像"来找到一些帮助。

在HTTP标头中指定它是PNG(或任何)图像!

(默认情况下,它们被解释为text.html)

在status.php文件中,输出<img src=...的标记,将其更改为如下

$image = file_get_contents("offline.png");
header("Content-Type: image/png");
echo $image;

它将发送请求的实际图像,而不是发送标记。标记对于img标记而言不是有效的src

更新下面修改的代码。

$clientId = '';             // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png';     // Set online image here
$offline = 'offline.png';   // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
header("Content-Type: image/png");
$image = null;
if ($json_array['stream'] != NULL) {
    $channelTitle = $json_array['stream']['channel']['display_name'];
    $streamTitle = $json_array['stream']['channel']['status'];
    $currentGame = $json_array['stream']['channel']['game'];
    $image = file_get_contents($online);
} else {
    $image = file_get_contents($offline);        
}
echo $image;

我想你在这个页面上动态地更改了图片。

更改最少的最简单方法就是使用iframe:

<iframe src="http://konvictgaming.com/status.php?channel=blindsniper47">    </iframe>