2012-10-21 107 views
0

我想讓這個.php文件像一個.png文件一樣,所以我可以做例如<img src='test.php'> [示例]PHP圖像不能正常工作

我添加的報頭作爲圖像和這樣做:

<?php 

/* Start Connection */ 
[Ignore This] 
/* End Connection */ 

/* Add to counter */ 
mysql_query("UPDATE counter SET counter = counter + 1"); 
/* End Add */ 

/* For Echo */ 
$count = mysql_fetch_row(mysql_query("SELECT counter FROM counter")); 
$hits = $count[0]; 
/* Create Echo as Null 
* @echo 
echo "$hits"; 
*/ 

/* End Echo */ 

//Img Area 
$font = "font.ttf"; 
$image = imagecreatefrompng("hits.png"); 
$color = imagecolorallocate($image, 255, 255, 255); 
imagettftext($image, 15, 0, 10, 24, $color, $font, $hits); 
imagepng($image, "test.png"); 
imagedestroy($image); 
header('Content-Type: image/png') 
?> 

但它只是顯示一個斷開的圖像。

這個文件是img.php

破碎的形象是img.php

我怎樣才能解決這個問題?

+1

值得一提的是,應該在新代碼中避免使用mysql_ *,以支持PDO或MySQLi。 – Corbin

回答

0

imagepng($image, "test.png");如果要將圖像保存到文件中,爲什麼要將其輸出到瀏覽器?如果你希望它直接輸出,只需使用imagepng($image);

此外,標題調用需要在任何輸出之前。

+0

如果我做imagepng($ image);它給了我一個錯誤。然後,點擊計數器無法正確運行,無法添加匹配。 –

+0

@ClubpenguinIcebox那麼這是什麼錯誤?我會試圖讓圖像和數據庫的東西分開工作,然後重新連接它們。換句話說,試着註釋掉所有的數據庫和硬編碼'$ hits = 55;'。然後看看圖像是否呈現正確。如果沒有,請使圖像正常工作。一旦圖像工作,然後嘗試添加數據庫的東西。 – Corbin

+0

它期望paremeter 1是一個資源,它說給出了一個布爾值。而且我知道命中= 55將工作,我不想這個代碼是一個圖像,雖然 –