You could generate directory names using hashes. By that i mean like images/generatedhashname/image.jpg or whichever hash way you choose.
Originally Posted by TheOner
A way to generate very short hashes is the crc32() hash.
Examples when using Hashes,
With CR32 hash
With MD5 hash:
With SHA1 hash:
cr32 therefore makes shorter hashes suited for this type of thing.
This way can be done when storing the directory names and retrieving the directory names from the database just like how you store and retrieve passwords.
If this method helps you or is contentious in any way,let me know.
For more info on how to implement the cr32 hash go to http://www.w3schools.com/php/func_string_crc32.asp or google it.