Dynamic Images
So I’m working on the “about” section of my site, when I decided to photoshop a head shot to make it look cool and set it as a background for the text. Playing around with a couple of shots, I decided that I really liked both. However, I’m setting up the background using an attached CSS file, therefore I couldn’t dynamically change the source of the background. I could’ve gone with a JavaScript solution, but that would just be overkill. Oh, what to do?
Here’s what. You set a PHP file as the image background. PHP? Yes, not PNG, GIF, or JPG… PHP. How is this possible? Through the underrated power of MIME. In short, it’s sort of the format of the output of a page. A normal webpage has a META tag that looks something like this: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> This tells your page that it’s supposed to output it’s content in the form of “text/html”.
Using this concept, we will output an image from from a PHP file. This script goes through a specific folder, and will output a random image from the folder.
Here’s the actual script.
Although this might be something you will not be using in every project, but at least you now know it’s possible.