ChenXiu Posted August 5, 2021 Share Posted August 5, 2021 (edited) 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 usingfile_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 August 5, 2021 by ChenXiu Quote Link to comment https://forums.phpfreaks.com/topic/313497-base64-vs-readfile-display-secured-image/ Share on other sites More sharing options...
requinix Posted August 5, 2021 Share Posted August 5, 2021 Can you tell me what the differences are between those two ideas? How each one behaves? Quote Link to comment https://forums.phpfreaks.com/topic/313497-base64-vs-readfile-display-secured-image/#findComment-1588876 Share on other sites More sharing options...
ChenXiu Posted August 5, 2021 Author Share Posted August 5, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313497-base64-vs-readfile-display-secured-image/#findComment-1588879 Share on other sites More sharing options...
ChenXiu Posted August 6, 2021 Author Share Posted August 6, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313497-base64-vs-readfile-display-secured-image/#findComment-1588890 Share on other sites More sharing options...
ChenXiu Posted August 11, 2021 Author Share Posted August 11, 2021 6 days and counting... Quote Link to comment https://forums.phpfreaks.com/topic/313497-base64-vs-readfile-display-secured-image/#findComment-1589014 Share on other sites More sharing options...
requinix Posted August 11, 2021 Share Posted August 11, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313497-base64-vs-readfile-display-secured-image/#findComment-1589039 Share on other sites More sharing options...
Solution ChenXiu Posted August 12, 2021 Author Solution Share Posted August 12, 2021 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 😃 ) Quote Link to comment https://forums.phpfreaks.com/topic/313497-base64-vs-readfile-display-secured-image/#findComment-1589062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.