【问题标题】:PHP images displaying on website as broken, even though the URLs are valid (Discord.php)PHP 图像在网站上显示为损坏,即使 URL 有效 (Discord.php)
【发布时间】:2019-06-27 22:05:04
【问题描述】:

又是我。我有一段昨天还在工作的代码,但是它现在不再显示图像,它们在浏览器中显示为“损坏”(网址指向有效位置,下面将提供示例)。这段代码的目的是查询 Discord API,然后从雪花中获取某些信息并将它们组合成有效的 HTML 标记,重申这在昨天有效,现在不再有效。

这就是图像在我创建的图像中的显示方式

Broken Images

现在,如果我们看看我的 PHP(一些信息为了安全而编辑)

<?php

$json_options = [
  "http" => [
    "method" => "GET",
    "header" => "Authorization: Bot API token"
  ]
];

$json_context = stream_context_create($json_options);

$json_get     = file_get_contents('https://discordapp.com/api/guilds/guildid/members?limit=1000', false, $json_context);

$json_decode  = json_decode($json_get, true);

$arr = array_filter($json_decode, function($e) {
  return in_array("role-id", $e['roles']);

});

foreach($arr as $e) {
  $uid = $e['user']['id']; $atar = $e['user']['avatar']; $uname = $e['user']['username']; $aimgurl = "https://cdn.discordapp.com/avatars/$uid/$atar.jpg";

  $count = $count + 1;
  echo "<img src='$aimgurl' alt=avatar$count' width='300' height='200' />";

}

?>

输出的一个例子是这样的 div:

<img src="https://cdn.discordapp.com/avatars/118715548849668096/505efab4aa33f882bab9ae22347bafa1.jpg" width="300" height="200">

现在,如果我手动访问链接https://cdn.discordapp.com/avatars/118715548849668096/505efab4aa33f882bab9ae22347bafa1.jpg,您会明显看到图像是有效的,但即使显示在空白 PHP 文档(而不是网页上)上,图像仍然会损坏。起初我以为可能有 javascript,或者与此冲突的东西,但我相信事实并非如此,问题的根源在于我如何设置图像的样式,或试图让它显示。但是,我不知道在不放置图像标签的情况下使用 PHP 将图像放到我的网页上的任何其他方法,可能有一种方法可以做到这一点。但是,如果你们中的任何人有任何建议的话,将不胜感激。谢谢。

【问题讨论】:

  • 你应该试试echo "&lt;img src='"+$aimgurl+"' alt=avatar$count' width='300' height='200' /&gt;";
  • 回显 $aimgurl 会发生什么?
  • 如果我回显 "\n", $aimgurl @Sanjit 它会提供一个图片 url 列表(都是有效的)
  • @MayankVadiya 如果我使用你的代码,它会在 div 中显示 00000000,所以不要去
  • 可能图像受到保护。我制作了一个网站,您需要在其中加载原始网站以显示图像。否则它不会工作。

标签: php html api discord snowflake-cloud-data-platform


【解决方案1】:

我不会将我自己的帖子标记为答案,我觉得这样做是不尊重的,因为我感谢社区和所提供的帮助。 Bernhard 给了我纠正问题所需的方向,问题是我需要使用 base64 对图像进行编码,然后才能显示它。这很奇怪,因为它昨天运行良好 100%,所以很奇怪今天没有。我最好的猜测是因为我昨天从 PHP 5.x 更新到 PHP 7.x。道歉,因为我知道我的代码可能并不完美或漂亮。希望这可以帮助另一个新的 PHP 用户。

    <?php

    $json_options = [
      "http" => [
        "method" => "GET",
        "header" => "Authorization: Bot bot-token-here" //add your token
      ]
    ];

    $json_context = stream_context_create($json_options);

    $json_get     = file_get_contents('https://discordapp.com/api/guilds/guild-id-here/members?limit=1000', false, $json_context); //replace guild id

    $json_decode  = json_decode($json_get, true);

    $arr = array_filter($json_decode, function($e) {
      return in_array("role-id-to-find", $e['roles']); //replace role id

    });

foreach($arr as $e) {
  $uid = $e['user']['id']; $atar = $e['user']['avatar']; $uname = $e['user']['username']; $aimgurl = "https://cdn.discordapp.com/avatars/$uid/$atar.png";
  $count = $count +1;
  $imageData = base64_encode(file_get_contents($aimgurl));
  echo 
  "<div class='usercontainer' style='position:relative;text-align:center;color:white;display:inline-block;font-size:10px;font-weight:900;z-index:1; overflow: auto;'>",
  "<style> .usercontainer img { width:60px;height:60px;border-radius:100%;padding:14px; } </style>",
  '<img src="data:image/png;base64,'.$imageData.'" onerror=this.style.display="none">',
  "<div class='user' style='position:absolute;top:50%;left:50%;transform: translate(-50%, -50%);font-family:'Raleway',Sans-Serif;'>",$uname,"</div>","</div>";
}
?>

【讨论】:

  • 如果您解决了以上问题,那么您应该将其标记为已回答,以便让其他人知道问题已解决。
  • @MagnusEriksson 好的,我不能将它标记为已解决 2 天,所以我会尽可能这样做:)
猜你喜欢
  • 2013-05-04
  • 2017-10-09
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
相关资源
最近更新 更多