Forum Moderators: coopster
image.php?a=red&b=big&c=png outputs the file red.big.png to the web accessible directory /output/ I've actually already picked a way of caching the output of this script but would like to discuss possible alternatives and get a feel for "best practices" in this area. Here are the two general methods of caching I had originally considered:
Method A) The PHP script is called through an <img /> tag and first uses the file_exists() function to see if the image has already been made. If it does exist, the PHP script uses the header() function and the readfile() function to output that existing image. Or if it does not exist, it generates the image, writes it to the static position then uses header() and readfile() to output the image.
Method B) The images are called using the static path in an <img /> tag. The directory
/output/ uses a custom 404 error page (image.php) which parses out the $_SERVER['REQUEST_URI'] with the explode() function to populate the input parameters and generate the image. It puts the image in the static folder, then uses header() and readfile() to output the image to the server. 1. What are the pro's and cons to these two different methods of caching the images?
2. Is there any reason why either method *can't* be used?
3. What other methods are there? Or, how would you improve on these methods?
I'd appreciate your input on this subject, or if you can recommend other Apache directives/PHP functions that would be useful in this scenario.
The other method has a tiny bit more overhead, of course, but only a tiny bit, and it should allow you to send the proper status code.
BTW, I've done similar simply by sending the right headers and using include() for the image file. In my case it wasn't that the image file might or might not be found, but someone wanted a specific item in the DB updated whenever specific images were called. So I just did the DB updates based on the request, then sent the headers and included the image file and it worked fine.... I think. I should dig that up and make sure ;-)
Seems that keeping all the caching/serving logic in the PHP scripts adds to portability and ease of updating. Does anyone feel as though there is any significant difference between using the file_exists PHP function or mod_rewrite's -f flag? I'd use mod_rewrite if it was noticeably better -- but it seems that readfile is a low overhead function and I imagine that file_exists is too.
;)
That said, I think if you do benchmark, drcrombie's last post is almost certain to be right.
I'm not actually sure that having it in .htaccess or httpd.conf would make that much difference *if* you're already allowing .htaccess with Allow Override. I *think* that the main drag on the server from .htaccess is enabling it at all so that the server is forced to recurse up through the directory hierarchy and check every directory for any parent .htaccess that is in force. Once you've set it to Allow Override, is httpd.conf significantly faster?
Anyway, like he said, not much of a muchness (whatever that means!).