1

How may I display an image without using any kind of HTML.

Let's say I have some protected images, which may only be shown to some users. The protected image is in directory images/protected/NoOneCanViewMeDirectly.png

Then we have a PHP file in the directory images/ShowImage.php, which check if the user is permitted to view the image.

How may I display the image using PHP only.

If you're not sure what I mean this is how I want to display the image. http://gyazo.com/077e14bc824f5c8764dbb061a7ead058

The solution is not echo '<img src="images/protected/NoOneCanViewMeDirectly.png">';

5
  • 1
    <img src="file.php"> and use a header. Commented Dec 29, 2014 at 11:46
  • This is not a question. Commented Dec 29, 2014 at 11:49
  • @EngineerCoder It is, just not in the typical syntax Commented Dec 29, 2014 at 11:50
  • How exactly isn't this a question? "How may I display the image using PHP only." Commented Dec 29, 2014 at 11:52
  • @Kaare Actually, this question's been asked before. You just needed to Google your question's title. You've not shown us what you tried. You only mention/asked "how". Commented Dec 29, 2014 at 11:54

3 Answers 3

4

You could just header out the image with the following syntax

header("Content-type: image/png");
echo file_get_contents(__DIR__."/images/protected/NoOneCanViewMeDirectly.png");
Sign up to request clarification or add additional context in comments.

6 Comments

1) Content type should be image/png for png files 2) better use readfile(...)
Thank you. That was what I searched for.
image/jpg and .png?
I didn't noticed the png, edited it into the answer. Never worked with readfile, so I don't know if it's better or not, but it works for me
also __DIR__ will return you current location of script file, not root directory, so if you have this code in say /modules/images/functions.php your image path will be /modules/images/iamges/protected/...
|
2
if (<here your logic to show file or not>)
    $path = $_SERVER["DOCUMENT_ROOT"].<path_to_your_file>; // to get it correctly
    $ext = explode(".", $path);
    header("Content-Type: image/".array_pop($ext)); // correct MIME-type
    header('Content-Length: ' . filesize($path)); // content length for most software
    readfile($path); // send it
}

Note: 1. image/jpg is not correct MIME-type, use image/jpeg instead (additional check needed), 2. readlfile() is better than echo file_get_contents() for binary data, 3. always try to provide content length for browsers and other software

Comments

-1

You can use imagepng to output png image to browser:

$im = imagecreatefrompng("images/protected/NoOneCanViewMeDirectly.png");

header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.