HTML is not statically typed. It scales ok? Migrating data centers with zero downtime
Oct 31

Packaging images in your code

Tech Add comments

Packing up resources can sometimes be a challenge. What if you have a plugin that you want someone to be able to install. Do you make them unpack a set of images/css/… to a certain place?

I recently say a PHP plugin that encapsulated everything it did in the one PHP script. I was wondering where the images were, and then I saw them inline in the PHP code itself!

/**
* Images
*/
$images = array();
$images['subscribed.png'] = "iVBORw0KGgoAAAANSUhEUgAAABUAAAAICAMAAAAhgUThAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAGUExURai0vvP8+20m9Z0AAAACdFJOU/8A5bcwSgAAAE5JREFUeNpiYAQBBhBGAgABxAARZYBgRigPIIBgchAI1cgAEEBwFWDlUNMYAAIIWS3cIAaAAEIyFy7OyAAQQAxIbkDYBhBAcCaKywACDAAgyABfpZamZwAAAABJRU5ErkJggg==";
$images['subthread.png'] = "iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAAEF7NTqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAACmSURBVHjaYvz//z8DCAAEEBMDFAAEECNMBCCA4CIAAQQXgQGAAGJiQAMAAYSiAiCAGIEYzgMIIBSlAAGEogwggBignP/oGCQOEEAYNiADgADCKwkQQBiOhQGAAGIByzIyosgCFTMCBBBOHQABhFMHQADh1AEQQDh1AAQQTh0AAYRTAh8ACCBEzKBZhY8PEEBMDGQAgAAiSxNAAJGlCSCAyAoIgAADAEniPAbcSIg9AAAAAElFTkSuQmCC";
$images['subthread-open.png'] = "iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAAEF7NTqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAACoSURBVHjaYvz//z8DCAAEEBMDFAAEECNMBCCA4CIAAQQXgQGAAGJiQAMAAYSiAiCAQOg/VOA/QAChKAUIIBRlAAHEAFOCjkHiAAGEYQMyAAggvJIAAYThFBgACCAWsCwj4380dzICBBBOHQABhFMHQADh1AEQQDh1AAQQTh0AAYRTAh8ACCBEzKBZhWE6kjxAADExkAEAAogsTQABRJYmgAAiKyAAAgwA3YE+/1F5tWAAAAAASUVORK5CYII=";
$images['spacer.png'] = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQI12NgYGBgAAAABQABXvMqOgAAAABJRU5ErkJggg==";

if(isset($_GET['image']))
{
header("content-type: image/png");
print base64_decode($images[$_GET['image']]);
exit;
}

I never would have thought to encode my images and output them from my script itself. Oh those crazy PHP guys :)

6 Responses to “Packaging images in your code”

  1. Elliot Anderson Says:

    Been there, done that :P

    I got the idea from a little script I found once by CeleronDude that did something similar. Most of the time I would just use an absolute link to the images off my server but if the app needs to run on its own then I would package it into the script.

  2. Pramod Biligiri Says:

    I think base64 encoding of images is popular on the web. I’ve seen some Javascript code and Greasemonkey scripts use this to store images.

  3. Lee Says:

    on what weird ass planet is that clever? ever heard of maintainability?

    besides, its a CPU/RAM hit to access your script and chuck out the code for the image. If you simply put it in the equivalent of your htdocs dir its a lot less burden. Guess you aren’t envisioning your app ever being successful, but then again with techniques like that, I can understand why ;)

  4. Dion Says:

    Lee -

    I know :) I thought that someone with a ukonline account would get the sarcasm of the thing ;)

    Dion

  5. Lee Says:

    Dion – I think you’ve touched on one of the core differences between PHP and Java developers. PHP developers will do what seems cool, whereas Java developers (generally) do what works, regardless of its “cool” factor.

    Or maybe its just that I perceive Java developers to be more pragmatic? I don’t know… I’ve done both camps, and now reside in the Java camp – I’ve seen the error of my ways, and try to educate those that haven’t yet seen the light, but sometimes its difficult…

  6. Vik Says:

    This reminds me of those old x86 Assembler “intros” put out by the pirate groups. The whole thing would be packaged in one tiny EXE or COM file, with the GIF image completely inlined. The purpose was to make it easy to transport the intro across the wire so more people would see it — having external GIFs, or a zipped EXE plug GIF would certainly reduce viewership.

    I don’t see how those ideas translate to webapps. I mean they’re supposed to be centrally hosted. Plus, wouldn’t it be more beneficial to allow people to change the PNG if they wanted… easily?

    I certainly don’t think this calls for a generalization of JSP vs PHP programmers, because I’ve seen some pretty whacky things being done with JSP as well. What I do see is that a JSP person would be more likely to do this hack in an external class (with some “framework”) because that’s the prevailing mindset.

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'ajax'