开发者

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

This question already has answers here: Closed 11 years ago.

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