It looks like your goals are: 1. to visually hide the URL and 2. to generally encode the data for use in the URL.
First, we need to eclipse the URL. Since URLs use most of the Base64 dictionary, any encoding that creates the binary (which should then be Base64-ed) is likely to simply increase the size. Itβs best to keep the dictionary in a URL-safe range with minimal escaping when using urlencode() . That is, you want this:
function rotUrl($url) { return strtr($url, 'abcdefghijklmnopqrstuvwxyz0-:/?=&%#123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0-:/?=&%#'); }
Now, to save bytes, we can encode the URL scheme into one char (say, h for HTTP, h for HTTPS) and convert the sizes to base 32. Finishing this:
function obscure($width, $height, $url) { $dimensions = base_convert($width, 10, 32) . "." . base_convert($height, 10, 32) . "."; preg_match('@^(https?)://(.+)@', $url, $m); return $dimensions . (($m[1] === 'http') ? 'h' : 'H') . rotUrl($m[2]); } function unobscure($str) { } $url = 'https://en.wikipedia.org/w/index.php?title=Special%3ASearch&search=Base64'; $obs = obscure(550, 300, $url);
Since we avoided characters that do not contain URLs, if this is placed in querystring (with urlencode ), it does not grow much (in this case, not at all).
In addition, you may want to sign this line so that people who know the encoding still cannot specify their parameters through the URL. For this you must use HMAC , and Base64URL - hash encoding. You can also just save the hash substring (~ 6 bits per character) to save space. sign() (below) adds an 8-digit MAC (48 bit hash at 6 bits / char):
function sign($key, $data) { return $data . _hmac($key, $data, 8); } function verify($key, $signed) { $mac = substr($signed, -8); $data = substr($signed, 0, -8); return $mac === _hmac($key, $data, 8) ? $data : false; } function _hmac($key, $data, $macLength) { $mac = substr(base64_encode(hash_hmac('sha256', $data, $key, true)), 0, $macLength); return strtr($mac, '+/', '-_'); // for URL } $key = "Hello World!"; $signed = sign($key, $obs); // appends MAC: "w-jjw2Wm" $obs = verify($key, $signed); // strips MAC and returns valid data, or FALSE
Update: Better RotURL function .