0

Am working with an small php project . in creating an text generating image. took some sample codes and tired.

    <?php

$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);


header('Content-Type: image/jpeg');

imagejpeg($im);

imagedestroy($im);
?>

i made some changes and it works good . when i tried to insert this php file in other html file using it results broken image. how can i solve this why it happens.

2
  • Get rid of the spaces before the <?php. Also, if that doesn't work, comment out the header line and paste any errors you're getting here. Commented Aug 25, 2012 at 19:12
  • This script renders an image. You can't just insert it into HTML, short of base-64 encoding the image data. Also, see Rekire's answer. Commented Aug 25, 2012 at 19:13

3 Answers 3

1

Please make sure that you don't have any whitespace before your <?php statment. In your example above there are 4 spaced before the <?php. Note that this also is important for empty lines.

Sign up to request clarification or add additional context in comments.

1 Comment

In addition, a closing ?> tag is unnecessary in scripts that are entirely PHP. This will prevent empty lines at the end of the script from affecting things.
0

You can't have the same page being both HTML and an image. The way you do what you want is by having the HTML file contain an IMG tag that references this script.

<img src="yourscript.php">

Comments

0

An HTML file cannot have multiple header sections. Once you have rendered content on the page the call to header() will generate an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.