Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6879163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:48:44+00:00 2026-05-27T04:48:44+00:00

I am trying to output an image to a browser and then output HTML

  • 0

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:

<?php
    function LoadJpeg($imgname){
        /* Attempt to open */
        $im = @imagecreatefromjpeg($imgname);
        /* See if it failed */
        if(!$im){
            /* Create a black image */
            $im  = imagecreatetruecolor(150, 30);
            $bgc = imagecolorallocate($im, 255, 255, 255);
            $tc  = imagecolorallocate($im, 0, 0, 0);
            imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
            /* Output an error message */
            imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
        }
        return $im;
    }

    header('Content-Type: image/jpeg');
    $img = LoadJpeg('images/Ball.jpg');
    imagejpeg($img);
    imagedestroy($img);
    //trying to start my text here
    header('Content-Type text/html; charset=utf-8');
    echo "<br /><h2>ross</h2>";
?>

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:48:44+00:00Added an answer on May 27, 2026 at 4:48 am

    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:

    <img src="generateimage.php" alt="generated image"/>
    <br/>
    <h2>ross</h2>
    

    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:

    <html><body>
    <form action="view_card.php" method="post">
        Select an image:
        <select name="imgname">
            <option value="tree">Picture of Christmas tree</option>
            <option value="santa">Picture of Santa</option>
        </select><br/>
    
        Write a message:
        <textarea name="message"></textarea>
        <br/>
    
        <input type="submit" value="View Christmas card"/>
    </form>
    </body></html>
    

    view_card.php:

    <html>
      <body>
        Here is your Christmas card:
        <hr/>
    
        <!-- sending the requested image to the generateimage.php script
             as a GET parameter -->
        <img src="generateimage.php?imgname=<?php echo(urlencode($_POST['imgname'])); ?>"/>
        <?php echo(htmlspecialchars($_POST['message'])); ?>
      </body>
    </html>
    

    generateimage.php:

    <?php
    /* Stop evil hackers from accessing files they are not supposed to */
    $allowed_files = array('tree' => 'tree.jpg', 'santa' => 'santa.jpg');
    if( !isset($allowed_files[$_GET['imgname']])) {
        exit; // Thank you for playing...
    }
    
    /* Attempt to open */
    $im = @imagecreatefromjpeg($allowed_files[$_GET['imgname']]);
    
    /* See if it failed */
    if(!$im){
        /* Create a black image */
        $im  = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
    
        /* Output an error message */
        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }
    
    header('Content-Type: image/jpeg');
    imagejpeg($im);
    imagedestroy($im);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a CGI program that will output a PNG image to
I am using openCV python bindings. I am trying to show the output image
I'm trying to output a TrueColor image using GD (specifically bgd.dll) from a C++
Am trying to output an image when a Java Servlet link is called. Currently
I am trying to create a high quality thumbnail of this image, with Java
I'm trying to output an image from RRD Tool using Perl. I've posted the
I'm trying to output the unicode latin cross character described in this chart :
I'm trying to output a sentence containing 4 variables, with their values emboldened using
I am trying to output values of each rows from a DataSet : for
I am trying to output a few paragraphs of text in an Excel spreadsheet,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.