仅当使用直接链接访问时,才会显示在PHP中创建的映像

时间:2015-11-22 13:17:00

标签: php html json php-gd

我在localhost上创建了一个脚本,显示了json的一些统计信息。如果我访问直接链接,一切都很好。如果我尝试将该链接添加到HTML图像标记,则该页面将呈现为白色且没有图像。

可能是什么问题?如果您需要代码,我稍后会进行修改(我在移动设备上)。

此错误可能是由于使用localhost

造成的
<!DOCTYPE html>
<html>
<head>
    <title>TEST</title>
</head>
<body>
    <img src="localhost/banner.php?id=STEAMID&bg=bg1&rank=0"></a>
</body>
</html>

如果我访问localhost/file.php?id=NUMERIC_STEAMID,我可以看到该图片,但如果我添加此行<img src="" />,该页面仍为空。

<?php
include("settings.php");

// GET CULOURS
if(!isset($_GET['culoare_titlu'])) $color_title = "FFC200"; else $color_title = substr($_GET['culoare_titlu'],0,6);
if(!isset($_GET['culoare_info'])) $color_info = "FFFFFF"; else $color_info = substr($_GET['culoare_info'],0,6);
if(!isset($_GET['bg'])) $bg = "bg1"; else $bg = $_GET['bg'];
if(!isset($_GET['rank'])) $rank = "0"; else $rank = (int)$_GET['rank'];
// Get Userid
if(!$_GET['id'] || !ctype_digit($_GET['id'])){ 
    echo '<meta http-equiv="refresh" content="0;url=index.php">';
}
$steamid = $_GET['id'];

// Get username, avatar , kills, deaths and played time
$noDigit = 0; 
$hoursPlayed = 0;
$weHaveCSGO = 0;
$nullJson = 0;
if(ctype_digit($steamid)){
    $noDigit = 1;
    $uuser = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$steamauth['apikey']."&steamids=".$steamid."";
    // cURL for this 
    $ch_1 = curl_init($uuser);
    curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, true);
    $mh = curl_multi_init();
     curl_multi_add_handle($mh, $ch_1);
     // execute all queries simultaneously, and continue when all are complete
    $running = null;
    do {
        curl_multi_exec($mh, $running);
        $content = curl_multi_getcontent($ch_1);
    } while ($running);
    $response_1 = curl_multi_getcontent($ch_1);
    $user = json_decode($response_1, true);
    // Until here
    if(isset($user['response']['players'][0]['steamid'])){
        $nullJson = 1;
        $customURL = $user['response']['players'][0]['profileurl'];
        $username = $user['response']['players'][0]['personaname'];
        $avatar = $user['response']['players'][0]['avatarfull'];
        $privacy = $user['response']['players'][0]['communityvisibilitystate'];
        if($privacy == 3){
            $getGames = simplexml_load_file($customURL."games?tab=all&xml=1");
            foreach($getGames->games->game as $game){
                if($game->appID == "730"){
                    $weHaveCSGO = 1;
                    $hoursPlayed = $game->hoursOnRecord;    
                }
            }
            $xml = simplexml_load_file($customURL."?xml=1");
            $customMessage = $xml->stateMessage;
            if($weHaveCSGO == 1){
                $ggame = "http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=".$steamauth['apikey']."&steamid=".$steamid."";
                // cURL for this too
                $ch_2 = curl_init($ggame);
                curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);
                $mh = curl_multi_init();
                curl_multi_add_handle($mh, $ch_2);
                // execute all queries simultaneously, and continue when all are complete
                $running = null;
                do {
                    curl_multi_exec($mh, $running);
                    $content = curl_multi_getcontent($ch_2);
                } while ($running);
                $response_2 = curl_multi_getcontent($ch_2);
                $game = json_decode($response_2);
                foreach($game->playerstats->stats as $stats){
                    if($stats->name == "total_kills"){ $total_kills = $stats->value ; }
                    else if($stats->name == "total_deaths"){ $total_deaths = $stats->value ; }
                    else if($stats->name == "last_match_favweapon_id"){ $last_match_weapon = $stats->value ; }
                    else if($stats->name == "total_rounds_played"){ $total_rounds_played = $stats->value; } 
                    else if($stats->name == "total_wins"){ $total_wins = $stats->value; } 
                }
            }
        }

// Convert weaponID to weaponName

// This function just convert weaponID to weaponName so i removed from here

    }
}

$rank_image = "images/ranks/0.png";
$rankimage = ImageCreateFromPNG($rank_image);

// Create banner
switch($bg){
    case 'bg1':
    case 'bg2':
    case 'bg3':
    case 'bg4':
        $im = ImageCreateFromPNG("images/banners/$bg.png") or die ( 'GD Library not available atm.' );
    break;
    default:
        $im = ImageCreateFromPNG("images/banners/bg1.png") or die ( 'GD Library not available atm.' );
    break;
}

