Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a unique url for images (imgur.com like) in php? [duplicate]

Tags:

php

image

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

Trying to get smth like http://imgur.com/t4Syk We know that imgur uses 5 case sensitive string for image. How to do it?

like image 785
michael Avatar asked Sep 17 '11 15:09

michael


1 Answers

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.

like image 188
Arnaud Le Blanc Avatar answered Nov 19 '22 19:11

Arnaud Le Blanc