2013-01-21 60 views
0

我已經寫了下面的一組代碼的編輯圖像。如何使用PHP

$image = ImageCreate(200, 50); 
$background_color = ImageColorAllocate($image, 255, 255, 255); // white 
$gray = ImageColorAllocate($image, 204, 204, 204); // gray 
ImageFilledRectangle($image, 50, 10, 150, 40, $gray); 
header('Content-type: image/png'); 
ImagePNG($image); 

同時運行此代碼我得到一個錯誤

The image "http://localhost/app/" cannot be displayed because it contains errors. 

請幫助我。

+0

沒有數據'運行從終端/命令行或在瀏覽器這個code'? – hjpotter92

+0

@閃回:在瀏覽器中運行 –

+0

似乎對我來說工作正常。 – hjpotter92

回答

2

這不是直接在PHP錯誤,因爲你發佈的代碼是正確的。這是mostlikely由php tags.

以外的一些額外的數據引起的,如果這是你已經在文件中它的工作原理

<?php 
$image = ImageCreate(200, 50); 
$background_color = ImageColorAllocate($image, 255, 255, 255); // white 
$gray = ImageColorAllocate($image, 204, 204, 204); // gray 
ImageFilledRectangle($image, 50, 10, 150, 40, $gray); 
header('Content-type: image/png'); 
ImagePNG($image); 
?> 

,但是這不會

<?php 
//see the new line outside the php tags below? this will break the image. 
?> 

<?php 
$image = ImageCreate(200, 50); 
$background_color = ImageColorAllocate($image, 255, 255, 255); // white 
$gray = ImageColorAllocate($image, 204, 204, 204); // gray 
ImageFilledRectangle($image, 50, 10, 150, 40, $gray); 
header('Content-type: image/png'); 
ImagePNG($image); 



//these extra lines wont matter, because they are inside the php tags. 


?> 

請確保您有沒有多餘的空格或在<?php之前換行。如果你有此代碼之前,包括文件,還要檢查該文件(文件),以確保有外<?php ?>

+0

是的,它現在正在工作。感謝您的支持 :) –