Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
$photoid = <photoid>;
$alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
// function to encode from photoid to shortcode
function base_encode($photoid, $alphabet)
{
$base_count = strlen($alphabet);
$encoded = '';
while ($photoid >= $base_count) {
$div = $photoid/$base_count;
$mod = ($photoid-($base_count*intval($div)));
$encoded = $alphabet[$mod] . $encoded;
$photoid = intval($div);
}
if ($photoid) $encoded = $alphabet[$photoid] . $encoded;
return $encoded;
}
$shortcode = base_encode($photoid, $alphabet);
// function to decode from shortcode to photoid
function base_decode($shortcode, $alphabet)
{
$decoded = 0;
$multi = 1;
while (strlen($shortcode) > 0) {
$digit = $shortcode[strlen($shortcode)-1];
$decoded += $multi * strpos($alphabet, $digit);
$multi = $multi * strlen($alphabet);
$shortcode = substr($shortcode, 0, -1);
}
return $decoded;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment