2010-09-08 18 views
2
沒有任何其他事情發生時才起作用

我正在使用這段PHP代碼來創建和旋轉圖像。它可以完美的工作,當我只是打電話給它img.php它躺在英寸在php中創建圖像只有在頁面

但是,如果我嘗試在其他任何內容包括img.php,或只包括片段在另一頁(在同一臺服務器上),它只顯示我有很多問號而不是圖像。

我的感覺是問題與發送的標題有關,但老實說我不知道​​。

任何建議如何使這個有用的,所以它可以被包含在另一個頁面,仍然工作?

<?php 
    header("Content-type: image/jpeg"); 
    // option one 
    // create a 250*77 image 
    $im = imagecreate(250, 77); 
    // option two 
    // creating a image from jpeg 
    $im = @imagecreatefromjpeg("images/test.jpg") 
    or die("Cannot Initialize new GD image stream"); 

    // white background and black text 
    $bg = imagecolorallocate($im, 255, 255, 255); 
    $textcolor = imagecolorallocate($im, 0, 0, 0); 

//create the text 
    $skrivetekst = date('d.m.Y'); 
    $skrivetekst2 = date('H:i:s'); 

// write the string at the top left 
    imagestring($im, 5, 0, 0, $skrivetekst, $textcolor); 
    imagestring($im, 5, 0, 20, $skrivetekst2, $textcolor); 


///Rotate the image 
$rotate = imagerotate($im, 90, 0); 
    // output the image 


// Output 
imagejpeg($rotate); 


    ImageDestroy ($rotate); 
?> 

問候特勒爾斯

+0

你是什麼意思的「很多問號」?圖像無法加載? Firebug的網頁標籤說明它是否被加載? – 2010-09-08 17:25:26

+0

在這裏您可以看到問題標記:http://tm-design.dk/kontrabande/ – Troels 2010-09-08 17:32:43

+0

或probl better,screendumb:http://tm-design.dk/kontrabande/problem.png – Troels 2010-09-08 17:43:27

回答

2

這是預期的行爲,你的腳本應該針對某些類型的輸出瀏覽器,通常輸出爲text/html,其中包含html內容。要輸出圖像,請發送Content-type: image/jpeg標題,然後發送圖像二進制內容(通過imagejpeg($rotate);)。

在您的案例中顯示的錯誤可能是由於您正在嘗試在回顯HTML /文本內容後發送標題。發送輸出後,您無法發送更多標題。

要在php生成的html中輸出php生成的圖像,需要將它分成兩頁,一個輸出圖像,另一個輸出html,html輸出將通過標準<img>標籤引用圖像,如@GSto所述。


編輯:

page.php文件:

<html> 
<head><title>HI!</title></head> 
<body><?php 
    //here goes your html building logic 
    /*If you are not using and PHP, make this page page.html*/ 
    ?> 
    See my php-generated image: <br /> 
    <img src="img.php" /> 
    </body> 
</html> 

img.php:

<?php 
    header("Content-type: image/jpeg"); 
    // option one 
    // create a 250*77 image 
    $im = imagecreate(250, 77); 
    //rest of your image-generating code.. 

您將訪問第一個頁面在瀏覽器中,將看到<img>標記,請求下一個文件並獲取顯示內容那個標籤。

+0

但這是否意味着圖像的創建必須是第一頁加載的第一件事? – Troels 2010-09-08 17:36:34

+0

你能給我一個分裂2的例子,因爲我真的不知道該怎麼做。 – Troels 2010-09-08 17:44:55

+0

@Troles,編輯..現在檢查。 – aularon 2010-09-08 17:58:47

5

可能是因爲您使用的是頁面上不同的報頭類型,並且它不是使用像頭。如果你想要把它放在網頁上,我就會把它放在一個圖像標記,並調用它的方式:

<img src='img.php'> 
+0

非常感謝。 – Troels 2010-09-08 18:25:57

0

您不能在與HTML內容相同的頁面上調用。

瀏覽器希望在單獨的請求中接收圖像,然後是HTML內容。如果你想在你的頁面中嵌入一個PHP生成的圖像,你必須有兩個不同的PHP腳本。

例如,你可以有一個叫my_page.php文件生成想要的HTML標記:

<html> 
    <head> 
    <title>My Page</title> 
    </head> 
    <body> 
    <p>Look at this nice dynamic image! Generated on <?php echo date(); ?>.</p> 
    <img src="my_image.php" /> 
    </body> 
</html> 

和一個名爲my_image.php文件生成想要的圖像:

<?php 
header('Content-Type: image/jpeg'); 

$date = date(); 
$x = imagefontwidth(5) * strlen($date); 
$y = imagefontheight(5); 

$img = imagecreate($x, $y); 
$textcolor = imagecolorallocate($im, 0, 0, 255); 
imagestring($img, 5, 0, 0, $date, $textcolor); 

imagejpeg($img); 

正如你所看到的通過包括指向my_image.php<img>標籤來顯示圖像。您不能在與HTML內容相同的頁面上運行(除非使用參數$filenamesee documentation)。如果你有變量傳遞到my_image.php,你可以使用GET參數來做到這一點。