2011-09-08 84 views
1

我是新的使用PHP與圖像。 我已經找到了代碼,顯示圖像圖片不顯示在php

<<?php 
// Create a blank image and add some text 
$im = imagecreatetruecolor(120, 20); 
$text_color = imagecolorallocate($im, 233, 14, 91); 
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color); 

// Save the image as 'simpletext.jpg' 
imagejpeg($im); 

// Free up memory 
imagedestroy($im); 
?>> 

但Firefox表現出一些虛假的代碼,而不是說Image.here是將圖像的鏈接,火狐顯示 http://tinypic.com/r/htw6cm/7 這裏是鏈接到圖片

+0

因爲它的localhost你手動嘗試打開圖像? –

+0

是的很明顯,它打開好 – Sharpzain120

+0

你可能發送錯誤的標題。您必須發送帶有jpeg圖像的'Content-Type:image/jpg'。 – JJJ

回答

2

什麼是您看到圖像的「字節」。默認情況下,您的瀏覽器會認爲您的腳本即將打印一些文本。所以你最終會看到一些奇怪的人物。

由於您打印的不是文字,而是圖像,因此您需要告訴瀏覽器腳本的內容將是圖像。在腳本開始

<?php 

header("Content-Type: image/jpeg"); // treat the script as an image 

// Create a blank image and add some text 
$im = imagecreatetruecolor(120, 20); 
$text_color = imagecolorallocate($im, 233, 14, 91); 
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color); 

// Save the image as 'simpletext.jpg' 
imagejpeg($im); 

// Free up memory 
imagedestroy($im); 
0

使用

<? 
header('Content-type: image/jpg'); 

發送一個報頭的圖像:您可以通過發送標題一樣,做到這一點。 因此瀏覽器將處理PHP的輸出爲JPG圖像數據

0
header('Content-type: image/jpg'); 

在腳本的開始,但在進行實際輸出,即在上述imagejpeg($im);

所以前行權,它可以讓你看到一個錯誤,如果任何發生

<?php 
// Create a blank image and add some text 
$im = imagecreatetruecolor(120, 20); 
$text_color = imagecolorallocate($im, 233, 14, 91); 
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color); 

//send header 
header('Content-type: image/jpg'); 

//output an image 
imagejpeg($im); 

還我刪除了一些失誤,錯誤和無用的運營商