Skip to content

Instantly share code, notes, and snippets.

@sanikkenway
Last active February 3, 2019 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanikkenway/abb03cfd9c3b2316ee7d1f2c05ffa30b to your computer and use it in GitHub Desktop.
Save sanikkenway/abb03cfd9c3b2316ee7d1f2c05ffa30b to your computer and use it in GitHub Desktop.
<?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