我编写了以下代码:
$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.
请帮帮我。
这不是直接的PHP
错误,因为您发布的代码是正确的。这很可能是由于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 ?>