8

Possible Duplicate:
PHP: How to generate a random, unique, alphanumeric string?

Trying to get smth like https://i.stack.imgur.com/3fB75.jpg We know that imgur uses 5 case sensitive string for image. How to do it?

Community
  • 1
  • 1
michael
  • 129
  • 2
  • 5

1 Answers1

12

You could just use your auto_increment image id, converted to base58 (a-zA-Z0-9) for example.

base_convert can convert up to base36:

$id = base_convert(123456789, 10, 36); // "21i3v9"

(See also PHP - How to base_convert() up to base 62)

If you want non-predictable image ids, look at this answer.


For MongoDB IDs (as you are using MongoDB):

The ids are 12 bytes numbers, encoded to base16, which makes them 24 bytes.

You can compress them to 17 bytes by converting them from base16 to base58:

gmp_strval(gmp_init("47cc67093475061e3d95369d", 16), 58)); // "1KXotnQBQbcPmeOo9"

Also take a look at the Sequence Numbers section here. This will allow you to generate smaller unique numbers for your images.

Community
  • 1
  • 1
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194