// Last match weaponID
$weapon = "images/weapons/hud/".$last_match_favweapon.".png";
$lmweapon = ImageCreateFromPNG($weapon);

//Culours for text
$color_green = imagecolorallocate($im, 0, 183, 21);
$color_red = imagecolorallocate($im, 255, 0, 0);
$color_title = "0x$color_title";
$color_info = "0x$color_info";
$color_online = "0x24FF00";
$color_offline = "0xFF0000";
$color_black = "0x000000";
$color_bgreen = imagecolorallocate($im,0,160,0);

// Make some changes
$avatar = str_replace("https","http",$avatar);

// Get new sizes for avatar
list($width, $height) = getimagesize($avatar);
$newwidth = 90;
$newheight = 90;

// Load
$source = imagecreatefromjpeg($avatar);

// Lenght username
if(strlen($username) > 21){
    $username = substr($username,0,21);
}

// Kills/Deaths Ratio
$kdr = @round($total_kills/$total_deaths,2);

// Win Ratio

$win = round((($total_wins/$total_rounds_played)*100),2);

//Let's create image, but first check user
if($noDigit == 0 || $weHaveCSGO == 0 || $nullJson == 0){
    imagettftext($im,20,0,140,60,$culoare_text_info,'fonts/arialbd.ttf',"This user doesn't exist !");
} else {
//Avatar
imagecopyresized($im, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Rank
imagecopy($im,$rankimage,96,3,0,0,imagesx($rankimage),imagesy($rankimage));
// Username
imagettftext($im,9,0,153,20,$color_info,'fonts/arialbd.ttf',$username);
// Status
if($customMessage == "Online"){
    imagettftext($im,7,0,290,20,$color_bgreen,'fonts/arialbd.ttf',"Online");
}
$cauta   = 'Last Online';
$cautare = strpos($customMessage, $cauta);
$cauta2 = 'In-Game<br/>Counter-Strike: Global Offensive';
$cautare2 = strpos($customMessage, $cauta2);
$cauta3 = 'In-Game<br/>';
$cautare3 = strpos($customMessage, $cauta3);
if ($cautare !== false) { 
    imagettftext($im,7,0,290,20,$color_red,'fonts/arialbd.ttf',"Offline");
} elseif($cautare2 !== false){
    imagettftext($im,7,0,282,20,$color_bgreen,'fonts/arialbd.ttf',"Playing CS:GO");
} elseif($cautare3 !== false){
    imagettftext($im,7,0,276,20,$color_title,'fonts/arialbd.ttf',"IN OTHER GAME");
}
// Fav weapons from last match
imagecopy($im,$lmweapon,107,30,0,0,imagesx($lmweapon),imagesy($lmweapon));
imagettftext($im,9,0,112,70,$color_title,'fonts/arialbd.ttf',$last_match_favweapon);
// K/D Ratio
imagettftext($im,11,0,166,52,$color_info,'fonts/arialbd.ttf',$kdr);
imagettftext($im,9,0,168,70,$color_title,'fonts/arialbd.ttf',"K/D");
// Win RATIO
imagettftext($im,11,0,216,52,$color_info,'fonts/arialbd.ttf',$win."%");
imagettftext($im,9,0,218,70,$color_title,'fonts/arialbd.ttf',"WIN %");
// Hours Played
imagettftext($im,11,0,276,52,$color_info,'fonts/arialbd.ttf',$hoursPlayed."h");
imagettftext($im,9,0,278,70,$color_title,'fonts/arialbd.ttf',"TIME");
}
header( 'Content-type: image/png' );
imagepng($im);
imagedestroy($im);

?>

2 个答案:

答案 0 :(得分:0)

本地主机/ banner.php ID = STEAMID&安培; BG = BG1&安培;秩= 0 应该是http://localhost/banner.php?id=STEAMID&bg=bg1&rank=0

答案 1 :(得分:0)

您的问题是您以错误的方式指向图像文件

<img src="localhost/banner.php?id=STEAMID&bg=bg1&rank=0"></a>

应该是这样的

<img src="http://localhost/banner.php?id=STEAMID&bg=bg1&rank=0"></a>

如果您不添加http://协议浏览器会将其读取为文件/目录,并会找到您的图像

假设您在localhost根目录

浏览器会将其加载为

http://localhost/localhost/banner.php?id=STEAMID&bg=bg1&rank=0

而不是

http://localhost/banner.php?id=STEAMID&bg=bg1&rank=0

当您直接访问您的图片时,您的浏览器会将其视为

http://localhost/banner.php?id=STEAMID&bg=bg1&rank=0