Reputation: 359
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.
Upvotes: 0
Views: 2067
Reputation: 14173
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 ?>
Upvotes: 2