Encode your images straight into PHP

Came across this today messing around with WAMP server. The index page it has has some images. I was looking to replace them as i was feeling lazy about creating an Index page

I couldnt find the images it was using, so had a look in the code. What they had done was encode the image straight into base65 encoding.

I like the idea, basically all you need is your PHP script, and no images, you dont have to worry about the permissions or directory structure or anything. Kind of nifty. Were you no further encode your scripts say with ioncube or the like, you could be fairly sure that your images in your script couldnt be replaced

Here is how it works:

1. Encode the image: http://www.motobit.com/util/base64-decoder-encoder.asp

2. use the result as a string in php, with <<< EOFILE and a carriage return preceeding it, and a carriage return and EOFILE; at the end of it.

3. Using the base64_decode() method, you can produce your image from the encoded string

WAMP use this in conjunction with a switch statement and GET variables to produce their images. Install WAMP and examine the index.php page it produces for a full working example.

I really like this. I know it beefs up your code size, making pages longer to load, but for something small, like a newsletter, it would save on calls to a central server.

Leave a comment