Hi guys, Can somebody direct me to a solutions about this problem please. I tried to output an image in my page directly to the browser. Here's an example: <?php echo '<html><body>'; echo 'Picture title here'; $image = imagecreatetruecolor($width, $height); header("Content-type: image/png"); imagepng($image); echo '</body></html>'; ?> PHP: Unfortunately, it produce error "Cannot modify header information - headers already sent....." If I change my code to the following code <?php $image = imagecreatetruecolor($width, $height); header("Content-type: image/png"); imagepng($image); echo '<html><body>'; echo 'Picture title here'; echo '</body></html>'; ?> PHP: then it works but the image is display on the top of the page and title is display under the image. What I want to display my picture title on the top of the page and then image under the picture title. Is there any way to fix this problem. Thanks.
No, there isn't. Headers are sent whenever output is sent, you cannot have a single file which is both plaintext and a .png file itself. The best work around would be to either make the image include the title, or have a separate HTML document to have both the title, and an <img> tag to the image. Dan
<?php ob_start(); echo '<html><body>'; echo 'Picture title here'; $image = imagecreatetruecolor($width, $height); $output = ob_get_content(); ob_end_clean(); header("Content-type: image/png"); echo $output; imagepng($image); echo '</body></html>'; ?>
That would be the proper way to do it, as suresh pointed out, since you have to do any header() changes BEFORE any content even gets output. But as Danltn pointed out, you can't output HTML and call it a PNG image.
No, that will also FAIL because echo is sending text to the browser. If you're using header() you cannot send ANYTHING to the browser before. Have two files. The first calls the second using IMG, for example: <img src="imagemaker.php"> Code (markup):
No, that will also FAIL because echo is sending text to the browser. If you're using header() you cannot send ANYTHING to the browser before. Code (markup): Re-read the code as you're incorrect, although Suresh's code is invalid, this is not the reason. He is using output buffering to stop output of text.
Do not echo <html> tags .. This one works for me, so .. it should work for you as well ! <?php echo "Nice picture"; $image = imagecreatetruecolor($width, $height); header("Content-type: image/png"); imagepng($image); ?> PHP:
How can that work for you? Echoing out plaintext, such as "Nice picture", will cause the image to be corrupt.