使用PHP创建的图像不会显示

时间:2015-12-04 14:52:28

标签: php html php-gd

我正在使用横​​幅生成器。如果我访问banner.php?id=<ID>,所有代码都可以正常运行。我正在研究localhost所以,我知道,如果我想看到我创建的这个图像,我必须写:

<img src="http://localhost/banner.php?id=<ID>" />

如果我访问直接链接,我只能看到图像。如果我将其添加到<img>标记中,为什么我看不到它?我以为是因为我使用cURL来获取一些变量,但没有。

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

// GET CULOURS
if(!isset($_GET['color_title'])) $color_title = "FFC200"; else $color_title = substr($_GET['color_title'],0,6);
if(!isset($_GET['color_info'])) $color_info = "FFFFFF"; else $color_info = substr($_GET['color_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.html">';
}

$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 = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$steamauth['apikey']."&steamids=".$steamid."");
    $user = json_decode($uuser, 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 = file_get_contents("http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=".$steamauth['apikey']."&steamid=".$steamid."");
                $game = json_decode($ggame);
                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

    if($last_match_weapon == "1") { $last_match_favweapon = "Desert Eagle"; }
    else if($last_match_weapon == "2") { $last_match_favweapon = "Dual Berettas"; }
    else if($last_match_weapon == "3") { $last_match_favweapon = "Five-Seven"; }
    else if($last_match_weapon == "7") { $last_match_favweapon = "AK-47"; }
    else if($last_match_weapon == "4") { $last_match_favweapon = "Glock-18"; }
    else if($last_match_weapon == "8") { $last_match_favweapon = "AUG"; }
    else if($last_match_weapon == "9") { $last_match_favweapon = "AWP"; }
    else if($last_match_weapon == "10") { $last_match_favweapon = "Famas"; }
    else if($last_match_weapon == "11") { $last_match_favweapon = "G3SG1"; }
    else if($last_match_weapon == "13") { $last_match_favweapon = "Galil AR"; }
    else if($last_match_weapon == "14") { $last_match_favweapon = "M249"; }
    else if($last_match_weapon == "16") { $last_match_favweapon = "M4"; }
    else if($last_match_weapon == "17") { $last_match_favweapon = "Mac-10"; }
    else if($last_match_weapon == "19") { $last_match_favweapon = "P90"; }
    else if($last_match_weapon == "24") { $last_match_favweapon = "UMP-45"; }
    else if($last_match_weapon == "25") { $last_match_favweapon = "XM1014"; }
    else if($last_match_weapon == "26") { $last_match_favweapon = "PP-Bizon"; }
    else if($last_match_weapon == "27") { $last_match_favweapon = "Mag-7"; }
    else if($last_match_weapon == "28") { $last_match_favweapon = "Negev"; }
    else if($last_match_weapon == "29") { $last_match_favweapon = "SawedOff"; }
    else if($last_match_weapon == "30") { $last_match_favweapon = "Tec-9"; }
    else if($last_match_weapon == "31") { $last_match_favweapon = "Zeus x27"; }
    else if($last_match_weapon == "32") { $last_match_favweapon = "P2000"; }
    else if($last_match_weapon == "33") { $last_match_favweapon = "MP7"; }
    else if($last_match_weapon == "34") { $last_match_favweapon = "MP9"; }
    else if($last_match_weapon == "35") { $last_match_favweapon = "Negev"; }
    else if($last_match_weapon == "36") { $last_match_favweapon = "P250"; }
    else if($last_match_weapon == "38") { $last_match_favweapon = "Scar-20"; }
    else if($last_match_weapon == "39") { $last_match_favweapon = "SG553"; }
    else if($last_match_weapon == "40") { $last_match_favweapon = "SSG08"; }
    else if($last_match_weapon == "42") { $last_match_favweapon = "Knife"; }
    else if($last_match_weapon == "43") { $last_match_favweapon = "Flashbang"; }
    else if($last_match_weapon == "44") { $last_match_favweapon = "HE Grenade"; }
    else if($last_match_weapon == "45") { $last_match_favweapon = "Smoke Grenade"; }
    else if($last_match_weapon == "46") { $last_match_favweapon = "Molotov"; }
    else if($last_match_weapon == "47") { $last_match_favweapon = "Decoy Grenade"; }
    else if($last_match_weapon == "48") { $last_match_favweapon = "Incendiary Grenade"; }
    else if($last_match_weapon == "49") { $last_match_favweapon = "C4"; }
    else if($last_match_weapon == "59") { $last_match_favweapon = "Knife"; }
    else if($last_match_weapon == "60") { $last_match_favweapon = "M4"; }
    else if($last_match_weapon == "61") { $last_match_favweapon = "USP-S"; }
    else if($last_match_weapon == "63") { $last_match_favweapon = "CZ75-Auto"; }
    else { $last_match_favweapon = "Unknown"; }
}
// Create rank image
switch($rank){
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
        $rank_image = "images/ranks/".$rank.".png";
    break;
    default:
        $rank_image = "images/ranks/0.png";
    break;
}
$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/AK-47.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,5,0,0,imagesx($rankimage),imagesy($rankimage));
// Username
imagettftext($im,9,0,153,20,$color_info,'fonts/arialbd.ttf',$username);
// Status
$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($customMessage == "Online"){
    imagettftext($im,9,90,347,63,$color_bgreen,'fonts/arialbd.ttf',"Online");
}elseif ($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,110,70,$color_title,'fonts/arialbd.ttf',$last_match_favweapon);
// K/D Ratio
imagettftext($im,11,0,175,52,$color_info,'fonts/arialbd.ttf',$kdr);
imagettftext($im,9,0,177,70,$color_title,'fonts/arialbd.ttf',"K/D");
// Win RATIO
imagettftext($im,11,0,220,52,$color_info,'fonts/arialbd.ttf', $win."%");
imagettftext($im,9,0,222,70,$color_title,'fonts/arialbd.ttf',"WIN %");
// Hours Played
imagettftext($im,11,0,280,52,$color_info,'fonts/arialbd.ttf',$hoursPlayed."h");
imagettftext($im,9,0,282,70,$color_title,'fonts/arialbd.ttf',"TIME");
}
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

3 个答案:

答案 0 :(得分:0)

这部分错了

<img src="http://localhost/banner.php?id=<ID>" />

应该是

<img src="http://localhost/path to your image" />

如果你已经完成了,请点击某个页面,

那么它应该是

<a href="http://localhost/banner.php?id=<ID>">
    <img src="http://localhost/path to your image" />
</a>

答案 1 :(得分:0)

你能告诉我们你的代码吗?你使用哪个图形库?我在本地计算机上使用GD2没有任何问题。

E.g。

<?php

header('Content-type: image/png');
$img = imagecreate(256, 256);
$background = imagecolorallocate($img, 255, 255, 0);
$color = imagecolorallocate($img, $_GET['red'], $_GET['blue'], $_GET['green']);
imagearc($img, 128, 128, 64, 64, 0, 0, $color);
imagepng($img);

?>

然后在HTML文件中:

<img src="img.php?red=255&blue=0&green=0" />

为我工作......

答案 2 :(得分:0)

当我将文件名“banner.php”更改为其他名称时,例如:'counter.php',... 相同的代码工作正常。我不知道文件名的原因是name =“banner”产生了这个错误