douyicao2199 2013-01-21 07:33
浏览 45

如何使用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 2013-01-21 08:04
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误