20 November 2006
Dynamic Image Generation in PHP
Through use of the GD libraries, it’s long been possible to write images in code with PHP. They’re unlikely to be photographic quality, but for captcha’s, counters, charts, etc, it’s unlikely that it needs to be. But this particular feature seems to be generally overlooked. PHP: Dynamic Image Generation is a nice tutorial for using this image generation facilities.
Hopefully, when and if you first write <img src="controller.php/counter/show" > it will hit you that PHP is not just for HTML. You can write custom stylesheets, client-side scripts, PDFs or near anything to be found on the web. All you need to do is manipulate the HTTP content-type before delivery.
Finally, on a deeper level, Jakob Nielsen’s Death of File Systems should hit you. You’ve long known that ending URLs with .php, .html, .css, .xhtml, etc, really doesn’t matter. But on a deeper level, the way a file of those types is delivered also doesn’t matter. A PHP script with no extension can quite feasibly deliver a PDF. It may not be a design goal of the language, but it is certainly possible, and should be embraced. Just as PHP/HTML/CGI/JSP/ASPX is insignificant to users, so is JPG/PNG/GIF. It also does not matter to users if the file is stored in a /images/ directory. The data of the file is all that matters. HTTP’s content-type currently nullifies the need for the browser to determine a file type, but eventually, application developers should be expected to deliver a quick-acting analysis routine which can claim ownership of a certain file based solely on the data. And when that day dawns, the file extension will finally be forever dead.
2 Comments currently posted.
Carl says:
Brian Bosh says:
In reality, I use multiple controllers. But I separate them by function, not type: I’ll have a admin.php and a public.php usually, with index.php a one liner to include public. Type is irrelevant to anything.
I could imagine, however, a controller like maps.php which happens to only generate images, but its function is to generate the map. Think of a map as an implementation of a PNG. An address like png.php?address=123%20Main%st&city=new%20york%20city&st=NY&zip=10001 doesn’t make sense because PNG does not support those options. But map.php?address=123%20Main%st&city=new%20york%20city&st=NY&zip=10001 which happens to generate a PNG makes perfect sense.


Monolithic controllers have a bad rep but I tend to like the idea of them.
What do you think of a system that places all content above the web root (ie /public_html) - all user requests for content pass through a specific php controller file.
The web root could be limited to the following files for example:
/public_html/index.php
/public_html/file_download.php
/public_html/image.php
/public_html/css.php
A content-type specific controller I suppose, with the actual content above any publicly accessible folders.