Skip to content
Advertisement

PHP GD Library output an image and text content on same page

I am trying to output an image to a browser and then output HTML (not directly related to the image) on the same page. Is this possible? I am having a heck of a time figuring it out. Here is my code I have been messing with:

JavaScript

Right near the bottom of my code is where I try to add in my html. when I run it, I only get the image, then no text after. If I try to move it to the top before the function, right after the opening php tag, the text works correctly, and then I get an error:

Warning: Cannot modify header information – headers already sent by (output started at /Applications/MAMP/htdocs/newimage.php:4) in /Applications/MAMP/htdocs/newimage.php on line 28

Any help would be greatly appreciated, thanks.

Advertisement

Answer

Stop and think for a moment. How would you normally embed an image in a HTML file? You create two files: text.html and image.jpg. Same here, you will create two scrips, one that outputs the HTML and one that generates the image. The HTML would look like:

JavaScript

The generateimage.php script only generates the image.

Lets take for example a form that allows the user to create a digital Christmas card: he can select the image and write a personal note beneath it.

form.html:

JavaScript

view_card.php:

JavaScript

generateimage.php:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement