Issue with header() and display jpeg

Posted on 2004-11-25
Last Modified: 2012-05-05
Hi all,

I try to display a jpeg image with the following command :
header("Content-Type: image/jpeg");

But it raises the following error (sorry it's in french) :
L'image « http://localhost/redim2.php?image=3 » ne peut être affichée, car elle contient des erreurs.

If I comment the instruction header(), the image is displayed with a lot of strange characters but it's displayed.

So what i have to change to make it works ?

Question by:Richard Coffre
    LVL 48

    Accepted Solution

    Make sure you don't have any trailing ouput (special white-spaces ..). Search for that output and eleminate that output

    try this:
    <?php ob_start();
    ?>Your code<?php
    header("Content-Type: image/jpeg");

    // create/show image-code
    LVL 9

    Expert Comment

    I think that your code produces extra characters that mess up the picture. Do you send debugging messages?
    or does it look like:


    notice the blanks above the <?php. They will make php send an error about headers that cannot be sent, so your image will not be recognized.
    remove all whitespace before the <?php
    LVL 9

    Expert Comment

    I must learn to type faster ;)
    LVL 9

    Expert Comment

    Header is fine. Error says that image has errors and can't be shown. Can you show the code to display image?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now