Jump to content

Base64 vs Readfile: Display Secured Image


ChenXiu
 Share

Go to solution Solved by ChenXiu,

Recommended Posts

An image is secured in the root directory, to be displayed to customer in html document.

This image can be displayed as a link to a php readfile page
The readfile page uses getimagesize("../rootdir/abc.png"), headers, and readfile()
echo "<img src='READFILE.php?image_name=abcd.png'>";
-or-
The image can be displayed directly to browser using
file_get_contents("../rootdir/abc.png"), base64_encode(), and data:image;base64.
echo '<img src="data:image;base64, ' .$img. ' " style="width:5in;height:5in;">';

Which is better? Why and why not?

Thank you.

Edited by ChenXiu
Link to comment
Share on other sites

Yes, I would be happy to.

Readfile() just grabs the file you tell it to grab, and spits it out* as an image, text file, etc., per whatever headers you have set at the beginning of the code. *Technically, it writes the file data into PHP's "Output Buffer" (the memory blocks allocated to PHP) from whence it interfaces with webserver software. The user's web browser interprets the html and renders the file as an image, pdf, or whatever.

file_get_contents() just grabs the file you tell it to, and reads the file into a string, rather than into the Output Buffer, like readfile() does.

That is why for large files, some authoritative posters say "Readfile is better for large files." But those posters, with their great authority, omit important details like "how large," and "large, compared to what," leaving the rest of us to guess whether a single 72 pixel 4" X 6" image is large or not (probably not).

Because file_get_contents() reads the file into a string, base64 has to be implemented and then you have to "tell that to your browser" via the "data:image;base64" implemented in the html <img src> code.

I'm going to guess for my purposes (e.g. a single 72 pixel 4" X 6" image) either way is just fine.

The reason I asked my question is that I am too much of a newbie to know
• all the errors that could possibly happen
• How to trap the errors if I don't know what errors might happen
• Which is more error prone? Readfile, or file_get_contents?
• browser compatibility?

So, I really have given this a lot of thought. I am hoping you have some insight on this.

Thank you.

 

Link to comment
Share on other sites

I sure hope that helped answer your question. If you have any other questions about PHP, or what various functions do, please don't hesitate to ask me, I would be glad to help. I've been doing PHP a long time. But if there is anything I don't know, I'll tell you a little secret: I go onto php.net and look them up. I find it very helpful. PHP is a wonderful programming language. In fact, a lot of forums are built around PHP -- their content seems to build pretty fast, and that shows how much interest there is in the topic.

Likewise, I hope I receive an answer to my question one day. I know it's pretty tricky.

Anyway, have a wonderful day.

Link to comment
Share on other sites

Sometimes I lose track of browser tabs.

To be clear, for this "72 pixel 4 X 6" image, are you talking about an image that renders to 4x6in at 72 dpi? That's about 3x4.5in or 288x432px at a standard monitor resolution of 96 dpi/ppi, which could be filesizes anywhere from 50KB to 300KB.
Now throw into that the fact that Base-64 encoding increases byte length by 33% and you're looking at embedding 75KB to 400KB into an <img>s src.

Link to comment
Share on other sites

  • Solution
17 hours ago, requinix said:

Base-64 encoding increases byte length

Good point. Thank you.
Using Data Base 64, the statistics are fairly humble:
image size: 16K
gzipped page size (using "page info" in browser): 19K
actual page size: 35K

For me, using readfile() seems to be the way to go.

Using this header in readfile, the image can have a tidy filename should a visitor want to right-click/save the image:
header('Content-Disposition: attachment; filename="Whatever_I_Want"');

Further, should the actual image be missing, readfile can display an image of an error message:
$imgData = getimagesize($complete_filename_and_path);
if( !$imageData ) { $file_name = "image-of-an-error.png"; }

Thank you again.
p.s. (even though I solved it before you did 😃 )

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.