douyicao2199
douyicao2199
2013-01-21 07:33

如何使用PHP编辑图像

I have written following set of codes.

$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);

while running this code I get an error

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

Please help me.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dtamho6404 dtamho6404 8年前

    This is not directly a PHP error, because the code you posted is correct. This is mostlikely caused by some extra data outside the php tags.

    if this is all you have in the file it works

    <?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);
    ?>
    

    but this wont

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

    Make sure you have no extra spaces or newlines before <?php. If you have an include file before this code, also check that file (files) to make sure there is no data outside <?php ?>

    点赞 评论 复制链接分享

相关推荐