Last active
February 3, 2019 12:13
-
-
Save sanikkenway/abb03cfd9c3b2316ee7d1f2c05ffa30b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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