How do I directly display an image using PHP?

10,172
<?php
header('Content-Type: image/jpeg');
readfile('path/to/image.jpeg');
Share:
10,172
Michael Cheal
Author by

Michael Cheal

Updated on July 04, 2022

Comments

  • Michael Cheal
    Michael Cheal almost 2 years

    I have a question about displaying images in PHP. I need to have a PHP file display as JUST an image, as opposed to an image embedded in a web page, as if you had browsed to the JPEG image directly. The reason that I need it to be a PHP page as opposed to actually browsing to the image is that I need to resize the image before it is delivered. It would be easiest to use an image directly because that way I can display the image in a desktop application more easily. Is there any way to do this? Thanks!

  • SDC
    SDC over 11 years
    readfile() is better if no processing is required on the file contents, because it just sends the file direct to the browser without loading it into PHP's memory, whereas echo file_get_contents() loads it into PHP's memory then outputs that, so it's doing twice as much as readfile().
  • berkes
    berkes over 11 years
    Please include the relevant code, don't just link to answers. As per the FAQ.
  • Ja͢ck
    Ja͢ck over 11 years
    Surely if it was this easy, the web server itself could do it :)