1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

header("Content-type: image/png") Help..

Discussion in 'PHP' started by php-lover, Dec 20, 2008.

  1. #1
    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.
     
    php-lover, Dec 20, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    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
     
    Danltn, Dec 20, 2008 IP
  3. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks Danltn
     
    php-lover, Dec 20, 2008 IP
  4. suresh2220

    suresh2220 Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?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>';

    ?>
     
    suresh2220, Dec 21, 2008 IP
  5. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    zerxer, Dec 21, 2008 IP
  6. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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):
     
    Yesideez, Dec 22, 2008 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    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.
     
    Danltn, Dec 22, 2008 IP
  8. farad

    farad Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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:
     
    farad, Dec 22, 2008 IP
  9. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #9
    How can that work for you? Echoing out plaintext, such as "Nice picture", will cause the image to be corrupt.
     
    zerxer, Dec 22, 2008 IP