如何使用PHP编辑图像

时间:2013-01-21 07:33:46

标签: php image-processing

我写了以下一组代码。

$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.

请帮帮我。

1 个答案:

答案 0 :(得分:2)

这不是直接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 ?>以外没有数据